Skip to content

Commit c301f49

Browse files
committed
Wait for Cloud Run deploy before env sync
1 parent 948f0c7 commit c301f49

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

.github/workflows/sync-cloud-run-env.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,30 @@ jobs:
101101
project_id: ${{ env.GCP_PROJECT_ID }}
102102
version: ">= 416.0.0"
103103

104+
- name: Wait for Cloud Run deployment of current commit
105+
if: steps.config.outputs.enabled == 'true'
106+
run: |
107+
set -euo pipefail
108+
109+
target_sha="${GITHUB_SHA}"
110+
deadline=$((SECONDS + 1800))
111+
112+
while true; do
113+
deployed_sha="$(gcloud run services describe "${CLOUD_RUN_SERVICE}" --region "${CLOUD_RUN_REGION}" --format='value(spec.template.metadata.labels.commit-sha)' 2>/dev/null || true)"
114+
if [ -n "${deployed_sha}" ] && [ "${deployed_sha}" = "${target_sha}" ]; then
115+
echo "Cloud Run service ${CLOUD_RUN_SERVICE} is on commit ${deployed_sha}."
116+
break
117+
fi
118+
119+
if [ "${SECONDS}" -ge "${deadline}" ]; then
120+
echo "Timed out waiting for Cloud Run service ${CLOUD_RUN_SERVICE} to deploy commit ${target_sha}. Last seen commit: ${deployed_sha:-<none>}" >&2
121+
exit 1
122+
fi
123+
124+
echo "Waiting for Cloud Run service ${CLOUD_RUN_SERVICE} to deploy commit ${target_sha}. Last seen commit: ${deployed_sha:-<none>}" >&2
125+
sleep 10
126+
done
127+
104128
- name: Sync Cloud Run environment
105129
if: steps.config.outputs.enabled == 'true'
106130
run: |
@@ -249,6 +273,30 @@ jobs:
249273
project_id: ${{ env.GCP_PROJECT_ID }}
250274
version: ">= 416.0.0"
251275

276+
- name: Wait for Cloud Run deployment of current commit
277+
if: steps.config.outputs.enabled == 'true'
278+
run: |
279+
set -euo pipefail
280+
281+
target_sha="${GITHUB_SHA}"
282+
deadline=$((SECONDS + 1800))
283+
284+
while true; do
285+
deployed_sha="$(gcloud run services describe "${CLOUD_RUN_SERVICE}" --region "${CLOUD_RUN_REGION}" --format='value(spec.template.metadata.labels.commit-sha)' 2>/dev/null || true)"
286+
if [ -n "${deployed_sha}" ] && [ "${deployed_sha}" = "${target_sha}" ]; then
287+
echo "Cloud Run service ${CLOUD_RUN_SERVICE} is on commit ${deployed_sha}."
288+
break
289+
fi
290+
291+
if [ "${SECONDS}" -ge "${deadline}" ]; then
292+
echo "Timed out waiting for Cloud Run service ${CLOUD_RUN_SERVICE} to deploy commit ${target_sha}. Last seen commit: ${deployed_sha:-<none>}" >&2
293+
exit 1
294+
fi
295+
296+
echo "Waiting for Cloud Run service ${CLOUD_RUN_SERVICE} to deploy commit ${target_sha}. Last seen commit: ${deployed_sha:-<none>}" >&2
297+
sleep 10
298+
done
299+
252300
- name: Sync Cloud Run environment
253301
if: steps.config.outputs.enabled == 'true'
254302
run: |

tests/test_sync_cloud_run_env_workflow.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ grep -Fq 'permissions:' "$workflow_file"
1010
grep -Fq 'id-token: write' "$workflow_file"
1111
grep -Fq 'workload_identity_provider: ${{ env.GCP_WORKLOAD_IDENTITY_PROVIDER }}' "$workflow_file"
1212
grep -Fq 'service_account: ${{ env.GCP_WORKLOAD_IDENTITY_SERVICE_ACCOUNT }}' "$workflow_file"
13+
grep -Fq 'Wait for Cloud Run deployment of current commit' "$workflow_file"
14+
grep -Fq 'target_sha="${GITHUB_SHA}"' "$workflow_file"
15+
grep -Fq "gcloud run services describe \"\${CLOUD_RUN_SERVICE}\" --region \"\${CLOUD_RUN_REGION}\" --format='value(spec.template.metadata.labels.commit-sha)'" "$workflow_file"
16+
grep -Fq 'Timed out waiting for Cloud Run service ${CLOUD_RUN_SERVICE} to deploy commit ${target_sha}. Last seen commit: ${deployed_sha:-<none>}' "$workflow_file"
1317
grep -Fq "environment: longbridge-hk" "$workflow_file"
1418
grep -Fq "environment: longbridge-sg" "$workflow_file"
1519
grep -Fq 'ENABLE_GITHUB_ENV_SYNC: ${{ vars.ENABLE_GITHUB_ENV_SYNC }}' "$workflow_file"

0 commit comments

Comments
 (0)