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
40 changes: 40 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand Down
Loading