Skip to content

Instantly share code, notes, and snippets.

@chris-burkhardt
Created June 28, 2021 03:01
Show Gist options
  • Save chris-burkhardt/015fc3bcda0ce08d32d37f3a343157d6 to your computer and use it in GitHub Desktop.
Save chris-burkhardt/015fc3bcda0ce08d32d37f3a343157d6 to your computer and use it in GitHub Desktop.
Github Actions Teardown Serverless Stack - Empty S3 Buckets
name: TeardownBranch
'on': [delete]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
region: [ap-southeast-2]
steps:
- name: Set the branch name as id so we can use it for named stack reference
run: echo "::set-output name=BRANCH_NAME::$(echo ${{github.event.ref}} | sed 's/\//_/g' | sed 's/.*_//' | tr '[:upper:]' '[:lower:]')"
id: set_branch_name_var
- name: Assume Role
if: github.ref != 'refs/heads/master'
uses: aws-actions/configure-aws-credentials@v1
with:
role-session-name: RoleSessionName
role-duration-seconds: 900
aws-region: '${{ matrix.region }}'
role-to-assume: '${{ secrets.AWS_ROLE }}'
aws-access-key-id: '${{ secrets.AWS_ACCESS_KEY_ID }}'
aws-secret-access-key: '${{ secrets.AWS_SECRET_ACCESS_KEY }}'
- uses: actions/checkout@v1
- name: Serverless Teardown
if: github.event.ref_type == 'branch'
run: |
cd Serverless
npm install
echo "Removing contents of all S3 buckets..."
for bucket in $(aws cloudformation describe-stack-resources
--stack-name ${{steps.set_branch_name_var.outputs.BRANCH_NAME }} --query
'StackResources[?(ResourceType==`AWS::S3::Bucket` &&
LogicalResourceId!=`ServerlessDeploymentBucket`)].PhysicalResourceId'
--output text); do echo aws s3 rm s3://$bucket --recursive; done;
echo "S3 bucket contents have been removed"
echo "Starting deletion of stack"
npx serverless remove -s ${{steps.set_branch_name_var.outputs.BRANCH_NAME }} -r ${{ matrix.region }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment