Terraform Destroy #3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Terraform Destroy | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| environment: | |
| description: "Environment to destroy" | |
| required: true | |
| type: choice | |
| options: | |
| - staging | |
| - prod | |
| env: | |
| TF_WORKING_DIR: terraform | |
| jobs: | |
| destroy: | |
| name: Terraform Destroy | |
| runs-on: ubuntu-latest | |
| environment: ${{ github.event.inputs.environment }} | |
| permissions: | |
| contents: read | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Terraform | |
| uses: hashicorp/setup-terraform@v2 | |
| with: | |
| terraform_version: 1.5.6 | |
| - name: Configure AWS Credentials | |
| uses: aws-actions/configure-aws-credentials@v2 | |
| with: | |
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| aws-region: ${{ secrets.AWS_REGION }} | |
| # Backend selection based on input | |
| - name: Terraform Init | |
| working-directory: ${{ env.TF_WORKING_DIR }} | |
| run: | | |
| if [ "${{ github.event.inputs.environment }}" = "staging" ]; then | |
| terraform init -reconfigure -backend-config=backend-staging.tfvars -input=false | |
| else | |
| terraform init -reconfigure -backend-config=backend-prod.tfvars -input=false | |
| fi | |
| - name: Terraform Destroy | |
| working-directory: ${{ env.TF_WORKING_DIR }} | |
| run: | | |
| if [ "${{ github.event.inputs.environment }}" = "staging" ]; then | |
| terraform destroy -var-file="staging.tfvars" -auto-approve | |
| else | |
| terraform destroy -var-file="prod.tfvars" -auto-approve | |
| fi |