From 0dcd22d67421c7792e312530a653807167e445af Mon Sep 17 00:00:00 2001 From: Dan Grebb Date: Mon, 25 Sep 2023 21:56:24 -0400 Subject: [PATCH] feat(github): adds cleanup job to main workflow --- .github/workflows/bdt-fe.yml | 38 ++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/.github/workflows/bdt-fe.yml b/.github/workflows/bdt-fe.yml index e6f4b5136..08638cdb4 100644 --- a/.github/workflows/bdt-fe.yml +++ b/.github/workflows/bdt-fe.yml @@ -1,4 +1,4 @@ -name: ๐Ÿšœ Build, Deploy, and Test +name: ๐Ÿšœ Build, Deploy, and Test run-name: "๐Ÿšœ ${{ github.ref_name == 'main' && 'PRD' || 'STG' }} - Build, Deploy, and Test - ${{ github.event_name == 'pull_request' && format('PR #{0}: {1}', github.event.pull_request.number, github.event.pull_request.title) || format('latest {0}', github.ref_name) }}" on: @@ -21,7 +21,6 @@ env: BRANCH_NAME: ${{ github.head_ref || github.ref_name }} jobs: - build_deploy: name: ๐Ÿš€ Build & Deploy uses: ./.github/workflows/bd-fe.yml @@ -77,3 +76,38 @@ jobs: uses: ./.github/workflows/test-psi.yml secrets: PSI_APIKEY: ${{ secrets.PSI_APIKEY }} + + cleanup: + name: ๐Ÿงน Cleanup + runs-on: ubuntu-latest + concurrency: + group: build-deploy + cancel-in-progress: true + steps: + - name: Delete Previous deployments + uses: actions/github-script@v6 + env: + GITHUB_SHA_HEAD: ${{ github.event.pull_request.head.sha }} + with: + script: | + const { GITHUB_SHA_HEAD } = process.env + const deployments = await github.rest.repos.listDeployments({ + owner: context.repo.owner, + repo: context.repo.repo, + sha: GITHUB_SHA_HEAD + }); + await Promise.all( + deployments.data.map(async (deployment) => { + await github.rest.repos.createDeploymentStatus({ + owner: context.repo.owner, + repo: context.repo.repo, + deployment_id: deployment.id, + state: 'inactive' + }); + return github.rest.repos.deleteDeployment({ + owner: context.repo.owner, + repo: context.repo.repo, + deployment_id: deployment.id + }); + }) + );