Build and Deploy to Cloud #166
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and Deploy to Cloud | |
on: | |
workflow_dispatch: | |
jobs: | |
build_and_deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Install dependencies | |
run: | | |
sudo apt update -y && sudo apt install -y curl | |
curl -fsSL https://get.docker.com -o get-docker.sh | |
chmod +x get-docker.sh | |
./get-docker.sh | |
- name: Build Docker image | |
env: | |
WOLFRAM_APP_ID: ${{ secrets.WOLFRAM_APP_ID }} | |
EC2_HOST: ${{ secrets.EC2_HOST }} | |
BASIC_USER: ${{ secrets.BASIC_USER }} | |
BASIC_PASSWORD: ${{ secrets.BASIC_PASSWORD }} | |
run: | | |
echo "BASIC_USER=$BASIC_USER" >> .creds | |
echo "BASIC_PASSWORD=$BASIC_PASSWORD" >> .creds | |
echo "WOLFRAM_APP_ID=$WOLFRAM_APP_ID" >> .creds | |
docker build --secret id=creds,src=.creds --build-arg public_ip=$EC2_HOST -t ramanujan-machine-web-portal:latest . | |
- name: Save Docker image as tarball | |
run: | | |
docker save ramanujan-machine-web-portal:latest | gzip > image.tar.gz | |
ls -al image.tar* | |
- name: Prepare SSH dir | |
run: | | |
mkdir -pv ~/.ssh/ | |
- name: Write key | |
env: | |
EC2_SSH_KEY: ${{ secrets.EC2_SSH_KEY }} | |
run: | | |
echo "$EC2_SSH_KEY" > ~/.ssh/actions.key | |
chmod 600 ~/.ssh/actions.key | |
- name: Write SSH config | |
env: | |
EC2_HOST: ${{ secrets.EC2_HOST }} | |
EC2_USERNAME: ${{ secrets.EC2_USERNAME }} | |
run: | | |
cat >>~/.ssh/config <<END | |
Host ec2 | |
HostName $EC2_HOST | |
User $EC2_USERNAME | |
IdentityFile ~/.ssh/actions.key | |
StrictHostKeyChecking=no | |
ServerAliveCountMax=10 | |
ServerAliveInterval=60 | |
END | |
- name: Copy Docker image to EC2 instance | |
env: | |
EC2_USERNAME: ${{ secrets.EC2_USERNAME }} | |
run: | | |
scp image.tar.gz ec2:/home/$EC2_USERNAME | |
- name: Ensure Docker is installed and running | |
run: | | |
ssh ec2 'sudo yum install docker' | |
ssh ec2 'sudo systemctl start docker' | |
- name: Stop running container | |
run: | | |
ssh ec2 'sudo docker ps -q | xargs --no-run-if-empty docker stop | xargs --no-run-if-empty docker rm' | |
- name: Configure Docker & load lmage | |
env: | |
EC2_USERNAME: ${{ secrets.EC2_USERNAME }} | |
run: | | |
ssh ec2 "sudo docker load -i /home/$EC2_USERNAME/image.tar.gz" | |
- name: Delete gzipped image tarball | |
env: | |
EC2_USERNAME: ${{ secrets.EC2_USERNAME }} | |
run: | | |
ssh ec2 "rm -f /home/$EC2_USERNAME/image.tar.gz" | |
- name: Run Docker container on EC2 instance | |
run: | | |
ssh ec2 "sudo docker run -p 80:80 -d ramanujan-machine-web-portal:latest" | |
- name: Cleanup Docker detritus | |
run: | | |
ssh ec2 "sudo docker system prune -f" |