SAM Pipelines with Github Actions (Cleanup)

by Afanasy Barbarov

Cleanup GitHub resources

As I promised, here is another Github Actions setup to delete the stack after pull request is merged.

It's simply one:

name: Cleanup resources

on:
  pull_request:
    types: [ closed ]

jobs:
  cleanup-feature-branch-stack:
    # this job will only run if the PR has been merged
    if: github.event.pull_request.merged == true\
     && startsWith('${{ github.event.pull_request.head.ref }}',\
     'feature-')
    runs-on: ubuntu-latest
    steps:
      - run: echo PR ${{ github.event.number }} has been merged
      - 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: eu-west-1
      - name: delete stack
        run: |
          aws cloudformation delete-stack \
          --stack-name ${{ github.event.pull_request.head.ref }}

That's all, folks!

Written by Afanasy Barbarov — Tech Lead with 15+ years shipping production systems in Rust, Go, and TypeScript. Facing a similar challenge? Reach out on LinkedIn. Support my work.

More articles

Previous post

Configure continuous deployments for a SAM application on Github.

Read more

Next post

The recipe: migrate the existing blog to AWS.

Read more