forked from DECSIS-ECI/Lab_P8_Terraform_Azure_LoadBalancing
-
Notifications
You must be signed in to change notification settings - Fork 0
251 lines (223 loc) · 8.99 KB
/
Copy pathterraform.yml
File metadata and controls
251 lines (223 loc) · 8.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# =============================================================================
# Lab #8 — Terraform CI/CD with Azure OIDC (no long-lived secrets)
# -----------------------------------------------------------------------------
# - On every PR targeting `main`: run fmt, validate and plan against the
# `dev` workspace and post the plan as a sticky comment.
# - On manual dispatch: run apply with explicit environment approval.
# - On manual dispatch (destroy=true): run destroy after approval.
#
# Authentication uses OpenID Connect (OIDC) federation between GitHub and
# Azure AD, so no client secret ever leaves Azure. Required GitHub repo
# variables / secrets:
#
# AZURE_CLIENT_ID (federated app registration's appId)
# AZURE_TENANT_ID
# AZURE_SUBSCRIPTION_ID
# BACKEND_RG (rg-tfstate-lab8)
# BACKEND_SA (sttfstate658)
# BACKEND_CONTAINER (tfstate)
# BACKEND_KEY (lab8/terraform.tfstate)
# =============================================================================
name: Terraform CI/CD
on:
pull_request:
branches: [main]
paths:
- "infra/**"
- "modules/**"
- ".github/workflows/terraform.yml"
workflow_dispatch:
inputs:
action:
description: "Action to perform"
required: true
default: "plan"
type: choice
options:
- plan
- apply
- destroy
permissions:
id-token: write # Required for OIDC token request to Azure AD
contents: read # Required to checkout the repo
pull-requests: write # Required to comment the plan on PRs
env:
TF_VERSION: "1.9.5"
TF_WORKING_DIR: infra
TF_VAR_FILE: env/dev.tfvars
ARM_USE_OIDC: "true"
ARM_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
ARM_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
jobs:
# ─── Static checks: fmt + validate ─────────────────────────────────────────
lint:
name: Lint (fmt + validate)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TF_VERSION }}
- name: Terraform fmt (recursive, check only)
run: terraform fmt -check -recursive
working-directory: .
- name: Azure Login (OIDC)
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
# Newer Terraform versions (>=1.10) validate the backend block even
# when -backend=false is passed, so we initialise against the real
# backend even for the lint job. The OIDC token is already minted in
# the previous step.
- name: Terraform Init (remote backend)
working-directory: ${{ env.TF_WORKING_DIR }}
run: |
terraform init \
-backend-config="resource_group_name=${{ secrets.BACKEND_RG }}" \
-backend-config="storage_account_name=${{ secrets.BACKEND_SA }}" \
-backend-config="container_name=${{ secrets.BACKEND_CONTAINER }}" \
-backend-config="key=${{ secrets.BACKEND_KEY }}"
- name: Terraform Validate
run: terraform validate -no-color
working-directory: ${{ env.TF_WORKING_DIR }}
# ─── Plan on every PR; comment results back ────────────────────────────────
plan:
name: Plan (PR)
needs: lint
if: github.event_name == 'pull_request' || (github.event_name == 'workflow_dispatch' && github.event.inputs.action == 'plan')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TF_VERSION }}
- name: Azure Login (OIDC)
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Terraform Init (remote backend)
working-directory: ${{ env.TF_WORKING_DIR }}
run: |
terraform init \
-backend-config="resource_group_name=${{ secrets.BACKEND_RG }}" \
-backend-config="storage_account_name=${{ secrets.BACKEND_SA }}" \
-backend-config="container_name=${{ secrets.BACKEND_CONTAINER }}" \
-backend-config="key=${{ secrets.BACKEND_KEY }}"
- name: Terraform Plan
id: plan
working-directory: ${{ env.TF_WORKING_DIR }}
run: |
terraform plan \
-var-file="${{ env.TF_VAR_FILE }}" \
-no-color \
-out=tfplan | tee plan.txt
terraform show -no-color tfplan > plan_full.txt
- name: Upload plan artifact
uses: actions/upload-artifact@v4
with:
name: tfplan
path: |
${{ env.TF_WORKING_DIR }}/tfplan
${{ env.TF_WORKING_DIR }}/plan_full.txt
retention-days: 7
- name: Comment plan on PR
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const plan = fs.readFileSync(
'${{ env.TF_WORKING_DIR }}/plan_full.txt', 'utf8'
).slice(0, 60000);
const body = [
'### Terraform Plan',
'',
'<details><summary>Click to expand the plan</summary>',
'',
'```hcl',
plan,
'```',
'',
'</details>',
'',
`_Triggered by @${context.actor} on \`${context.sha.substring(0,7)}\`_`
].join('\n');
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body
});
# ─── Apply on manual dispatch (with approval) ──────────────────────────────
apply:
name: Apply (manual)
needs: lint
if: github.event_name == 'workflow_dispatch' && github.event.inputs.action == 'apply'
runs-on: ubuntu-latest
environment:
name: production
url: https://portal.azure.com
steps:
- uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TF_VERSION }}
- name: Azure Login (OIDC)
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Terraform Init (remote backend)
working-directory: ${{ env.TF_WORKING_DIR }}
run: |
terraform init \
-backend-config="resource_group_name=${{ secrets.BACKEND_RG }}" \
-backend-config="storage_account_name=${{ secrets.BACKEND_SA }}" \
-backend-config="container_name=${{ secrets.BACKEND_CONTAINER }}" \
-backend-config="key=${{ secrets.BACKEND_KEY }}"
- name: Terraform Apply
working-directory: ${{ env.TF_WORKING_DIR }}
run: terraform apply -var-file="${{ env.TF_VAR_FILE }}" -auto-approve
- name: Show outputs
working-directory: ${{ env.TF_WORKING_DIR }}
run: terraform output
# ─── Destroy on manual dispatch (with approval) ────────────────────────────
destroy:
name: Destroy (manual)
needs: lint
if: github.event_name == 'workflow_dispatch' && github.event.inputs.action == 'destroy'
runs-on: ubuntu-latest
environment:
name: production
steps:
- uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TF_VERSION }}
- name: Azure Login (OIDC)
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Terraform Init (remote backend)
working-directory: ${{ env.TF_WORKING_DIR }}
run: |
terraform init \
-backend-config="resource_group_name=${{ secrets.BACKEND_RG }}" \
-backend-config="storage_account_name=${{ secrets.BACKEND_SA }}" \
-backend-config="container_name=${{ secrets.BACKEND_CONTAINER }}" \
-backend-config="key=${{ secrets.BACKEND_KEY }}"
- name: Terraform Destroy
working-directory: ${{ env.TF_WORKING_DIR }}
run: terraform destroy -var-file="${{ env.TF_VAR_FILE }}" -auto-approve