Skip to content

Commit 7c3d21b

Browse files
authored
Create GitHub Action script to prevent API v1 deployment if v1 and v2 model versions don't align (#2534)
* test: Try out script in PR with guaranteed failing run * fix: Pass Cloud project as env var * fix: Move project specification to script itself * fix: Pass project ID into workflow * fix: Install jq before running * fix: sudo not installed * fix: Update versions to test successful setup * fix: Correct UK version number * fix: Extract API versions when running * fix: Properly reference env var * fix: Properly import country package versions * fix: Clean up * fix: Move code to push.yaml, remove from pr.yaml * fix: Fix comments * fix: Respond to review
1 parent 5acacc6 commit 7c3d21b

5 files changed

Lines changed: 60 additions & 12 deletions

File tree

.github/find-api-model-versions.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
import sys
3+
from policyengine_api.constants import COUNTRY_PACKAGE_VERSIONS
4+
5+
6+
def find_api_model_versions_and_output_to_github():
7+
"""
8+
Find the API model versions and output them to a file for GitHub.
9+
"""
10+
# Try to get package versions for US and UK
11+
us_version = COUNTRY_PACKAGE_VERSIONS.get("us")
12+
uk_version = COUNTRY_PACKAGE_VERSIONS.get("uk")
13+
14+
if not us_version:
15+
print("Error: US package version not found.", file=sys.stderr)
16+
sys.exit(1)
17+
18+
if not uk_version:
19+
print("Error: UK package version not found.", file=sys.stderr)
20+
sys.exit(1)
21+
22+
# Write to GitHub Actions environment
23+
with open(os.environ["GITHUB_ENV"], "a") as f:
24+
f.write(f"US_VERSION={us_version}\n")
25+
f.write(f"UK_VERSION={uk_version}\n")
26+
27+
28+
if __name__ == "__main__":
29+
find_api_model_versions_and_output_to_github()
30+
print("API model versions found and written to GitHub environment.")
31+
sys.exit(0)

.github/install-jq.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#! /usr/bin/env bash
2+
3+
set -e
4+
5+
apt-get update
6+
apt-get install -y jq

.github/request-model-versions.sh renamed to .github/request-simulation-model-versions.sh

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
set -e
44

55
# Google Cloud Workflow execution script
6-
# Usage: ./wait_for_country_versions.sh -b <bucket_name> -us <us_version> -uk <uk_version> [-t timeout] [-i interval]
6+
# Usage: ./request-simulation-model-versions.sh -b <bucket_name> -us <us_version> -uk <uk_version> [-t timeout] [-i interval]
77

88
usage() {
9-
echo "Usage: $0 -b <bucket_name> -us <us_version> -uk <uk_version> [-t timeout] [-i interval]"
9+
echo "Usage: $0 -us <us_version> -uk <uk_version> [-t timeout] [-i interval]"
1010
echo ""
1111
echo "Required flags:"
1212
echo " -b bucket_name - GCS bucket name"
@@ -19,8 +19,8 @@ usage() {
1919
echo " -h help - Show this help message"
2020
echo ""
2121
echo "Example:"
22-
echo " $0 -b my-bucket -us v1.2.3 -uk v1.2.4"
23-
echo " $0 -b my-bucket -us v1.2.3 -uk v1.2.4 -t 600 -i 15"
22+
echo " $0 -us v1.2.3 -uk v1.2.4"
23+
echo " $0 -us v1.2.3 -uk v1.2.4 -t 600 -i 15"
2424
exit 1
2525
}
2626

@@ -34,6 +34,9 @@ CHECK_INTERVAL="10"
3434
# Parse command line arguments
3535
while [ $# -gt 0 ]; do
3636
case "$1" in
37+
-h|--help)
38+
usage
39+
;;
3740
-b)
3841
if [ -z "$2" ]; then
3942
echo "Error: -b requires a bucket name"
@@ -85,7 +88,7 @@ while [ $# -gt 0 ]; do
8588
done
8689

8790
# Validate required arguments
88-
if [ -z "$BUCKET_NAME" ] || [ -z "$US_VERSION" ] || [ -z "$UK_VERSION" ]; then
91+
if [ -z "$BUCKET_NAME" ] || -z "$US_VERSION" ] || [ -z "$UK_VERSION" ]; then
8992
echo "Error: Missing required arguments"
9093
echo "bucket_name (-b), us_version (-us), and uk_version (-uk) are required"
9194
usage
@@ -98,15 +101,10 @@ if ! [[ "$TIMEOUT_SECONDS" =~ ^[0-9]+$ ]] || ! [[ "$CHECK_INTERVAL" =~ ^[0-9]+$
98101
fi
99102

100103
# Configuration
101-
PROJECT_ID="${GOOGLE_CLOUD_PROJECT:-$(gcloud config get-value project 2>/dev/null)}"
102-
WORKFLOW_LOCATION="${WORKFLOW_LOCATION:-us-central1}"
104+
PROJECT_ID="prod-api-v2-c4d5"
105+
WORKFLOW_LOCATION="us-central1"
103106
WORKFLOW_NAME="wait-for-country-packages"
104107

105-
if [ -z "$PROJECT_ID" ]; then
106-
echo "Error: Could not determine project ID. Set GOOGLE_CLOUD_PROJECT environment variable."
107-
exit 1
108-
fi
109-
110108
echo "Starting workflow execution..."
111109
echo "Project: $PROJECT_ID"
112110
echo "Location: $WORKFLOW_LOCATION"
@@ -134,6 +132,7 @@ echo "Input: $INPUT_JSON"
134132
# Execute workflow
135133
echo "Executing workflow..."
136134
EXECUTION_RESULT=$(gcloud workflows execute "$WORKFLOW_NAME" \
135+
--project="$PROJECT_ID" \
137136
--location="$WORKFLOW_LOCATION" \
138137
--data="$INPUT_JSON" \
139138
--format="json")

.github/workflows/push.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ jobs:
6565
uses: actions/setup-python@v5
6666
with:
6767
python-version: "3.11"
68+
- name: Install dependencies (required for finding API model versions)
69+
run: make install
70+
- name: Install jq (required only for GitHub Actions)
71+
run: ".github/install-jq.sh"
72+
- name: Find API model versions and write to environment variable
73+
run: python3 .github/find-api-model-versions.py
74+
- name: Ensure full API and simulation API model versions are in sync
75+
run: ".github/request-simulation-model-versions.sh -b prod-api-v2-c4d5-metadata -us ${{ env.US_VERSION }} -uk ${{ env.UK_VERSION }}"
6876
- name: GCP authentication
6977
uses: "google-github-actions/auth@v2"
7078
with:

changelog_entry.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- bump: minor
2+
changes:
3+
added:
4+
- GitHub Actions checks that full API and simulation API model versions are in sync

0 commit comments

Comments
 (0)