Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: delete empty changesets after cloudformation deploy #33

Merged
merged 2 commits into from
Dec 23, 2022
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
28 changes: 25 additions & 3 deletions .github/workflows/deploy_cloudformation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,32 @@ jobs:
${OPTIONAL_PARAMETERS} \
${{ inputs.additionalParameters }}

# If there are no changes aws cloudformation deploy creates empty changeset, which ends up in FAILED state. Eventually the number of changesets
# hits cloudformation maximum number of changesets limit, thus cloudformation stack becomes un-updatable. To counter this situation
# let's try to delete this changeset after deploy. It is challenging to delete the exact changeset, so 1 changeset with the relevant
# description is selected and deleted. Avoiding to delete all potential changesets as deleting is sequential and could potentially
# take a long time (=> waste of GH credits, prolong deploy time,...)

# Using jq, because "--output text" appends "\nNone" to response and that would need to processed.
CHANGE_SET_NAME=$(
aws cloudformation list-change-sets \
--stack-name ${{ inputs.stackName }} \
--max-items 1 \
--output json \
--query "Summaries[?StatusReason=='The submitted information didn\'t contain changes. Submit different information to create a change set.'].ChangeSetName" \
| jq -r '.[0]')

if [ "${CHANGE_SET_NAME}" != "null" ]; then
echo ">>> Empty changeset found! Name: ${CHANGE_SET_NAME}. Deleting..."
aws cloudformation delete-change-set \
--stack-name ${{ inputs.stackName }} \
--change-set-name ${CHANGE_SET_NAME}
fi

STACK_OUTPUTS=$(aws cloudformation describe-stacks \
--stack-name ${{ inputs.stackName }} \
--output json \
--query 'Stacks[]' | jq -c '.[] | select(.Outputs != null) | .Outputs | map( { (.OutputKey|tostring): . } ) | add')
--stack-name ${{ inputs.stackName }} \
--output json \
--query 'Stacks[]' | jq -c '.[] | select(.Outputs != null) | .Outputs | map( { (.OutputKey|tostring): . } ) | add')

echo "stackOutputs=$STACK_OUTPUTS" >> $GITHUB_OUTPUT

Expand Down