-
Notifications
You must be signed in to change notification settings - Fork 3
495 lines (473 loc) · 19.2 KB
/
Copy pathdeploy.yml
File metadata and controls
495 lines (473 loc) · 19.2 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
name: Protected release
run-name: >-
${{ inputs.deployment_environment }} ${{ inputs.release_plan }} from
${{ inputs.source_commit }}
on:
workflow_dispatch:
inputs:
source_commit:
description: Full 40-character commit at the current tip of main
required: true
type: string
deployment_environment:
description: Protected release environment
required: true
type: choice
options:
- staging
- production
release_plan:
description: Fixed reviewed backend set; add another plan by pull request
required: true
type: choice
options:
- profile-recovery
permissions: {}
concurrency:
group: release-${{ inputs.deployment_environment }}
cancel-in-progress: false
jobs:
preflight:
name: Validate exact current-main release
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
actions: read
contents: read
outputs:
ci_run_id: ${{ steps.validate.outputs.ci_run_id }}
source_commit: ${{ steps.validate.outputs.source_commit }}
steps:
- name: Check out canonical main history
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
with:
fetch-depth: 0
persist-credentials: false
ref: main
- id: validate
name: Require current main and its successful push CI
env:
GH_TOKEN: ${{ github.token }}
SOURCE_COMMIT: ${{ inputs.source_commit }}
WORKFLOW_REF: ${{ github.ref }}
run: |
set -euo pipefail
if [[ "$WORKFLOW_REF" != "refs/heads/main" ]]; then
echo "Release workflow must run from main." >&2
exit 1
fi
if [[ ! "$SOURCE_COMMIT" =~ ^[0-9a-f]{40}$ ]]; then
echo "Source commit must be a full lowercase Git SHA." >&2
exit 1
fi
git fetch --no-tags origin refs/heads/main
current_main="$(git rev-parse origin/main)"
if [[ "$SOURCE_COMMIT" != "$current_main" ]]; then
echo "Source commit must equal the current tip of main." >&2
exit 1
fi
if [[ "$(git rev-parse HEAD)" != "$SOURCE_COMMIT" ]]; then
echo "Checked-out source does not match the approved commit." >&2
exit 1
fi
runs_file="$(mktemp)"
jobs_file="$(mktemp)"
trap 'rm -f "$runs_file" "$jobs_file"' EXIT
gh api --method GET \
"repos/${GITHUB_REPOSITORY}/actions/workflows/ci.yml/runs" \
-f head_sha="$SOURCE_COMMIT" \
-f branch=main \
-f event=push \
-f per_page=100 > "$runs_file"
ci_run_id="$(jq -er --arg sha "$SOURCE_COMMIT" '
.workflow_runs
| map(select(
.head_sha == $sha
and .head_branch == "main"
and .event == "push"
))
| sort_by([.run_number, (.run_attempt // 0)])
| last
| select(.status == "completed" and .conclusion == "success")
| .id
' "$runs_file")" || {
echo "The latest exact main-push CI run is not successful." >&2
exit 1
}
gh api --method GET \
"repos/${GITHUB_REPOSITORY}/actions/runs/${ci_run_id}/jobs" \
-f filter=latest \
-f per_page=100 > "$jobs_file"
for required_job in \
"Frontend lint + build" \
"Cloud Functions lint + test" \
"Commerce command journal emulator" \
"Test artifact scrubber" \
"Firestore security-rules tests"
do
if ! jq -e --arg name "$required_job" '
[.jobs[] | select(
.name == $name
and .status == "completed"
and .conclusion == "success"
)] | length == 1
' "$jobs_file" >/dev/null
then
echo "Required CI job is not exactly one successful result: $required_job" >&2
exit 1
fi
done
echo "ci_run_id=$ci_run_id" >> "$GITHUB_OUTPUT"
echo "source_commit=$SOURCE_COMMIT" >> "$GITHUB_OUTPUT"
echo "Preflight passed for current main and its exact push CI run."
prepare-pages:
name: Build immutable website artifact without cloud authority
if: ${{ inputs.deployment_environment == 'production' }}
needs: preflight
runs-on: ubuntu-latest
timeout-minutes: 20
permissions:
contents: read
steps:
- name: Validate required public build configuration
env:
PUBLIC_RECAPTCHA_SITE_KEY: ${{ vars.REACT_APP_RECAPTCHA_SITE_KEY }}
run: |
set -euo pipefail
if [[ -z "$PUBLIC_RECAPTCHA_SITE_KEY" ]]; then
echo "Required public App Check site key is missing." >&2
exit 1
fi
echo "Required public website configuration is present."
- name: Check out the approved current-main commit
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
with:
ref: ${{ needs.preflight.outputs.source_commit }}
persist-credentials: false
- name: Confirm website source commit
env:
SOURCE_COMMIT: ${{ needs.preflight.outputs.source_commit }}
run: |
set -euo pipefail
[[ "$(git rev-parse HEAD)" == "$SOURCE_COMMIT" ]] || {
echo "Website source does not match the approved commit." >&2
exit 1
}
- name: Set up locked Node runtime
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6
with:
node-version: '20'
cache: npm
- name: Install committed website dependencies
run: npm ci --legacy-peer-deps
- name: Build public website artifact
env:
CI: 'true'
DISABLE_ESLINT_PLUGIN: 'true'
SITE_ORIGIN: 'https://runmprc.com'
REACT_APP_RECAPTCHA_SITE_KEY: ${{ vars.REACT_APP_RECAPTCHA_SITE_KEY }}
REACT_APP_SENTRY_DSN: ${{ vars.REACT_APP_SENTRY_DSN }}
SOURCE_COMMIT: ${{ needs.preflight.outputs.source_commit }}
run: |
set -euo pipefail
npm run build
printf '%s\n' "$SOURCE_COMMIT" > build/release-commit.txt
- name: Reject server-credential material in the website artifact
run: |
set -euo pipefail
if grep -R -q -E \
'FIREBASE_SERVICE_ACCOUNT|FIREBASE_TOKEN|GOOGLE_APPLICATION_CREDENTIALS|BEGIN (RSA )?PRIVATE KEY' \
build
then
echo "Server credential material was found in the website artifact." >&2
exit 1
fi
- name: Retain the exact prebuilt Pages artifact
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
with:
name: pages-${{ needs.preflight.outputs.source_commit }}
path: build
if-no-files-found: error
retention-days: 30
deploy-backend:
name: Deploy and verify fixed Firebase backend
if: ${{ always() && needs.preflight.result == 'success' && (inputs.deployment_environment == 'staging' || needs.prepare-pages.result == 'success') }}
needs:
- preflight
- prepare-pages
runs-on: ubuntu-latest
timeout-minutes: 40
environment:
name: ${{ inputs.deployment_environment }}
permissions:
actions: read
contents: read
id-token: write
env:
DEPLOYMENT_ENVIRONMENT: ${{ inputs.deployment_environment }}
RELEASE_PLAN: ${{ inputs.release_plan }}
TARGET_PROJECT_ID: ${{ vars.FIREBASE_PROJECT_ID }}
outputs:
backend_verified: ${{ steps.verify.outputs.backend_verified }}
steps:
- name: Validate protected environment mapping
env:
DEPLOY_SERVICE_ACCOUNT: ${{ secrets.GCP_DEPLOY_SERVICE_ACCOUNT }}
WORKLOAD_IDENTITY_PROVIDER: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}
run: |
set -euo pipefail
if [[ "$DEPLOYMENT_ENVIRONMENT" == "staging" ]]; then
echo "Staging is unavailable until #113/#133 add one exact approved project." >&2
exit 1
fi
if [[ "$DEPLOYMENT_ENVIRONMENT" != "production" ]]; then
echo "Unsupported release environment." >&2
exit 1
fi
if [[ "$RELEASE_PLAN" != "profile-recovery" ]]; then
echo "Unsupported release plan." >&2
exit 1
fi
if [[ "$TARGET_PROJECT_ID" != "mid-peninsula-running-club" ]]; then
echo "Production project does not match the reviewed target." >&2
exit 1
fi
if [[ -z "$WORKLOAD_IDENTITY_PROVIDER" || -z "$DEPLOY_SERVICE_ACCOUNT" ]]; then
echo "Required protected cloud authority is missing." >&2
exit 1
fi
if [[ ! "$WORKLOAD_IDENTITY_PROVIDER" =~ ^projects/[0-9]+/locations/global/workloadIdentityPools/[A-Za-z0-9._-]+/providers/[A-Za-z0-9._-]+$ ]]; then
echo "Workload Identity provider configuration is invalid." >&2
exit 1
fi
if [[ ! "$DEPLOY_SERVICE_ACCOUNT" =~ ^[A-Za-z0-9._+-]+@[A-Za-z0-9.-]+\.iam\.gserviceaccount\.com$ ]]; then
echo "Deploy service account configuration is invalid." >&2
exit 1
fi
echo "Protected production mapping and authority are present."
- name: Check out the approved current-main commit
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
with:
ref: ${{ needs.preflight.outputs.source_commit }}
persist-credentials: false
- name: Confirm fixed source and target set
env:
SOURCE_COMMIT: ${{ needs.preflight.outputs.source_commit }}
run: |
set -euo pipefail
[[ "$(git rev-parse HEAD)" == "$SOURCE_COMMIT" ]] || {
echo "Backend source does not match the approved commit." >&2
exit 1
}
grep -q 'exports.createMemberOnSignUp' functions/index.js
grep -q 'exports.ensureMemberProfile' functions/index.js
test -s firestore.rules
echo "Exact source commit and fixed release resources are present."
- name: Set up locked Node runtime
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6
with:
node-version: '20'
cache: npm
cache-dependency-path: |
package-lock.json
functions/package-lock.json
- name: Install committed deploy dependencies without lifecycle scripts
run: |
set -euo pipefail
npm ci --legacy-peer-deps --ignore-scripts
npm --prefix functions ci --ignore-scripts
- name: Verify the lockfile Firebase CLI before cloud authentication
run: |
set -euo pipefail
expected_version="$(node -p "require('./package-lock.json').packages['node_modules/firebase-tools'].version")"
actual_version="$(npx --no-install firebase --version)"
if [[ "$actual_version" != "$expected_version" ]]; then
echo "Firebase CLI does not match package-lock.json." >&2
exit 1
fi
echo "Using the Firebase CLI version committed in package-lock.json."
- name: Confirm the prebuilt Pages artifact is still available after approval
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
with:
name: pages-${{ needs.preflight.outputs.source_commit }}
path: ${{ runner.temp }}/prebuilt-pages
- name: Recheck the prebuilt artifact before backend mutation
env:
SOURCE_COMMIT: ${{ needs.preflight.outputs.source_commit }}
run: |
set -euo pipefail
artifact="$RUNNER_TEMP/prebuilt-pages"
[[ "$(cat "$artifact/release-commit.txt")" == "$SOURCE_COMMIT" ]] || {
echo "Prebuilt website artifact no longer matches the approved commit." >&2
exit 1
}
if grep -R -q -E \
'FIREBASE_SERVICE_ACCOUNT|FIREBASE_TOKEN|GOOGLE_APPLICATION_CREDENTIALS|BEGIN (RSA )?PRIVATE KEY' \
"$artifact"
then
echo "Server credential material was found in the website artifact." >&2
exit 1
fi
echo "The exact credential-free website artifact remains available."
- name: Revalidate current main and exact CI after protected approval
env:
CI_RUN_ID: ${{ needs.preflight.outputs.ci_run_id }}
GH_TOKEN: ${{ github.token }}
RELEASE_RUN_ID: ${{ github.run_id }}
SOURCE_COMMIT: ${{ needs.preflight.outputs.source_commit }}
run: |
set -euo pipefail
git fetch --no-tags origin refs/heads/main
[[ "$(git rev-parse origin/main)" == "$SOURCE_COMMIT" ]] || {
echo "Main advanced after this release was requested; request a new release." >&2
exit 1
}
[[ "$(git rev-parse HEAD)" == "$SOURCE_COMMIT" ]] || {
echo "Backend source no longer matches the approved commit." >&2
exit 1
}
runs_file="$(mktemp)"
jobs_file="$(mktemp)"
release_file="$(mktemp)"
trap 'rm -f "$runs_file" "$jobs_file" "$release_file"' EXIT
gh api --method GET \
"repos/${GITHUB_REPOSITORY}/actions/runs/${RELEASE_RUN_ID}" \
> "$release_file"
release_created_at="$(jq -er '.created_at' "$release_file")"
release_created_epoch="$(date -u -d "$release_created_at" +%s)"
current_epoch="$(date -u +%s)"
if (( current_epoch - release_created_epoch > 86400 )); then
echo "Release request is older than 24 hours; request a new release." >&2
exit 1
fi
gh api --method GET \
"repos/${GITHUB_REPOSITORY}/actions/workflows/ci.yml/runs" \
-f head_sha="$SOURCE_COMMIT" \
-f branch=main \
-f event=push \
-f per_page=100 > "$runs_file"
validated_ci_run_id="$(jq -er --arg sha "$SOURCE_COMMIT" '
.workflow_runs
| map(select(
.head_sha == $sha
and .head_branch == "main"
and .event == "push"
))
| sort_by([.run_number, (.run_attempt // 0)])
| last
| select(.status == "completed" and .conclusion == "success")
| .id
' "$runs_file")" || {
echo "The newest exact main-push CI run is no longer successful." >&2
exit 1
}
if [[ "$validated_ci_run_id" != "$CI_RUN_ID" ]]; then
echo "The approved CI run is no longer the newest exact run." >&2
exit 1
fi
gh api --method GET \
"repos/${GITHUB_REPOSITORY}/actions/runs/${validated_ci_run_id}/jobs" \
-f filter=latest \
-f per_page=100 > "$jobs_file"
for required_job in \
"Frontend lint + build" \
"Cloud Functions lint + test" \
"Commerce command journal emulator" \
"Test artifact scrubber" \
"Firestore security-rules tests"
do
if ! jq -e --arg name "$required_job" '
[.jobs[] | select(
.name == $name
and .status == "completed"
and .conclusion == "success"
)] | length == 1
' "$jobs_file" >/dev/null
then
echo "Required CI job is no longer successful: $required_job" >&2
exit 1
fi
done
echo "Current main and its exact CI remain approved after the wait."
- id: auth
name: Obtain short-lived Google Cloud credentials
uses: google-github-actions/auth@7c6bc770dae815cd3e89ee6cdf493a5fab2cc093 # v3
with:
project_id: ${{ vars.FIREBASE_PROJECT_ID }}
workload_identity_provider: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ secrets.GCP_DEPLOY_SERVICE_ACCOUNT }}
create_credentials_file: true
cleanup_credentials: true
export_environment_variables: true
token_format: access_token
access_token_lifetime: 3300s
request_reason: mprc-release-${{ needs.preflight.outputs.source_commit }}
- id: capture
name: Capture private provider state before deployment
env:
CLOUD_ACCESS_TOKEN: ${{ steps.auth.outputs.access_token }}
run: >-
node .github/scripts/verify-firebase-release.mjs capture
--project "$TARGET_PROJECT_ID"
--output "$RUNNER_TEMP/firebase-before.json"
- id: deploy
name: Deploy the one fixed backend set
run: >-
timeout --signal=TERM --kill-after=30s 20m
npx --no-install firebase deploy
--only firestore:rules,functions:createMemberOnSignUp,functions:ensureMemberProfile
--project "$TARGET_PROJECT_ID"
--non-interactive
- id: verify
name: Always read back Rules and both Function revisions
if: ${{ always() && steps.auth.outcome == 'success' && steps.capture.outcome == 'success' }}
env:
CLOUD_ACCESS_TOKEN: ${{ steps.auth.outputs.access_token }}
DEPLOY_SUCCEEDED: ${{ steps.deploy.outcome == 'success' }}
run: |
set -euo pipefail
node .github/scripts/verify-firebase-release.mjs verify \
--project "$TARGET_PROJECT_ID" \
--before "$RUNNER_TEMP/firebase-before.json" \
--rules firestore.rules \
--deploy-succeeded "$DEPLOY_SUCCEEDED"
echo "backend_verified=true" >> "$GITHUB_OUTPUT"
deploy-pages:
name: Publish the prebuilt GitHub Pages copy
if: ${{ inputs.deployment_environment == 'production' && needs.deploy-backend.result == 'success' && needs.deploy-backend.outputs.backend_verified == 'true' }}
needs:
- preflight
- prepare-pages
- deploy-backend
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: write
steps:
- name: Download the exact prebuilt website artifact
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
with:
name: pages-${{ needs.preflight.outputs.source_commit }}
path: build
- name: Confirm artifact commit and credential separation
env:
SOURCE_COMMIT: ${{ needs.preflight.outputs.source_commit }}
run: |
set -euo pipefail
[[ "$(cat build/release-commit.txt)" == "$SOURCE_COMMIT" ]] || {
echo "Website artifact does not match the approved commit." >&2
exit 1
}
if grep -R -q -E \
'FIREBASE_SERVICE_ACCOUNT|FIREBASE_TOKEN|GOOGLE_APPLICATION_CREDENTIALS|BEGIN (RSA )?PRIVATE KEY' \
build
then
echo "Server credential material was found in the website artifact." >&2
exit 1
fi
echo "The prebuilt website artifact matches the verified backend commit."
- name: Publish GitHub Pages copy
uses: peaceiris/actions-gh-pages@84c30a85c19949d7eee79c4ff27748b70285e453 # v4
with:
github_token: ${{ github.token }}
publish_dir: ./build