Add CI/CD deployment pipeline with Forgejo Actions
- Add .forgejo/workflows/deploy.yml for automated deployment - Include rollback mechanism with image tagging - Add health check verification (http://127.0.0.1:3100/health) - Create manual rollback script for emergency use - Add deployment documentation and setup instructions - Supports auto-rollback on deployment failure
This commit is contained in:
parent
0453176544
commit
7d3525fe57
4 changed files with 290 additions and 0 deletions
100
.forgejo/workflows/deploy.yml
Normal file
100
.forgejo/workflows/deploy.yml
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
name: Deploy to Production
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
name: Deploy to Server
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Deploy via SSH
|
||||
uses: appleboy/ssh-action@v1.1.0
|
||||
with:
|
||||
host: ${{ secrets.SERVER_HOST }}
|
||||
username: ${{ secrets.SERVER_USER }}
|
||||
key: ${{ secrets.SSH_PRIVATE_KEY }}
|
||||
script: |
|
||||
set -e
|
||||
|
||||
echo "🚀 Starting deployment..."
|
||||
|
||||
# Navigate to project directory
|
||||
cd /root/docfast
|
||||
|
||||
# Check current git status
|
||||
echo "📋 Current status:"
|
||||
git status --short || true
|
||||
|
||||
# Pull latest changes
|
||||
echo "📥 Pulling latest changes..."
|
||||
git fetch origin
|
||||
git pull origin main
|
||||
|
||||
# Tag current running image for rollback
|
||||
echo "🏷️ Tagging current image for rollback..."
|
||||
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
|
||||
docker tag docfast-docfast:latest docfast-docfast:rollback-$TIMESTAMP || echo "No existing image to tag"
|
||||
|
||||
# Build new image
|
||||
echo "🔨 Building new Docker image..."
|
||||
docker compose build --no-cache
|
||||
|
||||
# Stop services gracefully
|
||||
echo "⏹️ Stopping services..."
|
||||
docker compose down --timeout 30
|
||||
|
||||
# Start services
|
||||
echo "▶️ Starting services..."
|
||||
docker compose up -d
|
||||
|
||||
# Wait for service to be ready
|
||||
echo "⏱️ Waiting for service to be ready..."
|
||||
for i in {1..30}; do
|
||||
if curl -f -s http://127.0.0.1:3100/health > /dev/null; then
|
||||
echo "✅ Service is healthy!"
|
||||
break
|
||||
fi
|
||||
if [ $i -eq 30 ]; then
|
||||
echo "❌ Service failed to start - initiating rollback..."
|
||||
docker compose down
|
||||
|
||||
# Try to rollback to previous image
|
||||
ROLLBACK_IMAGE=$(docker images --format "table {{.Repository}}:{{.Tag}}" | grep "docfast-docfast:rollback-" | head -n1 | tr -s ' ' | cut -d' ' -f1)
|
||||
if [ ! -z "$ROLLBACK_IMAGE" ]; then
|
||||
echo "🔄 Rolling back to $ROLLBACK_IMAGE"
|
||||
docker tag $ROLLBACK_IMAGE docfast-docfast:latest
|
||||
docker compose up -d
|
||||
|
||||
# Wait for rollback to be healthy
|
||||
sleep 10
|
||||
if curl -f -s http://127.0.0.1:3100/health > /dev/null; then
|
||||
echo "✅ Rollback successful"
|
||||
else
|
||||
echo "❌ Rollback failed - manual intervention required"
|
||||
fi
|
||||
else
|
||||
echo "❌ No rollback image available"
|
||||
fi
|
||||
|
||||
exit 1
|
||||
fi
|
||||
echo "⏳ Attempt $i/30 - waiting 5 seconds..."
|
||||
sleep 5
|
||||
done
|
||||
|
||||
# Cleanup old rollback images (keep last 5)
|
||||
echo "🧹 Cleaning up old rollback images..."
|
||||
docker images --format "table {{.Repository}}:{{.Tag}}" | grep "docfast-docfast:rollback-" | tail -n +6 | awk '{print $1":"$2}' | xargs -r docker rmi || true
|
||||
|
||||
# Final health check and status
|
||||
echo "🔍 Final health check..."
|
||||
HEALTH_STATUS=$(curl -f -s http://127.0.0.1:3100/health || echo "UNHEALTHY")
|
||||
echo "Health status: $HEALTH_STATUS"
|
||||
|
||||
echo "📊 Service status:"
|
||||
docker compose ps
|
||||
|
||||
echo "🎉 Deployment completed successfully!"
|
||||
Loading…
Add table
Add a link
Reference in a new issue