|
| 1 | +name: Deploy |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - staging |
| 7 | + - master |
| 8 | + |
| 9 | +jobs: |
| 10 | + deploy-staging: |
| 11 | + name: Deploy to Staging |
| 12 | + runs-on: ubuntu-latest |
| 13 | + if: github.ref == 'refs/heads/staging' |
| 14 | + steps: |
| 15 | + - name: Deploy via SSH |
| 16 | + uses: appleboy/ssh-action@v1 |
| 17 | + with: |
| 18 | + host: ${{ secrets.VPS_HOST }} |
| 19 | + username: ${{ secrets.VPS_DEPLOY_USER }} |
| 20 | + key: ${{ secrets.VPS_SSH_KEY }} |
| 21 | + script: | |
| 22 | + set -e |
| 23 | + cd /opt/faculytics |
| 24 | +
|
| 25 | + echo "Fetching latest staging..." |
| 26 | + git fetch origin staging |
| 27 | +
|
| 28 | + echo "Building staging image..." |
| 29 | + mkdir -p /tmp/faculytics-build |
| 30 | + git archive origin/staging | tar -x -C /tmp/faculytics-build |
| 31 | + docker build -t faculytics-api:staging -f /tmp/faculytics-build/Dockerfile /tmp/faculytics-build |
| 32 | + rm -rf /tmp/faculytics-build |
| 33 | +
|
| 34 | + echo "Deploying staging..." |
| 35 | + docker compose -f docker-compose.deploy.yml --profile staging up -d api-staging |
| 36 | +
|
| 37 | + echo "Waiting for health check..." |
| 38 | + sleep 15 |
| 39 | + if docker compose -f docker-compose.deploy.yml --profile staging ps api-staging | grep -q "(healthy)"; then |
| 40 | + echo "Staging deploy successful!" |
| 41 | + else |
| 42 | + echo "Health check - checking API response..." |
| 43 | + docker exec $(docker compose -f docker-compose.deploy.yml ps -q api-staging) node -e "fetch('http://localhost:5201/api/v1/health').then(r=>r.json()).then(console.log).catch(e=>{console.error(e);process.exit(1)})" || true |
| 44 | + docker compose -f docker-compose.deploy.yml --profile staging logs --tail=50 api-staging |
| 45 | + echo "WARNING: Health check inconclusive - check logs above" |
| 46 | + fi |
| 47 | +
|
| 48 | + echo "Cleaning old images..." |
| 49 | + docker system prune -f --filter "until=168h" || true |
| 50 | +
|
| 51 | + - name: Discord Notification |
| 52 | + if: always() |
| 53 | + uses: sarisia/actions-status-discord@v1 |
| 54 | + with: |
| 55 | + webhook: ${{ secrets.DISCORD_WEBHOOK_URL }} |
| 56 | + title: 'Staging Deploy' |
| 57 | + description: '${{ job.status == ''success'' && ''Staging deployment succeeded'' || ''Staging deployment failed'' }}' |
| 58 | + color: '${{ job.status == ''success'' && ''0x00ff00'' || ''0xff0000'' }}' |
| 59 | + |
| 60 | + deploy-production: |
| 61 | + name: Deploy to Production |
| 62 | + runs-on: ubuntu-latest |
| 63 | + if: github.ref == 'refs/heads/master' |
| 64 | + steps: |
| 65 | + - name: Deploy via SSH |
| 66 | + uses: appleboy/ssh-action@v1 |
| 67 | + with: |
| 68 | + host: ${{ secrets.VPS_HOST }} |
| 69 | + username: ${{ secrets.VPS_DEPLOY_USER }} |
| 70 | + key: ${{ secrets.VPS_SSH_KEY }} |
| 71 | + script: | |
| 72 | + set -e |
| 73 | + cd /opt/faculytics |
| 74 | +
|
| 75 | + echo "Fetching latest master..." |
| 76 | + git fetch origin master |
| 77 | +
|
| 78 | + echo "Building production image..." |
| 79 | + mkdir -p /tmp/faculytics-build |
| 80 | + git archive origin/master | tar -x -C /tmp/faculytics-build |
| 81 | + docker build -t faculytics-api:production -f /tmp/faculytics-build/Dockerfile /tmp/faculytics-build |
| 82 | + rm -rf /tmp/faculytics-build |
| 83 | +
|
| 84 | + echo "Deploying production..." |
| 85 | + docker compose -f docker-compose.deploy.yml --profile production up -d api-production |
| 86 | +
|
| 87 | + echo "Waiting for health check..." |
| 88 | + sleep 15 |
| 89 | + if docker compose -f docker-compose.deploy.yml --profile staging ps api-production | grep -q "(healthy)"; then |
| 90 | + echo "Production deploy successful!" |
| 91 | + else |
| 92 | + echo "Health check - checking API response..." |
| 93 | + docker exec $(docker compose -f docker-compose.deploy.yml ps -q api-production) node -e "fetch('http://localhost:5200/api/v1/health').then(r=>r.json()).then(console.log).catch(e=>{console.error(e);process.exit(1)})" || true |
| 94 | + docker compose -f docker-compose.deploy.yml --profile production logs --tail=50 api-production |
| 95 | + echo "WARNING: Health check inconclusive - check logs above" |
| 96 | + fi |
| 97 | +
|
| 98 | + echo "Cleaning old images..." |
| 99 | + docker system prune -f --filter "until=168h" || true |
| 100 | +
|
| 101 | + - name: Discord Notification |
| 102 | + if: always() |
| 103 | + uses: sarisia/actions-status-discord@v1 |
| 104 | + with: |
| 105 | + webhook: ${{ secrets.DISCORD_WEBHOOK_URL }} |
| 106 | + title: 'Production Deploy' |
| 107 | + description: '${{ job.status == ''success'' && ''Production deployment succeeded'' || ''Production deployment failed'' }}' |
| 108 | + color: '${{ job.status == ''success'' && ''0x00ff00'' || ''0xff0000'' }}' |
0 commit comments