|
| 1 | +name: Terraform |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + paths: |
| 7 | + - 'infra/**' |
| 8 | + pull_request: |
| 9 | + branches: [main] |
| 10 | + paths: |
| 11 | + - 'infra/**' |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: terraform-${{ github.ref }} |
| 15 | + cancel-in-progress: false |
| 16 | + |
| 17 | +env: |
| 18 | + TF_WORKING_DIR: infra |
| 19 | + WORKLOAD_IDENTITY_PROVIDER: ${{ vars.WORKLOAD_IDENTITY_PROVIDER }} |
| 20 | + SERVICE_ACCOUNT: ${{ vars.GCP_SERVICE_ACCOUNT }} |
| 21 | + |
| 22 | +jobs: |
| 23 | + terraform: |
| 24 | + name: ${{ github.event_name == 'pull_request' && 'Plan' || 'Apply' }} |
| 25 | + runs-on: ubuntu-latest |
| 26 | + permissions: |
| 27 | + contents: read |
| 28 | + id-token: write |
| 29 | + pull-requests: write # For PR comments |
| 30 | + steps: |
| 31 | + - uses: actions/checkout@v4 |
| 32 | + |
| 33 | + - id: auth |
| 34 | + uses: google-github-actions/auth@v2 |
| 35 | + with: |
| 36 | + workload_identity_provider: ${{ env.WORKLOAD_IDENTITY_PROVIDER }} |
| 37 | + service_account: ${{ env.SERVICE_ACCOUNT }} |
| 38 | + |
| 39 | + - uses: hashicorp/setup-terraform@v3 |
| 40 | + |
| 41 | + - name: Terraform Init |
| 42 | + run: terraform init |
| 43 | + working-directory: ${{ env.TF_WORKING_DIR }} |
| 44 | + |
| 45 | + - name: Terraform Format Check |
| 46 | + run: terraform fmt -check -recursive |
| 47 | + working-directory: ${{ env.TF_WORKING_DIR }} |
| 48 | + |
| 49 | + - name: Terraform Validate |
| 50 | + run: terraform validate |
| 51 | + working-directory: ${{ env.TF_WORKING_DIR }} |
| 52 | + |
| 53 | + - name: Terraform Plan |
| 54 | + id: plan |
| 55 | + run: terraform plan -no-color -out=tfplan |
| 56 | + working-directory: ${{ env.TF_WORKING_DIR }} |
| 57 | + |
| 58 | + - name: Comment Plan on PR |
| 59 | + if: github.event_name == 'pull_request' |
| 60 | + uses: actions/github-script@v7 |
| 61 | + with: |
| 62 | + script: | |
| 63 | + const plan = `${{ steps.plan.outputs.stdout }}`; |
| 64 | + const truncated = plan.length > 60000 ? plan.substring(0, 60000) + '\n\n... (truncated)' : plan; |
| 65 | + const body = [ |
| 66 | + '#### Terraform Plan', |
| 67 | + '', |
| 68 | + '<details><summary>Show Plan</summary>', |
| 69 | + '', |
| 70 | + '```terraform', |
| 71 | + truncated, |
| 72 | + '```', |
| 73 | + '', |
| 74 | + '</details>', |
| 75 | + '', |
| 76 | + `*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`, |
| 77 | + ].join('\n'); |
| 78 | +
|
| 79 | + const { data: comments } = await github.rest.issues.listComments({ |
| 80 | + owner: context.repo.owner, |
| 81 | + repo: context.repo.repo, |
| 82 | + issue_number: context.issue.number, |
| 83 | + }); |
| 84 | +
|
| 85 | + const botComment = comments.find(c => c.body?.includes('Terraform Plan')); |
| 86 | + if (botComment) { |
| 87 | + await github.rest.issues.updateComment({ |
| 88 | + owner: context.repo.owner, |
| 89 | + repo: context.repo.repo, |
| 90 | + comment_id: botComment.id, |
| 91 | + body, |
| 92 | + }); |
| 93 | + } else { |
| 94 | + await github.rest.issues.createComment({ |
| 95 | + owner: context.repo.owner, |
| 96 | + repo: context.repo.repo, |
| 97 | + issue_number: context.issue.number, |
| 98 | + body, |
| 99 | + }); |
| 100 | + } |
| 101 | +
|
| 102 | + - name: Terraform Apply |
| 103 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
| 104 | + run: terraform apply -auto-approve tfplan |
| 105 | + working-directory: ${{ env.TF_WORKING_DIR }} |
0 commit comments