@@ -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