diff --git a/.github/workflows/rollback.yml b/.github/workflows/rollback.yml index 1a4ff2d..5349419 100644 --- a/.github/workflows/rollback.yml +++ b/.github/workflows/rollback.yml @@ -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