|
| 1 | +name: deploy-express-serverside |
| 2 | +on: |
| 3 | + workflow_dispatch: |
| 4 | + inputs: |
| 5 | + reason: |
| 6 | + description: 'Reason for deploying Express API' |
| 7 | + required: true |
| 8 | + default: 'Manual trigger' |
| 9 | + |
| 10 | +jobs: |
| 11 | + deploy-express: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Trigger server-side deployment |
| 15 | + run: | |
| 16 | + echo "🚀 Triggering server-side deployment..." |
| 17 | + echo "Reason: ${{ github.event.inputs.reason || 'Push to master' }}" |
| 18 | + echo "Commit: ${{ github.sha }}" |
| 19 | + echo "Branch: ${{ github.ref_name }}" |
| 20 | +
|
| 21 | + - name: Configure SSH key |
| 22 | + uses: shimataro/ssh-key-action@v2 |
| 23 | + with: |
| 24 | + key: ${{ secrets.DO_SSH_KEY }} |
| 25 | + name: id_rsa |
| 26 | + known_hosts: ${{ secrets.KNOWN_HOSTS }} |
| 27 | + |
| 28 | + - name: Execute deployment on server |
| 29 | + run: | |
| 30 | + echo "Connecting to server and executing deployment..." |
| 31 | + ssh ${{ secrets.DO_USERNAME }}@eduquilt.com << 'EOF' |
| 32 | + set -e |
| 33 | +
|
| 34 | + # Log deployment start |
| 35 | + echo "=== Deployment started at $(date) ===" |
| 36 | + echo "Triggered by: ${{ github.event.inputs.reason || 'Push to master' }}" |
| 37 | + echo "Commit: ${{ github.sha }}" |
| 38 | + echo "Branch: ${{ github.ref_name }}" |
| 39 | +
|
| 40 | + # Change to deployment script directory |
| 41 | + cd /home/skuilder |
| 42 | +
|
| 43 | + # Execute the deployment script |
| 44 | + ./deploy-express-minimal-downtime.sh |
| 45 | +
|
| 46 | + echo "=== Deployment completed at $(date) ===" |
| 47 | + EOF |
| 48 | +
|
| 49 | + - name: Verify deployment |
| 50 | + run: | |
| 51 | + echo "Verifying Express API deployment..." |
| 52 | + max_retries=3 |
| 53 | + retry_count=0 |
| 54 | + while [ $retry_count -lt $max_retries ]; do |
| 55 | + response=$(curl -sS https://eduquilt.com/express) |
| 56 | + if [[ $? -ne 0 ]]; then |
| 57 | + echo "Error: Failed to fetch https://eduquilt.com/express" |
| 58 | + retry_count=$((retry_count+1)) |
| 59 | + if [ $retry_count -lt $max_retries ]; then |
| 60 | + echo "Retrying in 5 seconds... (Attempt $retry_count of $max_retries)" |
| 61 | + sleep 5 |
| 62 | + continue |
| 63 | + else |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | + fi |
| 67 | +
|
| 68 | + if echo "$response" | grep -q "${{ github.SHA }}"; then |
| 69 | + echo "Success. Current version found in production" |
| 70 | + |
| 71 | + # Log deployment success |
| 72 | + echo "✅ Express API deployed successfully with version ${{ github.SHA }}" |
| 73 | + echo "✅ Deployment triggered by: ${{ github.event.inputs.reason || 'Push to master' }}" |
| 74 | + echo "✅ Deployment timestamp: $(date -u)" |
| 75 | + |
| 76 | + exit 0 |
| 77 | + else |
| 78 | + echo "Current version not found in production. Retrying..." |
| 79 | + retry_count=$((retry_count+1)) |
| 80 | + if [ $retry_count -lt $max_retries ]; then |
| 81 | + echo "Retrying in 5 seconds... (Attempt $retry_count of $max_retries)" |
| 82 | + sleep 5 |
| 83 | + else |
| 84 | + echo "Error! Max retries reached. Current version not reported by production." |
| 85 | + echo "Response received:" |
| 86 | + echo "$response" |
| 87 | + exit 1 |
| 88 | + fi |
| 89 | + fi |
| 90 | + done |
| 91 | +
|
| 92 | + - name: Deployment failure cleanup |
| 93 | + if: failure() |
| 94 | + run: | |
| 95 | + echo "🔄 Deployment failed. Checking for rollback options..." |
| 96 | + ssh ${{ secrets.DO_USERNAME }}@eduquilt.com << 'EOF' |
| 97 | + cd /home/skuilder |
| 98 | + echo "Available builds for potential rollback:" |
| 99 | + ./manage-deployment.sh list |
| 100 | +
|
| 101 | + echo "Current service status:" |
| 102 | + ./manage-deployment.sh status |
| 103 | +
|
| 104 | + echo "Recent service logs:" |
| 105 | + ./manage-deployment.sh logs 20 || true |
| 106 | + EOF |
| 107 | +
|
| 108 | + echo "💡 To rollback manually, SSH to server and run:" |
| 109 | + echo " ./manage-deployment.sh rollback <build-name>" |
0 commit comments