|
| 1 | +name: Terraform CI/CD |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: ['main'] |
| 6 | + paths: |
| 7 | + - 'infra/**' |
| 8 | + - '.github/workflows/terraform.yml' |
| 9 | + pull_request: |
| 10 | + types: [opened, synchronize, reopened] |
| 11 | + branches: ['main'] |
| 12 | + paths: |
| 13 | + - 'infra/**' |
| 14 | + - '.github/workflows/terraform.yml' |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: read |
| 18 | + pull-requests: write |
| 19 | + id-token: write |
| 20 | + |
| 21 | +env: |
| 22 | + ARM_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }} |
| 23 | + ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }} |
| 24 | + ARM_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }} |
| 25 | + ARM_USE_AZUREAD: true |
| 26 | + ARM_USE_OIDC: true |
| 27 | + |
| 28 | +jobs: |
| 29 | + plan: |
| 30 | + if: github.event_name == 'push' || github.event_name == 'pull_request' |
| 31 | + runs-on: ubuntu-latest |
| 32 | + name: Plan |
| 33 | + environment: production |
| 34 | + outputs: |
| 35 | + exitcode: ${{ steps.plan.outputs.exitcode }} |
| 36 | + |
| 37 | + concurrency: |
| 38 | + group: ${{ github.workflow }} |
| 39 | + cancel-in-progress: false |
| 40 | + |
| 41 | + steps: |
| 42 | + - name: Checkout repository |
| 43 | + uses: actions/checkout@v5 |
| 44 | + |
| 45 | + - name: Set up Terraform |
| 46 | + uses: hashicorp/setup-terraform@v3 |
| 47 | + with: |
| 48 | + terraform_wrapper: false |
| 49 | + |
| 50 | + - name: Login to Azure |
| 51 | + uses: azure/login@v2 |
| 52 | + with: |
| 53 | + client-id: ${{ env.ARM_CLIENT_ID }} |
| 54 | + subscription-id: ${{ env.ARM_SUBSCRIPTION_ID }} |
| 55 | + tenant-id: ${{ env.ARM_TENANT_ID }} |
| 56 | + |
| 57 | + - name: Initialize configuration |
| 58 | + run: terraform -chdir=infra init -input=false |
| 59 | + |
| 60 | + - name: Verify formatting |
| 61 | + run: terraform -chdir=infra fmt -check -recursive |
| 62 | + |
| 63 | + - name: Validate configuration |
| 64 | + run: terraform -chdir=infra validate |
| 65 | + |
| 66 | + - name: Build plan |
| 67 | + id: plan |
| 68 | + run: | |
| 69 | + export exitcode=0 |
| 70 | + terraform -chdir=infra plan -detailed-exitcode -out=tfplan || export exitcode=$? |
| 71 | +
|
| 72 | + echo "exitcode=$exitcode" >> $GITHUB_OUTPUT |
| 73 | +
|
| 74 | + if [ $exitcode -eq 1 ]; then |
| 75 | + exit 1 |
| 76 | + else |
| 77 | + exit 0 |
| 78 | + fi |
| 79 | +
|
| 80 | + - name: Publish plan |
| 81 | + uses: actions/upload-artifact@v4 |
| 82 | + with: |
| 83 | + name: tfplan-${{ github.run_id }} |
| 84 | + path: infra/tfplan |
| 85 | + |
| 86 | + - name: Build summary |
| 87 | + id: build-summary |
| 88 | + run: | |
| 89 | + TF_PLAN=$(terraform -chdir=infra show -no-color tfplan) |
| 90 | + |
| 91 | + delimiter="$(openssl rand -hex 8)" |
| 92 | + echo "summary<<${delimiter}" >> $GITHUB_OUTPUT |
| 93 | + echo "## Terraform Plan" >> $GITHUB_OUTPUT |
| 94 | + echo "<details><summary>Click to expand</summary>" >> $GITHUB_OUTPUT |
| 95 | + echo "" >> $GITHUB_OUTPUT |
| 96 | + echo '```terraform' >> $GITHUB_OUTPUT |
| 97 | + echo "$TF_PLAN" >> $GITHUB_OUTPUT |
| 98 | + echo '```' >> $GITHUB_OUTPUT |
| 99 | + echo "</details>" >> $GITHUB_OUTPUT |
| 100 | + echo "${delimiter}" >> $GITHUB_OUTPUT |
| 101 | +
|
| 102 | + - name: Publish summary |
| 103 | + env: |
| 104 | + SUMMARY: ${{ steps.build-summary.outputs.summary }} |
| 105 | + run: echo "$SUMMARY" >> $GITHUB_STEP_SUMMARY |
| 106 | + |
| 107 | + - name: Push summary to PR |
| 108 | + if: github.event_name == 'pull_request' |
| 109 | + uses: actions/github-script@v7 |
| 110 | + env: |
| 111 | + SUMMARY: "${{ steps.build-summary.outputs.summary }}" |
| 112 | + with: |
| 113 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 114 | + script: | |
| 115 | + const body = `${process.env.SUMMARY}`; |
| 116 | + github.rest.issues.createComment({ |
| 117 | + issue_number: context.issue.number, |
| 118 | + owner: context.repo.owner, |
| 119 | + repo: context.repo.repo, |
| 120 | + body: body |
| 121 | + }) |
| 122 | +
|
| 123 | + apply: |
| 124 | + needs: plan |
| 125 | + if: github.event_name == 'push' && needs.plan.outputs.exitcode == 2 |
| 126 | + runs-on: ubuntu-latest |
| 127 | + name: Apply |
| 128 | + environment: production |
| 129 | + |
| 130 | + concurrency: |
| 131 | + group: ${{ github.workflow }} |
| 132 | + cancel-in-progress: false |
| 133 | + |
| 134 | + steps: |
| 135 | + - name: Checkout repository |
| 136 | + uses: actions/checkout@v5 |
| 137 | + |
| 138 | + - name: Set up Terraform |
| 139 | + uses: hashicorp/setup-terraform@v3 |
| 140 | + with: |
| 141 | + terraform_wrapper: false |
| 142 | + |
| 143 | + - name: Login to Azure |
| 144 | + uses: azure/login@v2 |
| 145 | + with: |
| 146 | + client-id: ${{ env.ARM_CLIENT_ID }} |
| 147 | + subscription-id: ${{ env.ARM_SUBSCRIPTION_ID }} |
| 148 | + tenant-id: ${{ env.ARM_TENANT_ID }} |
| 149 | + |
| 150 | + - name: Initialize configuration |
| 151 | + run: terraform -chdir=infra init -input=false |
| 152 | + |
| 153 | + - name: Download plan |
| 154 | + uses: actions/download-artifact@v4 |
| 155 | + with: |
| 156 | + name: tfplan-${{ github.run_id }} |
| 157 | + path: infra |
| 158 | + |
| 159 | + - name: Apply plan |
| 160 | + run: terraform -chdir=infra apply -auto-approve tfplan |
0 commit comments