Skip to content

Commit

Permalink
Rollback template
Browse files Browse the repository at this point in the history
  • Loading branch information
vectornguyen76 committed Dec 2, 2024
1 parent ec12c18 commit 1046b93
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions .github/workflows/rollback.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,43 @@ on:
required: true

jobs:
rollback:
remove-stack:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Declare variables
shell: bash
run: |
echo "SHA_SHORT=$(git rev-parse --short "$GITHUB_SHA")" >> "$GITHUB_ENV"
- name: Configure AWS credentials
id: creds
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1

- name: Get stack name
- name: Remove stack on fail
run: |
echo "SHA_SHORT=$(git rev-parse --short "$GITHUB_SHA")" >> "$GITHUB_ENV"
echo "FULL_STACK_NAME=${{ inputs.stack_name }}-${{ env.SHA_SHORT }}" >> "$GITHUB_ENV"
- name: Remove failed stack
run: |
if aws cloudformation describe-stacks --stack-name ${{ env.FULL_STACK_NAME }} >/dev/null 2>&1; then
aws cloudformation delete-stack --stack-name ${{ env.FULL_STACK_NAME }}
aws cloudformation wait stack-delete-complete --stack-name ${{ env.FULL_STACK_NAME }}
echo ${{ inputs.stack_name }}-${{ env.SHA_SHORT }}
# Get stack id for the delete_stack waiter
stack_info=$(aws cloudformation describe-stacks --stack-name ${{ inputs.stack_name }}-${{ env.SHA_SHORT }} --query "Stacks[*] | [0].StackId" 2>&1)
if echo $stack_info | grep 'does not exist' > /dev/null
then
echo "Stack does not exist."
echo $stack_info
exit 0
fi
if echo $stack_info | grep 'ValidationError' > /dev/null
then
echo $stack_info
exit 1
else
echo "Stack ${{ env.FULL_STACK_NAME }} does not exist"
aws cloudformation delete-stack --stack-name ${{ inputs.stack_name }}-${{ env.SHA_SHORT }}
echo $stack_info
aws cloudformation wait stack-delete-complete --stack-name ${{ inputs.stack_name }}-${{ env.SHA_SHORT }}
exit 0
fi

0 comments on commit 1046b93

Please sign in to comment.