Repository navigation
1063 lines (1036 loc) · 49.7 KB
/
Copy pathpush.yml
File metadata and controls
1063 lines (1036 loc) · 49.7 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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
name: Push
on:
push:
branches:
- master
env:
ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION: true
OLD_SIMULATION_GATEWAY_URL: ${{ vars.OLD_SIMULATION_GATEWAY_URL }}
concurrency:
group: deploy
cancel-in-progress: false
jobs:
check-release-commit:
# Whether this release run may skip the checks its merge run passed: the
# release commit changes only release files, and the merge commit's Push
# run passed every check (.github/release_commit.py). It runs on every
# push, so the checks that need it never need a skipped job; on a merge
# commit its steps do nothing, and the checks run.
name: Check the release commit
runs-on: ubuntu-latest
if: github.repository == 'PolicyEngine/policyengine-api'
permissions:
contents: read
actions: read
outputs:
merge_run_checks_passed: ${{ steps.release_commit.outputs.merge_run_checks_passed }}
merge_run_url: ${{ steps.release_commit.outputs.merge_run_url }}
steps:
- name: Checkout the release commit and its parent
if: github.event.head_commit.message == 'Update PolicyEngine API'
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Setup Python
if: github.event.head_commit.message == 'Update PolicyEngine API'
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Decide whether this run may skip the merge run's checks
id: release_commit
if: github.event.head_commit.message == 'Update PolicyEngine API'
run: bash .github/scripts/check_release_commit.sh
env:
GH_TOKEN: ${{ github.token }}
lint:
name: Lint
runs-on: ubuntu-latest
needs: check-release-commit
# Skipped on a release run whose merge run passed it on the same code.
# !cancelled() runs it when the release commit check fails.
if: |
!cancelled()
&& (github.repository == 'PolicyEngine/policyengine-api')
&& (needs.check-release-commit.outputs.merge_run_checks_passed != 'true')
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install ruff (version pinned in uv.lock)
run: bash .github/scripts/install_locked_ruff.sh
- name: Format check with ruff
run: ruff format --check .
- name: Lint with ruff
run: ruff check .
quality-guards:
name: Quality guards
runs-on: ubuntu-latest
needs: check-release-commit
# Skipped on a release run whose merge run passed it on the same code.
# !cancelled() runs it when the release commit check fails.
if: |
!cancelled()
&& (github.repository == 'PolicyEngine/policyengine-api')
&& (needs.check-release-commit.outputs.merge_run_checks_passed != 'true')
env:
# uv installs uv.lock on the Cloud Run image's Python, not the 3.14
# that .python-version selects.
UV_PYTHON: "3.12"
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
# The same uv version as the Cloud Run image and the pull request job.
- name: Set up uv
uses: astral-sh/setup-uv@v6
with:
version: "0.13.4"
# The declared-dependencies guard runs deptry in this environment.
- name: Install locked dependencies
run: uv sync --frozen --extra dev
- name: Install shellcheck (for the ci-scripts guard)
run: pip install "shellcheck-py==0.11.0.1"
- name: Run quality guards
run: python scripts/run_quality_guards.py
alembic-v1-check:
name: Alembic v1 qualification
needs: check-release-commit
# Skipped on a release run whose merge run passed it on the same code.
# !cancelled() runs it when the release commit check fails.
if: |
!cancelled()
&& (github.repository == 'PolicyEngine/policyengine-api')
&& (needs.check-release-commit.outputs.merge_run_checks_passed != 'true')
uses: ./.github/workflows/alembic-v1-check.yml
alembic-v2-check:
name: Alembic v2 qualification
needs: check-release-commit
# Skipped on a release run whose merge run passed it on the same code.
# !cancelled() runs it when the release commit check fails.
if: |
!cancelled()
&& (github.repository == 'PolicyEngine/policyengine-api')
&& (needs.check-release-commit.outputs.merge_run_checks_passed != 'true')
uses: ./.github/workflows/alembic-v2-check.yml
v2-integration-check:
name: Cross-database integration tests
needs: check-release-commit
# Skipped on a release run whose merge run passed it on the same code.
# !cancelled() runs it when the release commit check fails.
if: |
!cancelled()
&& (github.repository == 'PolicyEngine/policyengine-api')
&& (needs.check-release-commit.outputs.merge_run_checks_passed != 'true')
uses: ./.github/workflows/v2-integration-check.yml
ensure-staging-model-version-aligns-with-sim-api:
name: Ensure staging model version aligns with simulation API
runs-on: ubuntu-latest
if: |
(github.repository == 'PolicyEngine/policyengine-api')
&& (github.event.head_commit.message == 'Update PolicyEngine API')
environment: staging
env:
DEPLOYMENT_ENVIRONMENT: staging
SIM_ENTRYPOINT: ${{ vars.SIM_ENTRYPOINT }}
SIMULATION_ENTRYPOINT_URL: ${{ secrets.SIMULATION_ENTRYPOINT_URL }}
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Check the simulation service belongs to staging
run: bash .github/scripts/check_simulation_entrypoint_environment.sh
- name: Install jq
run: sudo apt-get install -y jq
- name: Set up uv for installed bundle validation
uses: astral-sh/setup-uv@v6
- name: Check the PolicyEngine bundle is the simulation API's latest
run: bash .github/check-policyengine-bundle-supported.sh
versioning:
name: Update versioning
needs:
- lint
- alembic-v1-check
- alembic-v2-check
- v2-integration-check
if: |
(github.repository == 'PolicyEngine/policyengine-api')
&& !(github.event.head_commit.message == 'Update PolicyEngine API')
runs-on: ubuntu-latest
env:
# uv installs uv.lock on the Cloud Run image's Python, not the 3.14
# that .python-version selects.
UV_PYTHON: "3.12"
steps:
- name: Generate GitHub App token
id: app-token
uses: actions/create-github-app-token@v1
with:
app-id: ${{ secrets.APP_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
- name: Checkout repo
uses: actions/checkout@v4
with:
token: ${{ steps.app-token.outputs.token }}
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Setup uv
uses: astral-sh/setup-uv@v6
with:
version: "0.13.4"
- name: Bump project version
run: python .github/bump_version.py
- name: Refresh uv lock
run: uv lock
- name: Build changelog
run: bash .github/scripts/build_changelog.sh
- name: Preview changelog update
run: ".github/get-changelog-diff.sh"
- name: Update changelog
uses: EndBug/add-and-commit@v9
with:
add: "."
committer_name: Github Actions[bot]
author_name: Github Actions[bot]
message: Update PolicyEngine API
publish-git-tag:
name: Publish Git Tag
runs-on: ubuntu-latest
needs:
- ensure-staging-model-version-aligns-with-sim-api
- check-release-commit
- lint
- alembic-v1-check
- alembic-v2-check
- v2-integration-check
# The checks may be skipped, so this job runs whatever they ended, and its
# first step after checkout requires that the merge run passed them on the
# same code or that this run did. Every release job after it states which
# jobs it needs to have passed: GitHub would otherwise skip it, because a
# job it needs needed a skipped one.
if: |
!cancelled()
&& (github.repository == 'PolicyEngine/policyengine-api')
&& (github.event.head_commit.message == 'Update PolicyEngine API')
&& needs.ensure-staging-model-version-aligns-with-sim-api.result == 'success'
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Require the merge run's checks or this run's
run: python .github/release_commit.py require-checks
env:
JOB_RESULTS: ${{ toJSON(needs) }}
- name: Publish Git Tag
run: bash .github/scripts/publish_git_tag.sh
migrate-v1-staging-cloud-sql:
name: Upgrade staging v1 Cloud SQL schema
needs:
- ensure-staging-model-version-aligns-with-sim-api
- publish-git-tag
if: |
!cancelled()
&& (github.repository == 'PolicyEngine/policyengine-api')
&& (github.event.head_commit.message == 'Update PolicyEngine API')
&& needs.ensure-staging-model-version-aligns-with-sim-api.result == 'success'
&& needs.publish-git-tag.result == 'success'
uses: ./.github/workflows/migrate-v1-cloud-sql.yml
with:
deployment_environment: staging
github_environment: staging-database
secrets: inherit
seed-v2-staging-database:
# Runs beside migrate-v1-staging-cloud-sql: the seeding job connects only to
# the v2 database and the v1 upgrade only to Cloud SQL, so neither needs the
# other. The staging deploy needs both.
name: Seed staging v2 database
needs:
- publish-git-tag
if: |
!cancelled()
&& (github.repository == 'PolicyEngine/policyengine-api')
&& (github.event.head_commit.message == 'Update PolicyEngine API')
&& needs.publish-git-tag.result == 'success'
uses: ./.github/workflows/seed-v2-database.yml
with:
deployment_environment: staging
secrets: inherit
release-tests:
# The release test suite (make test) on the release commit: the only run
# of it on the merged code, because pull requests need not be up to date
# with master to merge. It starts with the run, beside the checks and the
# staging database jobs, and the staging deploy needs it, so the tests pass
# before GCP authentication, image publication or candidate deployment.
# It keeps the staging environment and the variables the tests read when
# they ran inside the staging deploy job. The suite runs in six shards, one
# runner each, split by file (scripts/release_test_shards.py), and
# check-release-test-shards checks that each test ran in exactly one.
name: Run release tests (shard ${{ matrix.shard_index }} of 6)
runs-on: ubuntu-latest
if: |
(github.repository == 'PolicyEngine/policyengine-api')
&& (github.event.head_commit.message == 'Update PolicyEngine API')
environment: staging
env:
SIM_ENTRYPOINT: ${{ vars.SIM_ENTRYPOINT }}
SIMULATION_ENTRYPOINT_URL: ${{ secrets.SIMULATION_ENTRYPOINT_URL }}
POLICYENGINE_DB_INSTANCE_CONNECTION_NAME: ${{ vars.POLICYENGINE_DB_INSTANCE_CONNECTION_NAME }}
PRODUCTION_CLOUD_RUN_POLICYENGINE_DB_PASSWORD_SECRET: ${{ vars.PRODUCTION_CLOUD_RUN_POLICYENGINE_DB_PASSWORD_SECRET }}
# uv installs uv.lock on the Cloud Run image's Python, not the 3.14
# that .python-version selects.
UV_PYTHON: "3.12"
permissions:
contents: read
strategy:
# Every shard runs to the end, so one release run shows every failure.
fail-fast: false
matrix:
shard_index: [1, 2, 3, 4, 5, 6]
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Setup uv
uses: astral-sh/setup-uv@v6
- name: Update package lists
run: sudo apt-get update
- name: Install release test dependencies
run: sudo apt-get install -y redis-server
- name: Start release test Redis
run: sudo systemctl start redis-server
- name: Install locked application dependencies
run: make install
- name: Run release tests
run: bash .github/scripts/run_release_tests.sh
env:
RELEASE_TEST_SHARD_INDEX: ${{ matrix.shard_index }}
RELEASE_TEST_SHARD_COUNT: "6"
RELEASE_TEST_SHARD_REPORT_DIRECTORY: ${{ runner.temp }}/release-test-shard
RUNTIME_CACHE_MODE: local
RUNTIME_CACHE_URL: redis://127.0.0.1:6379/0
RUNTIME_CACHE_ENVIRONMENT: test
RUNTIME_CACHE_SERVICE: api
POLICYENGINE_DB_PASSWORD: ${{ secrets.POLICYENGINE_DB_PASSWORD }}
POLICYENGINE_GITHUB_MICRODATA_AUTH_TOKEN: ${{ secrets.POLICYENGINE_GITHUB_MICRODATA_AUTH_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
# Compatibility name required by PolicyEngine Core.
HUGGING_FACE_TOKEN: ${{ secrets.PE_UK_PRIVATE_HF_READ_TOKEN }}
GATEWAY_AUTH_ISSUER: ${{ secrets.GATEWAY_AUTH_ISSUER }}
GATEWAY_AUTH_AUDIENCE: ${{ secrets.GATEWAY_AUTH_AUDIENCE }}
GATEWAY_AUTH_CLIENT_ID: ${{ secrets.GATEWAY_AUTH_CLIENT_ID }}
GATEWAY_AUTH_CLIENT_SECRET_RESOURCE: ${{ secrets.GATEWAY_AUTH_CLIENT_SECRET_RESOURCE }}
- name: Upload the tests this shard collected and ran
uses: actions/upload-artifact@v4
with:
name: release-test-shard-${{ matrix.shard_index }}
path: ${{ runner.temp }}/release-test-shard
if-no-files-found: error
retention-days: 7
# upload-release-test-coverage sends every shard's report to Codecov at
# once.
- name: Keep this shard's coverage report
uses: actions/upload-artifact@v4
with:
name: release-test-coverage-shard-${{ matrix.shard_index }}
path: coverage.xml
if-no-files-found: error
retention-days: 7
upload-release-test-coverage:
# Sends the six release-test shards' coverage reports to Codecov in one
# upload, flagged unit, as pr.yml's upload-test-coverage does for pull
# requests. The release commit is the only master commit whose unit tests
# run, so this measures the merged code; codecov.yml carries the
# integration flag forward from the merge commit. A failed upload fails
# the run. The staging deploy doesn't wait for it.
name: Upload the release test coverage to Codecov
runs-on: ubuntu-latest
# The Codecov action sets no network timeouts, so a stalled upload would
# otherwise run until GitHub's 360-minute default.
timeout-minutes: 10
needs: release-tests
permissions:
contents: read
steps:
- name: Checkout the release commit and its parent
uses: actions/checkout@v4
with:
fetch-depth: 2
# A pipeline, so that the shell's pipefail fails the step if rev-parse
# fails, instead of uploading for an empty commit.
- name: Find the merge commit the release commit follows
id: merge_commit
run: git rev-parse --verify HEAD^ | sed 's/^/sha=/' >> "$GITHUB_OUTPUT"
- name: Download every shard's coverage report
uses: actions/download-artifact@v4
with:
pattern: release-test-coverage-shard-*
path: ${{ runner.temp }}/release-test-coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: PolicyEngine/policyengine-api
directory: ${{ runner.temp }}/release-test-coverage
# codecov.yml defines the flag and carries it forward.
flags: unit
fail_ci_if_error: true
# versioning's release commit changes only the version, uv.lock and the
# changelog, so these tests also measure the merge commit's code. Pull
# requests that branch from the merge commit, before its release commit
# lands, then compare with unit coverage measured on that code instead
# of coverage carried forward from an earlier commit.
- name: Upload the same coverage for the merge commit
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: PolicyEngine/policyengine-api
directory: ${{ runner.temp }}/release-test-coverage
flags: unit
fail_ci_if_error: true
override_commit: ${{ steps.merge_commit.outputs.sha }}
upload-integration-coverage:
# Sends the cross-database integration tests' coverage report, which
# v2-integration-check.yml keeps as an artifact, to Codecov flagged
# integration. No job waits for it, so a Codecov failure
# can't stop a release; a failed upload fails the run.
name: Upload the integration test coverage to Codecov
runs-on: ubuntu-latest
# The Codecov action sets no network timeouts, so a stalled upload would
# otherwise run until GitHub's 360-minute default.
timeout-minutes: 10
needs: v2-integration-check
permissions:
contents: read
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Download the integration coverage report
uses: actions/download-artifact@v4
with:
name: integration-coverage
path: ${{ runner.temp }}/integration-coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: PolicyEngine/policyengine-api
directory: ${{ runner.temp }}/integration-coverage
# codecov.yml defines the flag and carries it forward.
flags: integration
fail_ci_if_error: true
check-release-test-shards:
# Fails unless every shard collected the same tests and each test ran in
# exactly one shard (scripts/release_test_shards.py check-coverage).
name: Check that every release test ran in exactly one shard
runs-on: ubuntu-latest
needs: release-tests
if: |
!cancelled()
&& (github.repository == 'PolicyEngine/policyengine-api')
&& (github.event.head_commit.message == 'Update PolicyEngine API')
&& needs.release-tests.result == 'success'
permissions:
contents: read
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Download every shard's report
uses: actions/download-artifact@v4
with:
pattern: release-test-shard-*
path: ${{ runner.temp }}/release-test-shards
- name: Check that every release test ran in exactly one shard
run: python -m scripts.release_test_shards check-coverage --shard-count 6 "${{ runner.temp }}/release-test-shards"
deploy-cloud-run-staging:
name: Deploy and test staging Cloud Run candidate
runs-on: ubuntu-latest
needs:
- ensure-staging-model-version-aligns-with-sim-api
- publish-git-tag
- migrate-v1-staging-cloud-sql
- seed-v2-staging-database
- release-tests
- check-release-test-shards
if: |
!cancelled()
&& (github.repository == 'PolicyEngine/policyengine-api')
&& (github.event.head_commit.message == 'Update PolicyEngine API')
&& needs.ensure-staging-model-version-aligns-with-sim-api.result == 'success'
&& needs.publish-git-tag.result == 'success'
&& needs.migrate-v1-staging-cloud-sql.result == 'success'
&& needs.seed-v2-staging-database.result == 'success'
&& needs.release-tests.result == 'success'
&& needs.check-release-test-shards.result == 'success'
environment: staging
env:
DEPLOYMENT_ENVIRONMENT: staging
SIM_ENTRYPOINT: ${{ vars.SIM_ENTRYPOINT }}
SIMULATION_ENTRYPOINT_URL: ${{ secrets.SIMULATION_ENTRYPOINT_URL }}
POLICYENGINE_DB_INSTANCE_CONNECTION_NAME: ${{ vars.POLICYENGINE_DB_INSTANCE_CONNECTION_NAME }}
PRODUCTION_POLICYENGINE_DB_INSTANCE_CONNECTION_NAME: ${{ vars.PRODUCTION_POLICYENGINE_DB_INSTANCE_CONNECTION_NAME }}
ROUTE_IMPL_HEALTH: ${{ vars.ROUTE_IMPL_HEALTH }}
ROUTE_IMPL_SPECIFICATION: ${{ vars.ROUTE_IMPL_SPECIFICATION }}
ROUTE_IMPL_METADATA: ${{ vars.ROUTE_IMPL_METADATA }}
ROUTE_IMPL_POLICY: ${{ vars.ROUTE_IMPL_POLICY }}
ROUTE_IMPL_HOUSEHOLD: ${{ vars.ROUTE_IMPL_HOUSEHOLD }}
DB_READ_POLICY: ${{ vars.DB_READ_POLICY }}
DB_WRITE_POLICY: ${{ vars.DB_WRITE_POLICY }}
DB_READ_HOUSEHOLD: ${{ vars.DB_READ_HOUSEHOLD }}
DB_WRITE_HOUSEHOLD: ${{ vars.DB_WRITE_HOUSEHOLD }}
PUBLIC_API_AUTH_REQUIRED: ${{ vars.PUBLIC_API_AUTH_REQUIRED }}
PUBLIC_API_AUTH_ISSUER: ${{ vars.PUBLIC_API_AUTH_ISSUER }}
PUBLIC_API_AUTH_AUDIENCE: ${{ vars.PUBLIC_API_AUTH_AUDIENCE }}
PUBLIC_API_SERVER_CLIENT_IDS: ${{ vars.PUBLIC_API_SERVER_CLIENT_IDS }}
PUBLIC_API_SERVER_CREDENTIAL_REQUIRED: ${{ vars.PUBLIC_API_SERVER_CREDENTIAL_REQUIRED }}
PUBLIC_API_SERVER_AUTH_ISSUER: ${{ vars.PUBLIC_API_SERVER_AUTH_ISSUER }}
# Read-only (read:users) Auth0 client for admins' views of users. Unlike
# the production-only account-deletion settings, it can't delete the
# staging CI user.
AUTH0_USER_PROFILES_DOMAIN: ${{ vars.AUTH0_USER_PROFILES_DOMAIN }}
AUTH0_USER_PROFILES_CLIENT_ID: ${{ vars.AUTH0_USER_PROFILES_CLIENT_ID }}
AUTH0_USER_PROFILES_CLIENT_SECRET_RESOURCE: ${{ secrets.AUTH0_USER_PROFILES_CLIENT_SECRET_RESOURCE }}
# Invitation and type-change emails through the environment's Postmark
# server: its token's Secret Manager version, the sender address, and
# the app's address the emails link to. All three, or none.
POSTMARK_SERVER_TOKEN_SECRET_RESOURCE: ${{ secrets.POSTMARK_SERVER_TOKEN_SECRET_RESOURCE }}
POSTMARK_SENDER_ADDRESS: ${{ vars.POSTMARK_SENDER_ADDRESS }}
POLICYENGINE_APP_URL: ${{ vars.POLICYENGINE_APP_URL }}
CLOUD_RUN_SERVICE: policyengine-api-staging
CLOUD_RUN_RUNTIME_CACHE_ENVIRONMENT: staging
CLOUD_RUN_RUNTIME_CACHE_URL_SECRET: policyengine-api-staging-runtime-cache-url:latest
CLOUD_RUN_RUNTIME_CACHE_CA_CERT_SECRET: policyengine-api-staging-runtime-cache-ca:latest
CLOUD_RUN_POLICYENGINE_DB_PASSWORD_SECRET: ${{ vars.CLOUD_RUN_POLICYENGINE_DB_PASSWORD_SECRET }}
PRODUCTION_CLOUD_RUN_POLICYENGINE_DB_PASSWORD_SECRET: ${{ vars.PRODUCTION_CLOUD_RUN_POLICYENGINE_DB_PASSWORD_SECRET }}
V2_SUPABASE_PROJECT_REF: ${{ vars.V2_SUPABASE_PROJECT_REF }}
V2_SUPABASE_ENVIRONMENT: ${{ vars.V2_SUPABASE_ENVIRONMENT }}
PRODUCTION_V2_SUPABASE_PROJECT_REF: ${{ vars.PRODUCTION_V2_SUPABASE_PROJECT_REF }}
V2_RUNTIME_DATABASE_URL_SECRET_RESOURCE: ${{ secrets.V2_RUNTIME_DATABASE_URL_SECRET_RESOURCE }}
PRODUCTION_V2_RUNTIME_DATABASE_URL_SECRET_RESOURCE: ${{ vars.PRODUCTION_V2_RUNTIME_DATABASE_URL_SECRET_RESOURCE }}
# Staging stays scale-to-zero, single instance: it exists for per-push
# validation, not capacity. Both the revision-level (--min-instances) and
# service-level (--min) floors are 0.
CLOUD_RUN_MIN_INSTANCES: "0"
CLOUD_RUN_SERVICE_MIN_INSTANCES: "0"
CLOUD_RUN_MAX_INSTANCES: "1"
# uv installs uv.lock on the Cloud Run image's Python, not the 3.14
# that .python-version selects.
UV_PYTHON: "3.12"
permissions:
contents: read
id-token: write
outputs:
tag: ${{ steps.cloud_run.outputs.revision_tag }}
url: ${{ steps.candidate.outputs.url }}
revision: ${{ steps.candidate.outputs.revision }}
image: ${{ steps.candidate.outputs.image }}
stable_url: ${{ steps.previous.outputs.stable_url }}
previous_revision: ${{ steps.previous.outputs.revision }}
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Setup uv
uses: astral-sh/setup-uv@v6
# The staging integration tests below run from uv.lock.
- name: Install locked application dependencies
run: make install
- name: GCP authentication
uses: "google-github-actions/auth@v2"
with:
workload_identity_provider: "${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}"
service_account: "${{ secrets.GCP_DEPLOY_SERVICE_ACCOUNT }}"
- name: Set up GCloud
uses: "google-github-actions/setup-gcloud@v2"
- name: Install jq
run: sudo apt-get install -y jq
# One CI server token for this job's tests of the staging candidate,
# from the staging environment's own Auth0 tenant. Only the test step
# reads it (CI_SERVER_ACCESS_TOKEN). Requested before the deploy, so a
# missing credential stops the run before a candidate exists.
- name: Request the CI server credential for the staging candidate tests
id: ci_server_token
run: bash .github/scripts/request_ci_server_token.sh
env:
CI_SERVER_AUTH0_DOMAIN: ${{ vars.CI_SERVER_AUTH0_DOMAIN }}
CI_SERVER_AUTH0_CLIENT_ID: ${{ vars.CI_SERVER_AUTH0_CLIENT_ID }}
CI_SERVER_AUTH0_CLIENT_SECRET: ${{ secrets.CI_SERVER_AUTH0_CLIENT_SECRET }}
CI_SERVER_AUTH0_AUDIENCE: ${{ vars.CI_SERVER_AUTH0_AUDIENCE }}
# The staging database password is managed independently in Secret Manager
# and differs from the repository-level production database password.
- name: Synchronize shared Cloud Run runtime secrets
env:
POLICYENGINE_GITHUB_MICRODATA_AUTH_TOKEN: ${{ secrets.POLICYENGINE_GITHUB_MICRODATA_AUTH_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
PE_UK_PRIVATE_HF_READ_TOKEN: ${{ secrets.PE_UK_PRIVATE_HF_READ_TOKEN }}
run: bash .github/scripts/sync_cloud_run_secrets.sh
- name: Capture current Cloud Run staging state
id: previous
run: bash .github/scripts/capture_cloud_run_service_state.sh >> "$GITHUB_OUTPUT"
- name: Compute Cloud Run staging metadata
id: cloud_run
run: bash .github/scripts/compute_cloud_run_metadata.sh stg
- name: Build and push Cloud Run image
run: bash .github/scripts/build_cloud_run_image.sh
env:
CLOUD_RUN_IMAGE_TAG: ${{ steps.cloud_run.outputs.image_tag }}
CLOUD_RUN_TAG: ${{ steps.cloud_run.outputs.revision_tag }}
- name: Deploy tagged Cloud Run staging candidate
run: bash .github/scripts/deploy_cloud_run_candidate.sh
env:
PREVIOUS_CLOUD_RUN_REVISION: ${{ steps.previous.outputs.revision }}
CLOUD_RUN_IMAGE_TAG: ${{ steps.cloud_run.outputs.image_tag }}
CLOUD_RUN_TAG: ${{ steps.cloud_run.outputs.revision_tag }}
CLOUD_RUN_RUNTIME_SERVICE_ACCOUNT: policyengine-api-cr-staging@policyengine-api.iam.gserviceaccount.com
POLICYENGINE_DB_USER: ${{ vars.POLICYENGINE_DB_USER }}
POLICYENGINE_DB_NAME: ${{ vars.POLICYENGINE_DB_NAME }}
GATEWAY_AUTH_ISSUER: ${{ secrets.GATEWAY_AUTH_ISSUER }}
GATEWAY_AUTH_AUDIENCE: ${{ secrets.GATEWAY_AUTH_AUDIENCE }}
GATEWAY_AUTH_CLIENT_ID: ${{ secrets.GATEWAY_AUTH_CLIENT_ID }}
GATEWAY_AUTH_CLIENT_SECRET_RESOURCE: ${{ secrets.GATEWAY_AUTH_CLIENT_SECRET_RESOURCE }}
OBSERVABILITY_SERVICE_NAMESPACE: ${{ vars.OBSERVABILITY_SERVICE_NAMESPACE }}
OBSERVABILITY_TRACE_PROJECT_ID: ${{ vars.OBSERVABILITY_TRACE_PROJECT_ID }}
OTEL_EXPORTER_OTLP_ENDPOINT: ${{ vars.OBSERVABILITY_OTLP_ENDPOINT }}
POLICYENGINE_OTEL_GOOGLE_AUDIENCE: ${{ vars.OBSERVABILITY_OTLP_GOOGLE_AUDIENCE }}
- name: Resolve exact Cloud Run staging candidate
id: candidate
run: bash .github/scripts/resolve_cloud_run_candidate_state.sh >> "$GITHUB_OUTPUT"
env:
CLOUD_RUN_TAG: ${{ steps.cloud_run.outputs.revision_tag }}
- name: Wait for Cloud Run staging health
run: bash .github/scripts/health_check.sh "${{ steps.candidate.outputs.url }}/health"
- name: Run staging integration tests
run: uv run --frozen --extra dev python -m pytest tests/integration/test_cloud_run_candidate.py tests/integration/test_live_v2_metadata.py tests/integration/test_live_v2_policies.py -v
env:
API_BASE_URL: ${{ steps.candidate.outputs.url }}
CI_SERVER_ACCESS_TOKEN: ${{ steps.ci_server_token.outputs.token }}
STAGING_API_TEST_PROBE_ID: cloud-run-${{ steps.cloud_run.outputs.revision_tag }}
V2_MIGRATION_DATABASE_URL: ${{ secrets.V2_MIGRATION_DATABASE_URL }}
# Staging-only Auth0 CI application (password grant) and test user.
STAGING_AUTH0_TEST_DOMAIN: ${{ vars.STAGING_AUTH0_TEST_DOMAIN }}
STAGING_AUTH0_TEST_AUDIENCE: ${{ vars.STAGING_AUTH0_TEST_AUDIENCE }}
STAGING_AUTH0_TEST_CLIENT_ID: ${{ secrets.STAGING_AUTH0_TEST_CLIENT_ID }}
STAGING_AUTH0_TEST_CLIENT_SECRET: ${{ secrets.STAGING_AUTH0_TEST_CLIENT_SECRET }}
STAGING_AUTH0_TEST_USERNAME: ${{ secrets.STAGING_AUTH0_TEST_USERNAME }}
STAGING_AUTH0_TEST_PASSWORD: ${{ secrets.STAGING_AUTH0_TEST_PASSWORD }}
STAGING_AUTH0_TEST_REALM: ${{ vars.STAGING_AUTH0_TEST_REALM }}
parallel-live-simulation-tests-staging-cloud-run:
name: Run live simulations against the staging candidate
runs-on: ubuntu-latest
# The household calculation checks run after the other live tests and
# wait on Modal.
timeout-minutes: 30
needs: deploy-cloud-run-staging
if: |
!cancelled()
&& (github.repository == 'PolicyEngine/policyengine-api')
&& (github.event.head_commit.message == 'Update PolicyEngine API')
&& needs.deploy-cloud-run-staging.result == 'success'
# The staging environment holds the CI client the tests' token comes from.
environment: staging
env:
# uv installs uv.lock on the Cloud Run image's Python, not the 3.14
# that .python-version selects.
UV_PYTHON: "3.12"
permissions:
contents: read
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Setup uv
uses: astral-sh/setup-uv@v6
- name: Install locked live test dependencies
run: make install
# One CI server token for this job's tests of the staging candidate,
# from the staging environment's own Auth0 tenant. Only the test step
# reads it (CI_SERVER_ACCESS_TOKEN).
- name: Request the CI server credential for the staging candidate tests
id: ci_server_token
run: bash .github/scripts/request_ci_server_token.sh
env:
CI_SERVER_AUTH0_DOMAIN: ${{ vars.CI_SERVER_AUTH0_DOMAIN }}
CI_SERVER_AUTH0_CLIENT_ID: ${{ vars.CI_SERVER_AUTH0_CLIENT_ID }}
CI_SERVER_AUTH0_CLIENT_SECRET: ${{ secrets.CI_SERVER_AUTH0_CLIENT_SECRET }}
CI_SERVER_AUTH0_AUDIENCE: ${{ vars.CI_SERVER_AUTH0_AUDIENCE }}
# The staging candidate's settings, which the client must match.
PUBLIC_API_SERVER_AUTH_ISSUER: ${{ vars.PUBLIC_API_SERVER_AUTH_ISSUER }}
PUBLIC_API_AUTH_AUDIENCE: ${{ vars.PUBLIC_API_AUTH_AUDIENCE }}
PUBLIC_API_SERVER_CLIENT_IDS: ${{ vars.PUBLIC_API_SERVER_CLIENT_IDS }}
- name: Wait for Cloud Run staging candidate health before live tests
run: bash .github/scripts/health_check.sh "${{ needs.deploy-cloud-run-staging.outputs.url }}/health"
- name: Run live staging tests, one pytest worker per test
run: bash .github/scripts/run_live_staging_tests.sh
env:
API_BASE_URL: ${{ needs.deploy-cloud-run-staging.outputs.url }}
CI_SERVER_ACCESS_TOKEN: ${{ steps.ci_server_token.outputs.token }}
STAGING_API_TEST_PROBE_ID: cloud-run-${{ needs.deploy-cloud-run-staging.outputs.tag }}-${{ github.run_id }}-${{ github.run_attempt }}
promote-cloud-run-staging:
name: Promote staging Cloud Run traffic
runs-on: ubuntu-latest
needs:
- deploy-cloud-run-staging
- parallel-live-simulation-tests-staging-cloud-run
if: |
!cancelled()
&& (github.repository == 'PolicyEngine/policyengine-api')
&& (github.event.head_commit.message == 'Update PolicyEngine API')
&& needs.deploy-cloud-run-staging.result == 'success'
&& needs.parallel-live-simulation-tests-staging-cloud-run.result == 'success'
environment: staging
env:
CLOUD_RUN_SERVICE: policyengine-api-staging
POLICYENGINE_DB_INSTANCE_CONNECTION_NAME: ${{ vars.POLICYENGINE_DB_INSTANCE_CONNECTION_NAME }}
V2_SUPABASE_PROJECT_REF: ${{ vars.V2_SUPABASE_PROJECT_REF }}
V2_SUPABASE_ENVIRONMENT: ${{ vars.V2_SUPABASE_ENVIRONMENT }}
V2_RUNTIME_DATABASE_URL_SECRET_RESOURCE: ${{ secrets.V2_RUNTIME_DATABASE_URL_SECRET_RESOURCE }}
ROUTE_IMPL_HEALTH: ${{ vars.ROUTE_IMPL_HEALTH }}
ROUTE_IMPL_SPECIFICATION: ${{ vars.ROUTE_IMPL_SPECIFICATION }}
ROUTE_IMPL_METADATA: ${{ vars.ROUTE_IMPL_METADATA }}
ROUTE_IMPL_POLICY: ${{ vars.ROUTE_IMPL_POLICY }}
ROUTE_IMPL_HOUSEHOLD: ${{ vars.ROUTE_IMPL_HOUSEHOLD }}
DB_READ_POLICY: ${{ vars.DB_READ_POLICY }}
DB_WRITE_POLICY: ${{ vars.DB_WRITE_POLICY }}
DB_READ_HOUSEHOLD: ${{ vars.DB_READ_HOUSEHOLD }}
DB_WRITE_HOUSEHOLD: ${{ vars.DB_WRITE_HOUSEHOLD }}
permissions:
contents: read
id-token: write
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: GCP authentication
uses: "google-github-actions/auth@v2"
with:
workload_identity_provider: "${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}"
service_account: "${{ secrets.GCP_DEPLOY_SERVICE_ACCOUNT }}"
- name: Set up GCloud
uses: "google-github-actions/setup-gcloud@v2"
- name: Install jq
run: sudo apt-get install -y jq
- name: Verify exact tested Cloud Run staging candidate
run: bash .github/scripts/resolve_cloud_run_candidate_state.sh
env:
CLOUD_RUN_TAG: ${{ needs.deploy-cloud-run-staging.outputs.tag }}
CLOUD_RUN_EXPECTED_REVISION: ${{ needs.deploy-cloud-run-staging.outputs.revision }}
CLOUD_RUN_EXPECTED_IMAGE: ${{ needs.deploy-cloud-run-staging.outputs.image }}
- name: Promote Cloud Run staging candidate
id: promote
continue-on-error: true
run: bash .github/scripts/set_cloud_run_revision.sh
env:
CLOUD_RUN_TARGET_REVISION: ${{ needs.deploy-cloud-run-staging.outputs.revision }}
CLOUD_RUN_EXPECTED_CURRENT_REVISION: ${{ needs.deploy-cloud-run-staging.outputs.previous_revision }}
- name: Wait for Cloud Run staging service health
id: stable_health
if: ${{ steps.promote.outcome == 'success' }}
continue-on-error: true
run: bash .github/scripts/health_check.sh "${{ needs.deploy-cloud-run-staging.outputs.stable_url }}/health"
- name: Restore previous Cloud Run staging revision
if: ${{ steps.promote.outcome == 'failure' || steps.stable_health.outcome == 'failure' }}
run: bash .github/scripts/restore_cloud_run_revision.sh "${{ needs.deploy-cloud-run-staging.outputs.stable_url }}/health"
env:
CLOUD_RUN_TARGET_REVISION: ${{ needs.deploy-cloud-run-staging.outputs.previous_revision }}
CLOUD_RUN_EXPECTED_CURRENT_REVISION: ${{ needs.deploy-cloud-run-staging.outputs.revision }}
- name: Fail after unsuccessful Cloud Run staging promotion
if: ${{ always() && (steps.promote.outcome == 'failure' || steps.stable_health.outcome == 'failure') }}
run: exit 1
ensure-production-model-version-aligns-with-sim-api:
name: Ensure production model version aligns with simulation API
runs-on: ubuntu-latest
needs: promote-cloud-run-staging
if: |
!cancelled()
&& (github.repository == 'PolicyEngine/policyengine-api')
&& (github.event.head_commit.message == 'Update PolicyEngine API')
&& needs.promote-cloud-run-staging.result == 'success'
environment: production
env:
DEPLOYMENT_ENVIRONMENT: production
SIM_ENTRYPOINT: ${{ vars.SIM_ENTRYPOINT }}
SIMULATION_ENTRYPOINT_URL: ${{ secrets.SIMULATION_ENTRYPOINT_URL }}
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Check the simulation service belongs to production
run: bash .github/scripts/check_simulation_entrypoint_environment.sh
- name: Install jq
run: sudo apt-get install -y jq
- name: Set up uv for installed bundle validation
uses: astral-sh/setup-uv@v6
- name: Check the PolicyEngine bundle is the simulation API's latest
run: bash .github/check-policyengine-bundle-supported.sh
migrate-v1-production-cloud-sql:
name: Upgrade production v1 Cloud SQL schema
needs: ensure-production-model-version-aligns-with-sim-api
if: |
!cancelled()
&& (github.repository == 'PolicyEngine/policyengine-api')
&& (github.event.head_commit.message == 'Update PolicyEngine API')
&& needs.ensure-production-model-version-aligns-with-sim-api.result == 'success'
uses: ./.github/workflows/migrate-v1-cloud-sql.yml
with:
deployment_environment: production
github_environment: production-database
secrets: inherit
seed-v2-production-database:
# Runs beside migrate-v1-production-cloud-sql, as in staging. The
# production deploy needs both.
name: Seed production v2 database
needs:
- ensure-production-model-version-aligns-with-sim-api
if: |
!cancelled()
&& (github.repository == 'PolicyEngine/policyengine-api')
&& (github.event.head_commit.message == 'Update PolicyEngine API')
&& needs.ensure-production-model-version-aligns-with-sim-api.result == 'success'
uses: ./.github/workflows/seed-v2-database.yml
with:
deployment_environment: production
secrets: inherit
deploy-cloud-run-candidate:
name: Deploy production Cloud Run candidate
runs-on: ubuntu-latest
needs:
- migrate-v1-production-cloud-sql
- seed-v2-production-database
if: |
!cancelled()
&& (github.repository == 'PolicyEngine/policyengine-api')
&& (github.event.head_commit.message == 'Update PolicyEngine API')
&& needs.migrate-v1-production-cloud-sql.result == 'success'
&& needs.seed-v2-production-database.result == 'success'
environment: production
env:
DEPLOYMENT_ENVIRONMENT: production
SIM_ENTRYPOINT: ${{ vars.SIM_ENTRYPOINT }}
SIMULATION_ENTRYPOINT_URL: ${{ secrets.SIMULATION_ENTRYPOINT_URL }}
POLICYENGINE_DB_INSTANCE_CONNECTION_NAME: ${{ vars.POLICYENGINE_DB_INSTANCE_CONNECTION_NAME }}
PRODUCTION_POLICYENGINE_DB_INSTANCE_CONNECTION_NAME: ${{ vars.PRODUCTION_POLICYENGINE_DB_INSTANCE_CONNECTION_NAME }}
ROUTE_IMPL_HEALTH: ${{ vars.ROUTE_IMPL_HEALTH }}
ROUTE_IMPL_SPECIFICATION: ${{ vars.ROUTE_IMPL_SPECIFICATION }}
ROUTE_IMPL_METADATA: ${{ vars.ROUTE_IMPL_METADATA }}
ROUTE_IMPL_POLICY: ${{ vars.ROUTE_IMPL_POLICY }}
ROUTE_IMPL_HOUSEHOLD: ${{ vars.ROUTE_IMPL_HOUSEHOLD }}
DB_READ_POLICY: ${{ vars.DB_READ_POLICY }}
DB_WRITE_POLICY: ${{ vars.DB_WRITE_POLICY }}
DB_READ_HOUSEHOLD: ${{ vars.DB_READ_HOUSEHOLD }}
DB_WRITE_HOUSEHOLD: ${{ vars.DB_WRITE_HOUSEHOLD }}
PUBLIC_API_AUTH_REQUIRED: ${{ vars.PUBLIC_API_AUTH_REQUIRED }}
PUBLIC_API_AUTH_ISSUER: ${{ vars.PUBLIC_API_AUTH_ISSUER }}
PUBLIC_API_AUTH_AUDIENCE: ${{ vars.PUBLIC_API_AUTH_AUDIENCE }}
PUBLIC_API_SERVER_CLIENT_IDS: ${{ vars.PUBLIC_API_SERVER_CLIENT_IDS }}
PUBLIC_API_SERVER_CREDENTIAL_REQUIRED: ${{ vars.PUBLIC_API_SERVER_CREDENTIAL_REQUIRED }}
PUBLIC_API_SERVER_AUTH_ISSUER: ${{ vars.PUBLIC_API_SERVER_AUTH_ISSUER }}
AUTH0_MANAGEMENT_DOMAIN: ${{ vars.AUTH0_MANAGEMENT_DOMAIN }}
AUTH0_MANAGEMENT_CLIENT_ID: ${{ vars.AUTH0_MANAGEMENT_CLIENT_ID }}
AUTH0_MANAGEMENT_CLIENT_SECRET_RESOURCE: ${{ secrets.AUTH0_MANAGEMENT_CLIENT_SECRET_RESOURCE }}
AUTH0_USER_PROFILES_DOMAIN: ${{ vars.AUTH0_USER_PROFILES_DOMAIN }}
AUTH0_USER_PROFILES_CLIENT_ID: ${{ vars.AUTH0_USER_PROFILES_CLIENT_ID }}
AUTH0_USER_PROFILES_CLIENT_SECRET_RESOURCE: ${{ secrets.AUTH0_USER_PROFILES_CLIENT_SECRET_RESOURCE }}
POSTMARK_SERVER_TOKEN_SECRET_RESOURCE: ${{ secrets.POSTMARK_SERVER_TOKEN_SECRET_RESOURCE }}
POSTMARK_SENDER_ADDRESS: ${{ vars.POSTMARK_SENDER_ADDRESS }}
POLICYENGINE_APP_URL: ${{ vars.POLICYENGINE_APP_URL }}
CLOUD_RUN_SERVICE: policyengine-api
CLOUD_RUN_RUNTIME_CACHE_ENVIRONMENT: production
CLOUD_RUN_RUNTIME_CACHE_URL_SECRET: policyengine-api-prod-runtime-cache-url:latest
CLOUD_RUN_RUNTIME_CACHE_CA_CERT_SECRET: policyengine-api-prod-runtime-cache-ca:latest
CLOUD_RUN_POLICYENGINE_DB_PASSWORD_SECRET: ${{ vars.CLOUD_RUN_POLICYENGINE_DB_PASSWORD_SECRET }}
PRODUCTION_CLOUD_RUN_POLICYENGINE_DB_PASSWORD_SECRET: ${{ vars.PRODUCTION_CLOUD_RUN_POLICYENGINE_DB_PASSWORD_SECRET }}
V2_SUPABASE_PROJECT_REF: ${{ vars.V2_SUPABASE_PROJECT_REF }}
V2_SUPABASE_ENVIRONMENT: ${{ vars.V2_SUPABASE_ENVIRONMENT }}
PRODUCTION_V2_SUPABASE_PROJECT_REF: ${{ vars.PRODUCTION_V2_SUPABASE_PROJECT_REF }}
V2_RUNTIME_DATABASE_URL_SECRET_RESOURCE: ${{ secrets.V2_RUNTIME_DATABASE_URL_SECRET_RESOURCE }}
PRODUCTION_V2_RUNTIME_DATABASE_URL_SECRET_RESOURCE: ${{ vars.PRODUCTION_V2_RUNTIME_DATABASE_URL_SECRET_RESOURCE }}
# Sized by the Stage 2 qualification and the PR 4 host cutover — rationale
# and numbers in docs/migration/cloud-run-operations.md ("Runtime shape and
# scaling"). Warm capacity is expressed service-level (--min); the
# revision-level floor stays 0 (default in cloud_run_env.sh) to avoid
# per-tag pinning. Max raised to 8 for the 100% Cloud Run cutover: at 50/50
# the service was already pinned at the previous ceiling of 4 during the
# daytime peak, so full traffic needs the extra headroom.
CLOUD_RUN_SERVICE_MIN_INSTANCES: "2"
CLOUD_RUN_MAX_INSTANCES: "8"
# uv installs uv.lock on the Cloud Run image's Python, not the 3.14
# that .python-version selects.
UV_PYTHON: "3.12"
permissions:
contents: read
id-token: write
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Setup uv
uses: astral-sh/setup-uv@v6
- name: GCP authentication
uses: "google-github-actions/auth@v2"
with:
workload_identity_provider: "${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}"
service_account: "${{ secrets.GCP_DEPLOY_SERVICE_ACCOUNT }}"
- name: Set up GCloud
uses: "google-github-actions/setup-gcloud@v2"
- name: Install jq
run: sudo apt-get install -y jq
# One CI server token for this job's tests of the production candidate,
# from the production environment's own Auth0 tenant. Only the smoke
# test step reads it (CI_SERVER_ACCESS_TOKEN). Requested before the
# deploy, so a missing credential stops the run before a candidate
# exists.
- name: Request the CI server credential for the production candidate tests
id: ci_server_token
run: bash .github/scripts/request_ci_server_token.sh
env:
CI_SERVER_AUTH0_DOMAIN: ${{ vars.CI_SERVER_AUTH0_DOMAIN }}
CI_SERVER_AUTH0_CLIENT_ID: ${{ vars.CI_SERVER_AUTH0_CLIENT_ID }}
CI_SERVER_AUTH0_CLIENT_SECRET: ${{ secrets.CI_SERVER_AUTH0_CLIENT_SECRET }}
CI_SERVER_AUTH0_AUDIENCE: ${{ vars.CI_SERVER_AUTH0_AUDIENCE }}
- name: Synchronize complete Cloud Run runtime secret batch
env:
POLICYENGINE_DB_PASSWORD: ${{ secrets.POLICYENGINE_DB_PASSWORD }}
POLICYENGINE_GITHUB_MICRODATA_AUTH_TOKEN: ${{ secrets.POLICYENGINE_GITHUB_MICRODATA_AUTH_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
PE_UK_PRIVATE_HF_READ_TOKEN: ${{ secrets.PE_UK_PRIVATE_HF_READ_TOKEN }}
run: bash .github/scripts/sync_cloud_run_secrets.sh --include-database-password
- name: Capture current Cloud Run production state
id: previous
run: bash .github/scripts/capture_cloud_run_service_state.sh >> "$GITHUB_OUTPUT"
- name: Compute Cloud Run candidate metadata
id: cloud_run
run: bash .github/scripts/compute_cloud_run_metadata.sh stage3-prod
- name: Deploy tagged Cloud Run candidate
run: bash .github/scripts/deploy_cloud_run_candidate.sh
env:
PREVIOUS_CLOUD_RUN_REVISION: ${{ steps.previous.outputs.revision }}
CLOUD_RUN_IMAGE_TAG: ${{ steps.cloud_run.outputs.image_tag }}
CLOUD_RUN_TAG: ${{ steps.cloud_run.outputs.revision_tag }}
CLOUD_RUN_RUNTIME_SERVICE_ACCOUNT: ${{ secrets.GCP_CLOUD_RUN_RUNTIME_SERVICE_ACCOUNT }}
POLICYENGINE_DB_USER: ${{ vars.POLICYENGINE_DB_USER }}
POLICYENGINE_DB_NAME: ${{ vars.POLICYENGINE_DB_NAME }}
GATEWAY_AUTH_ISSUER: ${{ secrets.GATEWAY_AUTH_ISSUER }}
GATEWAY_AUTH_AUDIENCE: ${{ secrets.GATEWAY_AUTH_AUDIENCE }}
GATEWAY_AUTH_CLIENT_ID: ${{ secrets.GATEWAY_AUTH_CLIENT_ID }}
GATEWAY_AUTH_CLIENT_SECRET_RESOURCE: ${{ secrets.GATEWAY_AUTH_CLIENT_SECRET_RESOURCE }}
OBSERVABILITY_SERVICE_NAMESPACE: ${{ vars.OBSERVABILITY_SERVICE_NAMESPACE }}
OBSERVABILITY_TRACE_PROJECT_ID: ${{ vars.OBSERVABILITY_TRACE_PROJECT_ID }}
OTEL_EXPORTER_OTLP_ENDPOINT: ${{ vars.OBSERVABILITY_OTLP_ENDPOINT }}
POLICYENGINE_OTEL_GOOGLE_AUDIENCE: ${{ vars.OBSERVABILITY_OTLP_GOOGLE_AUDIENCE }}
- name: Resolve exact Cloud Run production candidate
id: candidate
run: bash .github/scripts/resolve_cloud_run_candidate_state.sh >> "$GITHUB_OUTPUT"
env:
CLOUD_RUN_TAG: ${{ steps.cloud_run.outputs.revision_tag }}
- name: Wait for Cloud Run candidate health
run: bash .github/scripts/health_check.sh "${{ steps.candidate.outputs.url }}/health"
- name: Install locked Cloud Run smoke test dependencies
run: make install
- name: Run Cloud Run candidate smoke tests
run: uv run --frozen --extra dev python -m pytest tests/integration/test_cloud_run_candidate.py tests/integration/test_live_v2_metadata.py -v
env:
API_BASE_URL: ${{ steps.candidate.outputs.url }}
CI_SERVER_ACCESS_TOKEN: ${{ steps.ci_server_token.outputs.token }}
STAGING_API_TEST_PROBE_ID: cloud-run-${{ steps.cloud_run.outputs.revision_tag }}
- name: Verify exact tested Cloud Run production candidate
run: bash .github/scripts/resolve_cloud_run_candidate_state.sh
env:
CLOUD_RUN_TAG: ${{ steps.cloud_run.outputs.revision_tag }}
CLOUD_RUN_EXPECTED_REVISION: ${{ steps.candidate.outputs.revision }}
CLOUD_RUN_EXPECTED_IMAGE: ${{ steps.candidate.outputs.image }}
- name: Promote Cloud Run production candidate
id: promote
continue-on-error: true
run: bash .github/scripts/set_cloud_run_revision.sh
env:
CLOUD_RUN_TARGET_REVISION: ${{ steps.candidate.outputs.revision }}
CLOUD_RUN_EXPECTED_CURRENT_REVISION: ${{ steps.previous.outputs.revision }}
- name: Wait for Cloud Run production service health
id: stable_health
if: ${{ steps.promote.outcome == 'success' }}
continue-on-error: true
run: bash .github/scripts/health_check.sh "${{ steps.previous.outputs.stable_url }}/health"
- name: Restore previous Cloud Run production revision