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" \