-
Notifications
You must be signed in to change notification settings - Fork 0
368 lines (323 loc) · 12.8 KB
/
terraform.yml
File metadata and controls
368 lines (323 loc) · 12.8 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# =============================================================================
# Terraform CI/CD Pipeline
# =============================================================================
#
# PIPELINE STAGES:
#
# ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐
# │ Lint & │───▶│ Security │───▶│ Plan │──┬▶│ Apply │───▶│ Apply │ │ Infracost│
# │ Format │ │ (Checkov)│ │ (matrix) │ │ │ DEV │ │ PROD │ │ Cost Est │
# └──────────┘ └──────────┘ └──────────┘ │ └──────────┘ └──────────┘ └──────────┘
# PR PR PR │ merge only merge + gate └── PR only
#
# RELEASE LIFECYCLE:
# 1. Developer pushes to feature branch and opens a PR
# 2. Lint job checks formatting (terraform fmt) and runs TFLint
# 3. Security job runs Checkov static analysis on all Terraform files
# 4. Plan jobs run in parallel for dev and prod (matrix strategy)
# 5. Plan output is posted as a PR comment for team review
# 6. On merge to main:
# a. Dev environment auto-applies (fast feedback)
# b. Prod requires manual approval via GitHub Environment protection
#
# REQUIRED SECRETS:
# ARM_CLIENT_ID - Azure Service Principal App ID
# ARM_CLIENT_SECRET - Azure Service Principal Password
# ARM_SUBSCRIPTION_ID - Azure Subscription ID
# ARM_TENANT_ID - Azure AD Tenant ID
#
# REQUIRED GITHUB ENVIRONMENTS:
# dev - No protection rules (auto-deploy)
# production - Required reviewers + wait timer (manual approval)
#
# =============================================================================
name: "Terraform CI/CD"
on:
push:
branches: [main, master]
paths:
- "modules/**"
- "environments/**"
- ".github/workflows/**"
- ".tflint.hcl"
- ".checkov.yml"
pull_request:
branches: [main, master]
paths:
- "modules/**"
- "environments/**"
- ".github/workflows/**"
- ".tflint.hcl"
- ".checkov.yml"
permissions:
contents: read
pull-requests: write
id-token: write
security-events: write
env:
TF_VERSION: "1.7.0"
TF_IN_AUTOMATION: "true"
ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }}
ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET }}
ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }}
ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }}
ARM_ACCESS_KEY: ${{ secrets.ARM_ACCESS_KEY }}
# ---------------------------------------------------------------------------
# JOBS
# ---------------------------------------------------------------------------
jobs:
# =========================================================================
# Stage 1: Lint & Format Check
# =========================================================================
# Runs on every PR and push to main.
# Ensures all .tf files are properly formatted and pass TFLint rules.
# This is the first gate — fails fast before heavier jobs run.
# =========================================================================
lint:
name: "Lint & Format"
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TF_VERSION }}
- name: Check formatting
run: terraform fmt -check -recursive -diff
- name: Setup TFLint
uses: terraform-linters/setup-tflint@v4
with:
tflint_version: latest
- name: Init TFLint plugins
run: tflint --init --config=.tflint.hcl
- name: Lint VNET module
run: tflint --config=../../.tflint.hcl
working-directory: modules/vnet
- name: Lint dev environment
run: tflint --config=../../.tflint.hcl
working-directory: environments/dev
- name: Lint prod environment
run: tflint --config=../../.tflint.hcl
working-directory: environments/prod
# =========================================================================
# Stage 2: Security Scan (Checkov)
# =========================================================================
# Runs Checkov static analysis against all Terraform configurations.
# Checks for security misconfigurations such as:
# - Storage accounts without encryption
# - VMs with password authentication
# - Key Vaults without purge protection
# - Missing network restrictions
# - Overly permissive NSG rules
#
# Results are uploaded as SARIF to GitHub Security tab.
# Soft-fail is enabled so findings are reported but don't block the pipeline.
# =========================================================================
security:
name: "Security Scan (Checkov)"
runs-on: ubuntu-latest
needs: lint
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run Checkov on VNET module
uses: bridgecrewio/checkov-action@v12
with:
directory: modules/vnet
framework: terraform
config_file: .checkov.yml
output_format: cli,sarif
output_file_path: console,checkov-module.sarif
soft_fail: true
quiet: true
- name: Run Checkov on dev environment
uses: bridgecrewio/checkov-action@v12
with:
directory: environments/dev
framework: terraform
config_file: .checkov.yml
output_format: cli,sarif
output_file_path: console,checkov-dev.sarif
soft_fail: true
quiet: true
- name: Run Checkov on prod environment
uses: bridgecrewio/checkov-action@v12
with:
directory: environments/prod
framework: terraform
config_file: .checkov.yml
output_format: cli,sarif
output_file_path: console,checkov-prod.sarif
soft_fail: true
quiet: true
- name: Upload Checkov SARIF to GitHub Security
if: always()
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: checkov-module.sarif
category: checkov-module
# =========================================================================
# Stage 3: Terraform Plan (per environment)
# =========================================================================
# Uses a matrix strategy to plan dev and prod in parallel.
# On PRs, the plan output is posted as a comment for reviewers.
# This lets the team see exactly what will change before approving.
# =========================================================================
plan:
name: "Plan - ${{ matrix.environment }}"
runs-on: ubuntu-latest
needs: [lint, security]
strategy:
fail-fast: false
matrix:
environment: [dev, prod]
defaults:
run:
working-directory: environments/${{ matrix.environment }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TF_VERSION }}
- name: Terraform Init
run: terraform init
- name: Terraform Validate
run: terraform validate
- name: Terraform Plan
id: plan
run: terraform plan -no-color -out=${{ matrix.environment }}.tfplan
continue-on-error: true
- name: Post plan output to PR comment
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const output = `### Terraform Plan — \`${{ matrix.environment }}\` ${{ steps.plan.outcome == 'success' && '✅' || '❌' }}
<details><summary>Show Plan</summary>
\`\`\`
${{ steps.plan.outputs.stdout }}
\`\`\`
</details>
*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
});
- name: Fail if plan failed
if: steps.plan.outcome == 'failure'
run: exit 1
# =========================================================================
# Stage 3b: Infracost Cost Estimation (PR only)
# =========================================================================
# Shows the cost impact of infrastructure changes as a PR comment.
# Compares the cost of the current branch against the base branch.
# Requires INFRACOST_API_KEY secret (free tier: https://infracost.io).
# =========================================================================
cost:
name: "Cost Estimate"
runs-on: ubuntu-latest
needs: plan
if: github.event_name == 'pull_request' && vars.INFRACOST_ENABLED == 'true'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Infracost
uses: infracost/actions/setup@v3
with:
api-key: ${{ secrets.INFRACOST_API_KEY }}
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TF_VERSION }}
- name: Infracost breakdown (dev)
run: |
cd environments/dev
terraform init -backend=false
cd ../..
infracost breakdown \
--path environments/dev \
--format json \
--out-file /tmp/infracost-dev.json
- name: Infracost breakdown (prod)
run: |
cd environments/prod
terraform init -backend=false
cd ../..
infracost breakdown \
--path environments/prod \
--format json \
--out-file /tmp/infracost-prod.json
- name: Combine Infracost outputs
run: |
infracost output \
--path "/tmp/infracost-dev.json" \
--path "/tmp/infracost-prod.json" \
--format json \
--out-file /tmp/infracost-combined.json
- name: Post Infracost comment
run: |
infracost comment github \
--path /tmp/infracost-combined.json \
--repo ${{ github.repository }} \
--github-token ${{ secrets.GITHUB_TOKEN }} \
--pull-request ${{ github.event.pull_request.number }} \
--behavior update
# =========================================================================
# Stage 4: Apply Dev (automatic on merge)
# =========================================================================
# Triggered only on push to main (i.e., after PR merge).
# Uses the "dev" GitHub Environment (no protection rules).
# This gives immediate feedback on whether changes work in a real env.
# =========================================================================
apply-dev:
name: "Apply - dev"
runs-on: ubuntu-latest
needs: plan
if: (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master') && github.event_name == 'push'
environment: dev
defaults:
run:
working-directory: environments/dev
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TF_VERSION }}
- name: Terraform Init
run: terraform init
- name: Terraform Apply
run: terraform apply -auto-approve
# =========================================================================
# Stage 5: Apply Prod (manual approval gate)
# =========================================================================
# Runs after dev succeeds. Requires manual approval through the
# "production" GitHub Environment (configured with required reviewers).
# This is the final safety gate before changes reach production.
# =========================================================================
apply-prod:
name: "Apply - prod"
runs-on: ubuntu-latest
needs: apply-dev
if: (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master') && github.event_name == 'push'
environment: production
defaults:
run:
working-directory: environments/prod
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TF_VERSION }}
- name: Terraform Init
run: terraform init
- name: Terraform Apply
run: terraform apply -auto-approve