Skip to content

Commit

Permalink
Merge pull request #977 from CDCgov/destroy_pr_environment_on_pr_close
Browse files Browse the repository at this point in the history
Destroy PR Environment Only on PR Close
  • Loading branch information
halprin authored Mar 27, 2024
2 parents 714190f + a19f790 commit 428dc09
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 33 deletions.
34 changes: 1 addition & 33 deletions .github/workflows/terraform-ci-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:

- uses: actions/checkout@v4

# keep in sync with terraform-ci-destroy.yml
- uses: dorny/paths-filter@v3
id: filter
with:
Expand Down Expand Up @@ -74,36 +75,3 @@ jobs:
- paths-filter
uses: ./.github/workflows/deploy_reusable-skip.yml
if: needs.paths-filter.outputs.operations != 'true'


destroy-environment:
name: Destroy PR Environment
environment:
name: pr
needs:
- pr-deploy
- paths-filter
if: needs.paths-filter.outputs.operations == 'true' && always()
runs-on: ubuntu-latest
env:
ARM_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
ARM_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
ARM_USE_OIDC: true
permissions:
id-token: write
contents: read
defaults:
run:
working-directory: operations/environments/pr

steps:

- uses: actions/checkout@v4

- name: Terraform Init
id: init
run: terraform init -backend-config="key=pr_${{ github.event.number }}.tfstate"

- name: Terraform Destroy
run: terraform destroy -auto-approve -input=false -var="pr_number=${{ github.event.number }}"
56 changes: 56 additions & 0 deletions .github/workflows/terraform-ci-destroy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Terraform CI Destroy

on:
pull_request:
types:
- closed

jobs:

paths-filter:
runs-on: ubuntu-latest
outputs:
operations: ${{ steps.filter.outputs.operations }}

steps:

- uses: actions/checkout@v4

# keep in sync with terraform-ci-deploy.yml
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
operations:
- 'operations/environments/pr/**'
- 'operations/template/**'
destroy-environment:
name: Destroy PR Environment
environment:
name: pr
needs:
- paths-filter
if: needs.paths-filter.outputs.operations == 'true'
runs-on: ubuntu-latest
env:
ARM_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
ARM_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
ARM_USE_OIDC: true
permissions:
id-token: write
contents: read
defaults:
run:
working-directory: operations/environments/pr

steps:

- uses: actions/checkout@v4

- name: Terraform Init
run: terraform init -backend-config="key=pr_${{ github.event.number }}.tfstate"

- name: Terraform Destroy
run: terraform destroy -auto-approve -input=false -var="pr_number=${{ github.event.number }}"

0 comments on commit 428dc09

Please sign in to comment.