Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/sync-cloud-run-env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,62 @@ jobs:

gcloud "${gcloud_args[@]}"
done

- name: Prune old Cloud Run revisions
if: steps.config.outputs.enabled == 'true'
env:
SYNC_PLAN_JSON: ${{ steps.strategy_requirements.outputs.sync_plan_json }}
run: |
set -euo pipefail

mapfile -t target_services < <(python - <<'PY'
import json
import os
plan = json.loads(os.environ["SYNC_PLAN_JSON"])
for target in plan["targets"]:
print(target["service_name"])
PY
)

for cloud_run_service in "${target_services[@]}"; do
keep_revisions="$(
gcloud run services describe "${cloud_run_service}" \
--region "${CLOUD_RUN_REGION}" \
--format=json \
| python -c '
import json
import sys
service = json.load(sys.stdin)
revisions = {
item.get("revisionName")
for item in service.get("status", {}).get("traffic", [])
if item.get("revisionName") and int(item.get("percent") or 0) > 0
}
Comment on lines +594 to +595

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep tagged revisions during prune

The keep-set only includes traffic targets with percent > 0, so revisions that are reachable only via a traffic tag (0% rollout) are treated as deletable. Cloud Run documentation states tagged revisions can be tested while serving 0% traffic and also that revisions that are able to receive traffic cannot be deleted; in that case gcloud run revisions delete will fail and this workflow step will start failing for services that keep tagged canary/test revisions. Include tagged revisions (or otherwise detect tag entries) in keep_revisions before deletion.

Useful? React with 👍 / 👎.

latest_ready = service.get("status", {}).get("latestReadyRevisionName")
if latest_ready:
revisions.add(latest_ready)
for revision in sorted(revisions):
print(revision)
'
)"

if [ -z "${keep_revisions}" ]; then
echo "No active Cloud Run revision found for ${cloud_run_service}; skipping revision prune." >&2
continue
fi

mapfile -t revisions_to_delete < <(
gcloud run revisions list \
--service "${cloud_run_service}" \
--region "${CLOUD_RUN_REGION}" \
--format='value(metadata.name)' \
| grep -Fvx -f <(printf '%s\n' "${keep_revisions}") || true
)

for revision in "${revisions_to_delete[@]}"; do
echo "Deleting old Cloud Run revision ${revision} for ${cloud_run_service}."
gcloud run revisions delete "${revision}" \
--region "${CLOUD_RUN_REGION}" \
--quiet
done
done
Loading