Skip to content

Commit

Permalink
tests deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
kayprogrammer committed Dec 23, 2024
1 parent df19158 commit 0d919b6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 123 deletions.
154 changes: 52 additions & 102 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,120 +1,70 @@
name: Deploy to VPS
name: Deploy API and Monitor Health

on:
push:
branches:
- main

env:
SSH_AUTH_SOCK: /tmp/ssh_agent.sock

jobs:
deploy:
runs-on: ubuntu-latest

steps:
# Checkout code
- name: Checkout code
uses: actions/checkout@v2

# Install dependencies
- name: Install Dependencies
run: |
sudo apt-get update
sudo apt-get install -y sshpass rsync
# Step 1: Checkout the code from the repository
- name: Checkout code
uses: actions/checkout@v2

# 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 2: Set up Docker
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

# 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"
- name: Build and push Docker image
run: |
docker-compose -f docker-compose.yml build api
docker-compose -f docker-compose.yml up -d
# 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"
# SSH and deploy with 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"
# Step 3: Check container health
- name: Check API container health
run: |
container_health=$(docker inspect --format '{{.State.Health.Status}}' myapp_api)
if [ "$container_health" != "healthy" ]; then
echo "API container is unhealthy, sending Slack notification..."
curl -X POST -H 'Content-type: application/json' \
--data '{"text":"❌ API container failed health check! Check logs."}' \
https://hooks.slack.com/services/your/slack/webhook
exit 1 # Fail the pipeline
else
echo "API container is healthy."
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
# Step 4: Run tests or post-deployment tasks (optional)
- name: Run tests
run: |
# Add your testing commands here
echo "Tests go here."
echo "Deployment successful"
EOF
# Step 5: Clean up
- name: Tear down Docker containers
run: |
docker-compose -f docker-compose.yml down
# 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 6: Slack notification on success
- name: Notify Slack on success
if: success()
uses: slackapi/[email protected]
with:
method: chat.postMessage
token: ${{ secrets.SLACK_BOT_TOKEN }}
payload: |
channel: ${{ secrets.SLACK_CHANNEL_ID }}
text: "✅ Deployment succeeded! API container is healthy."
attachments:
- color: "00FF00"
fields:
- title: "Status"
short: true
value: "Success"
- title: "API Health"
short: true
value: "Healthy"
# 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."
# Step 7: Slack notification on failure (already covered in Step 3)
19 changes: 0 additions & 19 deletions Dockerfile-Prod

This file was deleted.

13 changes: 11 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: "3.8"

services:
db:
restart: always
Expand All @@ -17,8 +19,9 @@ services:
retries: 5
networks:
- shared_network

api:
restart: always
restart: "no" # Ensures that Docker doesn't automatically restart after failure
build:
context: ./
dockerfile: Dockerfile
Expand All @@ -33,6 +36,12 @@ services:
depends_on:
- db
command: air ./main.go -b 0.0.0.0
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/api/v1/general/site-detail"] # Health check endpoint
interval: 30s
retries: 3
start_period: 10s
timeout: 5s
networks:
- shared_network

Expand All @@ -56,4 +65,4 @@ volumes:

networks:
shared_network:
driver: bridge
driver: bridge

0 comments on commit 0d919b6

Please sign in to comment.