Update newdestroy.yaml #63
This file contains 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: Destroy Terraform Workflow | |
on: | |
#workflow_dispatch: | |
permissions: | |
contents: read | |
id-token: write | |
jobs: | |
terraform: | |
name: Terraform Destroy Workflow | |
runs-on: ubuntu-latest | |
steps: | |
# Step 1: Checkout code | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
# Step 2: Setup Terraform | |
- name: Setup Terraform | |
uses: hashicorp/setup-terraform@v2 | |
with: | |
terraform_version: 1.5.6 | |
# Step 3: Configure AWS Credentials | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v2 | |
with: | |
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/${{ secrets.ROLE_NAME }} | |
aws-region: ${{ secrets.AWS_REGION }} | |
role-session-name: TerraformDestroySession | |
# Step 4: Terraform Init | |
- name: Terraform Init | |
env: | |
FAILURE_FLAG: false | |
run: | | |
terraform init || echo "FAILURE_FLAG=true" >> $GITHUB_ENV | |
# Step 5: Terraform Destroy | |
- name: Terraform Destroy | |
env: | |
TF_LOG: TRACE | |
TF_LOG_PATH: terraform_destroy.log | |
FAILURE_FLAG: false | |
run: | | |
terraform destroy -auto-approve || echo "FAILURE_FLAG=true" >> $GITHUB_ENV | |
# Step 6: Ensure Logs Directory Exists | |
- name: Ensure Logs Directory Exists | |
run: mkdir -p $GITHUB_WORKSPACE/logs | |
# Step 7: Copy Terraform Destroy Logs to Logs Directory | |
- name: Copy Terraform Destroy Logs to Logs Directory | |
run: | | |
if [ -f terraform_destroy.log ]; then | |
cp terraform_destroy.log $GITHUB_WORKSPACE/logs/ | |
fi | |
# Step 8: Print Logs on Failure | |
- name: Print Logs on Failure | |
if: ${{ env.FAILURE_FLAG == 'true' }} | |
run: | | |
echo "Terraform Destroy failed. Logs are as follows:" | |
cat $GITHUB_WORKSPACE/logs/terraform_destroy.log || true | |
# Step 9: Upload Terraform Destroy Logs as Artifacts | |
- name: Upload Terraform Destroy Logs as Artifacts | |
if: always() # This will always run | |
uses: actions/upload-artifact@v3 | |
with: | |
name: terraform-destroy-logs | |
path: logs/ | |
# Step 10: Fail Workflow if Terraform Destroy Fails | |
- name: Fail Workflow if Terraform Destroy Fails | |
if: ${{ env.FAILURE_FLAG == 'true' }} | |
run: | | |
echo "Terraform Destroy failed. Marking workflow as failed." | |
exit 1 |