Skip to content

Commit 8b0f5e7

Browse files
0x46616c6bclaude
andcommitted
fix: deactivate stale in-progress deployments before creating new ones
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 <claude@anthropic.com>
1 parent 2f0c038 commit 8b0f5e7

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

action.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,51 @@ runs:
334334
echo "${env}-${cluster}"
335335
}
336336
337+
deactivate_stale_deployments () {
338+
local environment="$1"
339+
340+
local deployments_response
341+
deployments_response=$(curl -s \
342+
-H "Accept: application/vnd.github+json" \
343+
-H "Authorization: Bearer ${GITHUB_TOKEN_INPUT}" \
344+
-H "X-GitHub-Api-Version: 2022-11-28" \
345+
"https://api.github.com/repos/${GITHUB_REPOSITORY}/deployments?environment=${environment}&per_page=30")
346+
347+
local deployment_ids
348+
deployment_ids=$(echo "$deployments_response" | jq -r '.[].id // empty')
349+
350+
for dep_id in $deployment_ids; do
351+
local status_response
352+
status_response=$(curl -s \
353+
-H "Accept: application/vnd.github+json" \
354+
-H "Authorization: Bearer ${GITHUB_TOKEN_INPUT}" \
355+
-H "X-GitHub-Api-Version: 2022-11-28" \
356+
"https://api.github.com/repos/${GITHUB_REPOSITORY}/deployments/${dep_id}/statuses?per_page=1")
357+
358+
local latest_state
359+
latest_state=$(echo "$status_response" | jq -r '.[0].state // empty')
360+
361+
if [[ "$latest_state" == "in_progress" || "$latest_state" == "queued" || "$latest_state" == "pending" ]]; then
362+
echo "Marking stale deployment ${dep_id} (${latest_state}) as inactive" >&2
363+
curl -s \
364+
-X POST \
365+
-H "Accept: application/vnd.github+json" \
366+
-H "Authorization: Bearer ${GITHUB_TOKEN_INPUT}" \
367+
-H "X-GitHub-Api-Version: 2022-11-28" \
368+
"https://api.github.com/repos/${GITHUB_REPOSITORY}/deployments/${dep_id}/statuses" \
369+
-d '{"state": "inactive", "description": "Superseded by newer deployment"}' > /dev/null
370+
fi
371+
done
372+
}
373+
337374
create_deployment () {
338375
local environment="$1"
339376
local image="$2"
340377
local tag="$3"
341378
379+
# Deactivate any previous in-progress deployments for this environment
380+
deactivate_stale_deployments "${environment}"
381+
342382
RESPONSE=$(curl -s -w "\n%{http_code}" \
343383
-X POST \
344384
-H "Accept: application/vnd.github+json" \

0 commit comments

Comments
 (0)