Repository navigation
133 lines (119 loc) · 4.9 KB
/
Copy pathdeploy-games-api.yml
File metadata and controls
133 lines (119 loc) · 4.9 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
127
128
129
130
131
132
133
name: Deploy Games API
on:
workflow_run:
workflows: ["Test"]
types: [completed]
branches: [main]
permissions:
contents: read
concurrency:
group: games-api-production-deploy
cancel-in-progress: false
jobs:
check:
name: Check tested revision is current
runs-on: ubuntu-latest
timeout-minutes: 5
if: >-
${{ github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.event == 'push' &&
github.event.workflow_run.head_repository.id == github.event.repository.id &&
github.event.workflow_run.head_branch == 'main' }}
outputs:
current: ${{ steps.revision.outputs.current }}
steps:
# A newer main commit supersedes this one and gets its own deploy, so
# skip this deploy instead of failing it.
- name: Compare tested revision with main
id: revision
env:
GH_TOKEN: ${{ github.token }}
EXPECTED_SHA: ${{ github.event.workflow_run.head_sha }}
run: |
current_sha="$(gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/main" --jq '.object.sha')"
if [[ "$current_sha" == "$EXPECTED_SHA" ]]; then
echo 'current=true' >> "$GITHUB_OUTPUT"
else
echo "::notice::Main advanced to $current_sha; skipping deployment of superseded $EXPECTED_SHA."
echo 'current=false' >> "$GITHUB_OUTPUT"
fi
deploy:
name: Deploy Games API
runs-on: ubuntu-latest
timeout-minutes: 15
environment: production
needs: check
if: ${{ needs.check.outputs.current == 'true' }}
# The Worker runs on the Sentry Internal Cloudflare account, not on the
# account that CLOUDFLARE_ACCOUNT_ID names for the MCP Workers. Only the
# steps that use the credentials get them, so install scripts cannot read
# them.
steps:
- name: Require Cloudflare credentials
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.GAMES_API_CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.GAMES_API_CLOUDFLARE_ACCOUNT_ID }}
run: |
if [[ -z "$CLOUDFLARE_API_TOKEN" || -z "$CLOUDFLARE_ACCOUNT_ID" ]]; then
echo 'GAMES_API_CLOUDFLARE_API_TOKEN and GAMES_API_CLOUDFLARE_ACCOUNT_ID must be set in the production environment.' >&2
exit 1
fi
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
ref: ${{ github.event.workflow_run.head_sha }}
persist-credentials: false
- name: Setup Node.js
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
with:
node-version: "22"
# pnpm/action-setup@v6.1.1
- uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115
name: Install pnpm
with:
run_install: false
- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> "$GITHUB_ENV"
- uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
name: Setup pnpm cache
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Require tested revision on main before deployment
env:
GH_TOKEN: ${{ github.token }}
EXPECTED_SHA: ${{ github.event.workflow_run.head_sha }}
run: |
current_sha="$(gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/main" --jq '.object.sha')"
if [[ "$current_sha" != "$EXPECTED_SHA" ]]; then
echo 'Main advanced before deployment; refusing deployment.' >&2
exit 1
fi
- name: Deploy Games API
working-directory: apps/games-api
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.GAMES_API_CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.GAMES_API_CLOUDFLARE_ACCOUNT_ID }}
DEPLOYMENT_MARKER: games-api:${{ github.run_id }}:${{ github.run_attempt }}:${{ github.event.workflow_run.head_sha }}
run: pnpm exec wrangler deploy --minify --keep-vars --message "$DEPLOYMENT_MARKER"
- name: Smoke-test the leaderboard
env:
TESTED_SHA: ${{ github.event.workflow_run.head_sha }}
run: |
for attempt in 1 2 3 4 5 6; do
status="$(curl -sS -o /dev/null -w '%{http_code}' https://games.sentry.new/v1/snake/leaderboard || true)"
if [[ "$status" == "200" ]]; then
echo "## Games API deployment" >> "$GITHUB_STEP_SUMMARY"
echo "Source: $TESTED_SHA" >> "$GITHUB_STEP_SUMMARY"
exit 0
fi
echo "Attempt $attempt: HTTP $status"
sleep 10
done
echo 'The leaderboard did not return 200 after deployment.' >&2
exit 1