Skip to content

updates

updates #75

Workflow file for this run

name: Deploy to VPS
on:
push:
branches:
- main
env:
SSH_AUTH_SOCK: /tmp/ssh_agent.sock
jobs:
deploy:
runs-on: ubuntu-latest
steps:
# Step 1: Checkout code
- name: Checkout code
uses: actions/checkout@v2
# Step 2: Install Dependencies
- name: Install Dependencies
run: |
sudo apt-get update
sudo apt-get install -y sshpass rsync curl
# Step 3: Install Docker Compose
- name: Install Docker Compose
run: |
sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version
# Step 4: Setup SSH with passphrase
- name: Setup SSH passphrase
env:
SSH_PASSPHRASE: ${{ secrets.SSH_PASSPHRASE }}
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
run: |
ssh-agent -a $SSH_AUTH_SOCK > /dev/null
echo 'echo $SSH_PASSPHRASE' > ~/.ssh_askpass && chmod +x ~/.ssh_askpass
echo "$SSH_PRIVATE_KEY" | tr -d '\r' | DISPLAY=None SSH_ASKPASS=~/.ssh_askpass ssh-add - >/dev/null
echo "SSH key added"
# Step 5: Verify SSH Connection
- name: Verify SSH Connection
env:
VPS_IP: ${{ secrets.VPS_IP }}
run: |
sshpass -p "${{ secrets.SSH_PASSPHRASE }}" ssh -o StrictHostKeyChecking=no docker@${VPS_IP} "echo Connection successful"
# Step 6: Copy files to VPS
- name: Copy files to VPS
env:
VPS_IP: ${{ secrets.VPS_IP }}
run: |
sshpass -p "${{ secrets.SSH_PASSPHRASE }}" rsync -avz -e "ssh -o StrictHostKeyChecking=no" ./ docker@${VPS_IP}:/backend
echo "Files copied to VPS"
# Step 7: SSH and Deploy Docker
- name: SSH and Deploy Docker
env:
VPS_IP: ${{ secrets.VPS_IP }}
run: |
sshpass -p "${{ secrets.SSH_PASSPHRASE }}" ssh -o StrictHostKeyChecking=no docker@${VPS_IP} << 'EOF'
cd /backend
# Backup the current running container
if docker ps -q -f name=backend; then
docker tag backend:latest backend:backup || echo "Failed to tag existing image"
fi
# Try to build and deploy the new Docker container
if ! docker-compose down || ! docker-compose up -d --build --remove-orphans; then
echo "Deployment failed. Rolling back..."
docker-compose down
if docker images | grep -q 'backend:backup'; then
docker tag backend:backup backend:latest
docker-compose up -d
echo "Rollback to the previous version was successful."
else
echo "No backup available for rollback. Exiting..."
exit 1
fi
fi
echo "Deployment successful"
EOF
# Step 8: Verify Container Health
- name: Verify Container Health
env:
VPS_IP: ${{ secrets.VPS_IP }}
run: |
health_status=$(sshpass -p "${{ secrets.SSH_PASSPHRASE }}" ssh -o StrictHostKeyChecking=no docker@${VPS_IP} << 'EOF'
container_id=$(docker ps -q -f name=api)
if [ -z "$container_id" ]; then
echo "No container found with name 'api'."
exit 1
else
docker inspect --format '{{json .State.Health.Status}}' "$container_id"
fi
EOF
)
if [[ "$health_status" != "\"healthy\"" ]]; then
echo "API container is unhealthy! Status: $health_status"
exit 1
else
echo "API container is healthy."
fi
# Step 9: Notify Slack - Success
- name: Notify Slack - Success
if: success()
uses: slackapi/[email protected]
with:
method: chat.postMessage
token: ${{ secrets.SLACK_BOT_TOKEN }}
payload: |
channel: ${{ secrets.SLACK_CHANNEL_ID }}
text: "✅ Backend Deployment to VPS succeeded! 🚀"
attachments:
- color: "36a64f"
fields:
- title: "Status"
short: true
value: "Success"
- title: "Deployment Details"
short: true
value: "The latest code was successfully deployed to the VPS."
# Step 10: Notify Slack - Failure
- name: Notify Slack - Failure
if: failure()
uses: slackapi/[email protected]
with:
method: chat.postMessage
token: ${{ secrets.SLACK_BOT_TOKEN }}
payload: |
channel: ${{ secrets.SLACK_CHANNEL_ID }}
text: "❌ Backend Deployment to VPS failed! 😞"
attachments:
- color: "ff0000"
fields:
- title: "Status"
short: true
value: "Failed"
- title: "Error"
short: true
value: "The deployment process encountered an issue. Check the logs for details."