|
| 1 | +name: Terraform |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + paths: |
| 8 | + - 'infra/**' |
| 9 | + pull_request: |
| 10 | + paths: |
| 11 | + - 'infra/**' |
| 12 | + |
| 13 | +permissions: |
| 14 | + id-token: write |
| 15 | + contents: read |
| 16 | + pull-requests: write |
| 17 | + |
| 18 | +concurrency: |
| 19 | + group: terraform-${{ github.ref }} |
| 20 | + cancel-in-progress: false |
| 21 | + |
| 22 | +env: |
| 23 | + TF_VAR_aws_profile: "" |
| 24 | + |
| 25 | +jobs: |
| 26 | + plan: |
| 27 | + name: Terraform Plan |
| 28 | + runs-on: ubuntu-latest |
| 29 | + if: github.event_name == 'pull_request' |
| 30 | + |
| 31 | + steps: |
| 32 | + - name: Checkout repository |
| 33 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 34 | + |
| 35 | + - name: Configure AWS credentials |
| 36 | + uses: aws-actions/configure-aws-credentials@ececac1a45f3b08a01d2dd070d28d111c5fe6722 # v4.1.0 |
| 37 | + with: |
| 38 | + role-to-assume: ${{ secrets.AWS_TERRAFORM_ROLE_ARN }} |
| 39 | + aws-region: us-east-1 |
| 40 | + |
| 41 | + - name: Setup Terraform |
| 42 | + uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2 |
| 43 | + with: |
| 44 | + terraform_wrapper: false |
| 45 | + |
| 46 | + - name: Terraform Init |
| 47 | + working-directory: infra |
| 48 | + run: terraform init |
| 49 | + |
| 50 | + - name: Terraform Validate |
| 51 | + working-directory: infra |
| 52 | + run: terraform validate |
| 53 | + |
| 54 | + - name: Terraform Plan |
| 55 | + id: plan |
| 56 | + working-directory: infra |
| 57 | + run: | |
| 58 | + set -o pipefail |
| 59 | + terraform plan -no-color -out=tfplan 2>&1 | tee plan_output.txt |
| 60 | + echo "plan_exitcode=$?" >> "$GITHUB_OUTPUT" |
| 61 | +
|
| 62 | + - name: Post Plan to PR |
| 63 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 64 | + with: |
| 65 | + script: | |
| 66 | + const fs = require('fs'); |
| 67 | + const plan = fs.readFileSync('infra/plan_output.txt', 'utf8'); |
| 68 | + const truncated = plan.length > 60000 |
| 69 | + ? plan.substring(0, 60000) + '\n\n... (truncated)' |
| 70 | + : plan; |
| 71 | + const body = [ |
| 72 | + '### Terraform Plan', |
| 73 | + '```', |
| 74 | + truncated, |
| 75 | + '```', |
| 76 | + `*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*` |
| 77 | + ].join('\n'); |
| 78 | + github.rest.issues.createComment({ |
| 79 | + issue_number: context.issue.number, |
| 80 | + owner: context.repo.owner, |
| 81 | + repo: context.repo.repo, |
| 82 | + body: body |
| 83 | + }); |
| 84 | +
|
| 85 | + - name: Setup Infracost |
| 86 | + uses: infracost/actions/setup@e9d6e6cd65e168e76b0de50ff9957d2fe8bb1832 # v3.0.1 |
| 87 | + with: |
| 88 | + api-key: ${{ secrets.INFRACOST_API_KEY }} |
| 89 | + |
| 90 | + - name: Run Infracost Breakdown |
| 91 | + working-directory: infra |
| 92 | + run: infracost breakdown --path=. --format=json --out-file=/tmp/infracost.json |
| 93 | + |
| 94 | + - name: Post Infracost Comment |
| 95 | + run: | |
| 96 | + infracost comment github \ |
| 97 | + --path=/tmp/infracost.json \ |
| 98 | + --repo=${{ github.repository }} \ |
| 99 | + --pull-request=${{ github.event.pull_request.number }} \ |
| 100 | + --github-token=${{ github.token }} \ |
| 101 | + --behavior=update |
| 102 | +
|
| 103 | + apply: |
| 104 | + name: Terraform Apply |
| 105 | + runs-on: ubuntu-latest |
| 106 | + if: github.ref == 'refs/heads/master' && github.event_name == 'push' |
| 107 | + environment: production-infra |
| 108 | + |
| 109 | + steps: |
| 110 | + - name: Checkout repository |
| 111 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 112 | + |
| 113 | + - name: Configure AWS credentials |
| 114 | + uses: aws-actions/configure-aws-credentials@ececac1a45f3b08a01d2dd070d28d111c5fe6722 # v4.1.0 |
| 115 | + with: |
| 116 | + role-to-assume: ${{ secrets.AWS_TERRAFORM_ROLE_ARN }} |
| 117 | + aws-region: us-east-1 |
| 118 | + |
| 119 | + - name: Setup Terraform |
| 120 | + uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2 |
| 121 | + with: |
| 122 | + terraform_wrapper: false |
| 123 | + |
| 124 | + - name: Terraform Init |
| 125 | + working-directory: infra |
| 126 | + run: terraform init |
| 127 | + |
| 128 | + - name: Terraform Apply |
| 129 | + working-directory: infra |
| 130 | + run: terraform apply -auto-approve |
0 commit comments