manual terraform #15
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: manual terraform | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| apply_or_destroy: | |
| type: choice | |
| description: "Apply or destroy" | |
| required: true | |
| options: | |
| - "apply" | |
| - "destroy" | |
| directory: | |
| type: string | |
| description: "Directory to apply or destroy" | |
| required: true | |
| permissions: | |
| id-token: write | |
| contents: read | |
| env: | |
| S3_BUCKET: "terraform-state-bucket-730335203034" | |
| jobs: | |
| apply: | |
| if: ${{ github.event.inputs.apply_or_destroy == 'apply' }} | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 10 | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Setup AWS Credentials | |
| uses: ./.github/actions/configure_aws_credentials | |
| id: configure-aws-credentials | |
| with: | |
| aws-role-arn: ${{ secrets.AWS_ROLE_ARN }} | |
| - name: Get Terraform version | |
| id: terraform-version | |
| uses: bigwheel/[email protected] | |
| - name: SetUp Terraform | |
| uses: hashicorp/setup-terraform@v3 | |
| with: | |
| terraform_version: ${{ steps.terraform-version.outputs.terraform_version }} | |
| - name: Terraform Init | |
| working-directory: ${{ github.event.inputs.directory }} | |
| run: terraform init | |
| - name: Terraform Apply | |
| working-directory: ${{ github.event.inputs.directory }} | |
| run: terraform apply -auto-approve | |
| destroy: | |
| if: ${{ github.event.inputs.apply_or_destroy == 'destroy' }} | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 10 | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Setup AWS Credentials | |
| uses: ./.github/actions/configure_aws_credentials | |
| id: configure-aws-credentials | |
| with: | |
| aws-role-arn: ${{ secrets.AWS_ROLE_ARN }} | |
| - name: Get Terraform version | |
| id: terraform-version | |
| uses: bigwheel/[email protected] | |
| - name: SetUp Terraform | |
| uses: hashicorp/setup-terraform@v3 | |
| with: | |
| terraform_version: ${{ steps.terraform-version.outputs.terraform_version }} | |
| - name: Terraform Init | |
| working-directory: ${{ github.event.inputs.directory }} | |
| run: terraform init | |
| - name: Terraform destroy | |
| working-directory: ${{ github.event.inputs.directory }} | |
| run: | | |
| s3_bucket=$(grep 'backend "s3"' backend.tf -A6 | grep bucket | awk '{print $3}' | sed -e 's/"//g') | |
| filename=$(basename ${{ github.event.inputs.directory }}.tfstate) | |
| aws s3 cp s3://${s3_bucket}/${filename} terraform.tfstate | |
| terraform destroy -auto-approve -lock-timeout=10m |