Repository navigation
126 lines (108 loc) · 4.71 KB
/
Copy pathdeploy.yml
File metadata and controls
126 lines (108 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
name: Deploy (Production)
on:
push:
branches: ["main"]
workflow_dispatch:
# Never run two deploys at once — do NOT cancel a running deploy
concurrency:
group: deploy-production
cancel-in-progress: false
permissions:
contents: read
jobs:
deploy:
name: SSH Deploy → Debian / PM2
runs-on: ubuntu-latest
timeout-minutes: 20
environment:
name: production
url: https://forms.msk-scripts.de
steps:
- name: Deploy over SSH
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.DEPLOY_SSH_HOST }}
username: ${{ secrets.DEPLOY_SSH_USER }}
key: ${{ secrets.DEPLOY_SSH_KEY }}
port: ${{ secrets.DEPLOY_SSH_PORT }}
script: |
set -euo pipefail
cd /opt/msk-forms
echo "-> Pull latest"
git fetch --all --prune
# Remember where we came from, so a failed release can be undone.
PREV_SHA="$(git rev-parse HEAD)"
echo "Previous release: ${PREV_SHA}"
git reset --hard origin/main
echo "-> Install dependencies (frozen)"
# Silenced & non-fatal: corepack can't replace a root-owned global
# pnpm (EACCES); the global pnpm works fine for the install.
corepack enable >/dev/null 2>&1 || true
pnpm install --frozen-lockfile --prod=false
echo "-> Prisma migrate (deploy) + generate"
pnpm prisma migrate deploy
pnpm prisma generate
echo "-> Build (keeping the previous bundle as a fallback)"
rm -rf apps/web/.next.prev
if [ -d apps/web/.next ]; then mv apps/web/.next apps/web/.next.prev; fi
pnpm run build
echo "-> Reload services via PM2 (zero-downtime)"
pm2 reload ecosystem.config.cjs --update-env
# PM2 reporting "online" only means the process exists, which is not
# the same as it serving. On 2026-08-17 a bundle missing part of
# @swc/helpers died on startup without ever binding the port: PM2
# showed online, this workflow reported success, and the site
# returned 503 for 40 minutes. Ask the port, not the supervisor.
serving() {
code=""
for i in $(seq 1 20); do
code="$(curl -s -o /dev/null -w "%{http_code}" --max-time 5 http://127.0.0.1:3008/ || true)"
if [ "$code" = "200" ]; then echo "Serving after ${i} attempt(s)."; return 0; fi
sleep 3
done
echo "Not serving on :3008 (last status: ${code:-none})"
return 1
}
echo "-> Health check"
if serving; then
rm -rf apps/web/.next.prev
echo "-> Save PM2 process list"
pm2 save
echo "Deploy finished"
exit 0
fi
# From here the new release is known bad. Put the previous one back
# rather than leaving the site down until someone reads the log.
#
# Note what this does NOT undo: `prisma migrate deploy` already ran
# and migrations are forward-only. That is tolerable because they are
# additive in practice (older code ignores columns it does not know),
# but a migration that drops or renames something still needs a human.
echo "::error::New release is not serving — rolling back to ${PREV_SHA}"
pm2 logs msk-forms-web --lines 40 --nostream --err || true
if [ ! -d apps/web/.next.prev ]; then
echo "::error::No previous bundle kept (first deploy?). Leaving the failed release in place."
exit 1
fi
git reset --hard "${PREV_SHA}"
pnpm install --frozen-lockfile --prod=false
pnpm prisma generate
rm -rf apps/web/.next
mv apps/web/.next.prev apps/web/.next
pm2 reload ecosystem.config.cjs --update-env
if serving; then
echo "::error::Rolled back to ${PREV_SHA}. The site is up on the previous release; the pushed commit is NOT live."
exit 1
fi
echo "::error::Rollback did not come up either — manual intervention required."
exit 1
- name: Notify Discord (optional)
if: always()
continue-on-error: true
run: |
STATUS="${{ job.status }}"
if [ -n "${{ secrets.DEPLOY_DISCORD_WEBHOOK }}" ]; then
curl -fsSL -H "Content-Type: application/json" \
-d "{\"content\":\"**MSK Forms** Deploy: \`$STATUS\` (commit ${GITHUB_SHA::7})\"}" \
"${{ secrets.DEPLOY_DISCORD_WEBHOOK }}"
fi