Skip to content
This repository has been archived by the owner on Feb 7, 2025. It is now read-only.

Commit

Permalink
Delete the PR environment only when the PR is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
halprin committed Mar 26, 2024
1 parent a64eab3 commit 06dfa87
Show file tree
Hide file tree
Showing 2 changed files with 58 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 }}"
57 changes: 57 additions & 0 deletions .github/workflows/terraform-ci-destroy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Terraform CI Deploy

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
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 }}"

0 comments on commit 06dfa87

Please sign in to comment.