Skip to content

Workflow file for this run

name: CI/CD Pipeline
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build Docker image
run: |
docker build -t my-backend:latest -f Dockerfile .
- name: Save Docker image to a tar file
run: |
docker save my-backend:latest -o my-backend.tar
- name: Create SSH key file
run: |
echo "${{ secrets.VM_SSH_KEY }}" > /tmp/vm_ssh_key.pem
chmod 600 /tmp/vm_ssh_key.pem
- name: Copy Docker image to VM
run: |
scp -i /tmp/vm_ssh_key.pem -o StrictHostKeyChecking=no my-backend.tar ${{ secrets.VM_USERNAME }}@${{ secrets.VM_HOST }}:/home/${{ secrets.VM_USERNAME }}/my-backend.tar
- name: Load Docker image and run container on VM
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.VM_HOST }}
username: ${{ secrets.VM_USERNAME }}
key: ${{ secrets.VM_SSH_KEY }}
script: |
if ! command -v docker &> /dev/null
then
echo 'Docker not found. Installing Docker...'
sudo apt-get update
sudo apt-get install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker ${{ secrets.VM_USERNAME }}
newgrp docker
fi
docker load -i /home/${{ secrets.VM_USERNAME }}/my-backend.tar &&
rm /home/${{ secrets.VM_USERNAME }}/my-backend.tar &&
docker run -d --name my-backend-container -p 80:80 my-backend:latest
- name: Clean up SSH key file
run: |
rm /tmp/vm_ssh_key.pem