From 8b0f5e72688eb9309da87e8b5fc0aeb2a67e4c35 Mon Sep 17 00:00:00 2001 From: Falk Scheerschmidt Date: Wed, 22 Apr 2026 14:34:05 +0200 Subject: [PATCH] fix: deactivate stale in-progress deployments before creating new ones MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the Kustomization is updated in quick succession, GitHub Deployments can become orphaned — stuck in `in_progress` forever because the deployment-id annotation in the GitOps repo gets overwritten by the subsequent run and the flux-deployment-reporter only sees the latest ID. This adds a `deactivate_stale_deployments` function that runs before each new deployment creation. It queries existing deployments for the target environment and marks any with status `in_progress`, `queued`, or `pending` as `inactive`. The cleanup is best-effort and idempotent. Co-Authored-By: Claude --- action.yml | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/action.yml b/action.yml index 31dcc00..88f1cfe 100644 --- a/action.yml +++ b/action.yml @@ -334,11 +334,51 @@ runs: echo "${env}-${cluster}" } + deactivate_stale_deployments () { + local environment="$1" + + local deployments_response + deployments_response=$(curl -s \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer ${GITHUB_TOKEN_INPUT}" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/${GITHUB_REPOSITORY}/deployments?environment=${environment}&per_page=30") + + local deployment_ids + deployment_ids=$(echo "$deployments_response" | jq -r '.[].id // empty') + + for dep_id in $deployment_ids; do + local status_response + status_response=$(curl -s \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer ${GITHUB_TOKEN_INPUT}" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/${GITHUB_REPOSITORY}/deployments/${dep_id}/statuses?per_page=1") + + local latest_state + latest_state=$(echo "$status_response" | jq -r '.[0].state // empty') + + if [[ "$latest_state" == "in_progress" || "$latest_state" == "queued" || "$latest_state" == "pending" ]]; then + echo "Marking stale deployment ${dep_id} (${latest_state}) as inactive" >&2 + curl -s \ + -X POST \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer ${GITHUB_TOKEN_INPUT}" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/${GITHUB_REPOSITORY}/deployments/${dep_id}/statuses" \ + -d '{"state": "inactive", "description": "Superseded by newer deployment"}' > /dev/null + fi + done + } + create_deployment () { local environment="$1" local image="$2" local tag="$3" + # Deactivate any previous in-progress deployments for this environment + deactivate_stale_deployments "${environment}" + RESPONSE=$(curl -s -w "\n%{http_code}" \ -X POST \ -H "Accept: application/vnd.github+json" \