-
Notifications
You must be signed in to change notification settings - Fork 14
415 lines (379 loc) · 15.4 KB
/
Copy pathtf-workflow.yml
File metadata and controls
415 lines (379 loc) · 15.4 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
---
name: 🌎 Terraform workflow
on:
workflow_call:
inputs:
working_directory:
required: true
type: string
description: 'Root directory of the terraform where all resources exist.'
provider:
required: true
type: string
description: 'Cloud provider to run the workflow. e.g. azurerm, aws, gcp or digitalocean'
aws_region:
required: false
type: string
default: us-east-2
description: 'AWS region of terraform deployment.'
gcp_region:
required: false
type: string
description: 'GCP region of terraform deployment.'
var_file:
required: false
type: string
description: 'Terraform var file directory. e.g. vars/dev.tfvars'
destroy:
required: false
type: boolean
default: false
description: 'Set true to destroy terraform infrastructure.'
approvers:
required: false
type: string
description: 'Approvals list to approve apply or destroy'
terraform_version:
type: string
default: 1.3.6
description: 'Required Terraform version'
timeout:
required: false
type: number
default: 10
description: 'Timeout for approval step'
minimum-approvals:
required: false
type: string
default: 1
description: 'Minimum approvals required to accept the plan'
token_format:
required: false
type: string
default: access_token
description: 'Output format for the generated authentication token. For OAuth 2.0 access tokens, specify "access_token". For OIDC tokens, specify "id_token". To skip token generation, leave this value empty'
access_token_lifetime:
required: false
type: string
default: 300s
description: 'Desired lifetime duration of the access token, in seconds'
project_id:
required: false
type: string
description: 'ID of the default project to use for future API calls and invocations.'
create_credentials_file:
required: false
type: string
default: true
description: 'If true, the action will securely generate a credentials file which can be used for authentication via gcloud and Google Cloud SDKs.'
git_ssh_key_setup:
required: false
type: string
default: false
description: 'If true, sets up SSH keys for Git access to clone private repositories.'
target_environment:
description: "Name of the deployment environment (e.g., dev, staging, prod). Leave empty if no environment-specific context is needed."
required: false
type: string
default: ""
plan_only:
description: "Set this to `true` to run `terraform plan` only"
required: false
type: boolean
default: false
target:
required: false
type: string
description: 'Target specific Terraform resource (e.g., module.vpc_ec2). If not set, fallback to target_file or target.txt.'
target_file:
required: false
type: string
description: 'Path to file with target resource (e.g., vars/target.txt)'
secrets:
AZURE_CREDENTIALS:
required: false
description: 'Azure Credentials to install Azure in github runner.'
AWS_ACCESS_KEY_ID:
required: false
description: 'AWS Access Key ID to install AWS CLI.'
BUILD_ROLE:
required: false
description: 'AWS OIDC role for aws authentication.'
AWS_SECRET_ACCESS_KEY:
required: false
description: 'AWS Secret access key to install AWS CLI'
AWS_SESSION_TOKEN:
required: false
description: 'AWS Session Token to install AWS CLI'
GCP_CREDENTIALS:
required: false
description: 'The Google Cloud JSON service account key to use for authentication'
DIGITALOCEAN_ACCESS_TOKEN:
required: false
description: 'The DigitalOcean Personal Access Token for Application & API'
env-vars:
required: false
description: 'Pass required environment variables'
WORKLOAD_IDENTITY_PROVIDER:
required: false
description: 'The full identifier of the Workload Identity Provider'
SERVICE_ACCOUNT:
required: false
description: 'The service account to be used'
SSH_PRIVATE_KEY:
required: false
description: 'Private SSH key to register in the SSH agent'
jobs:
terraform-workflow:
runs-on: ubuntu-latest
environment: ${{ inputs.target_environment }}
outputs:
tfplanExitCode: ${{ steps.tf-plan.outputs.exitcode }}
steps:
- name: 📦 Checkout
uses: actions/checkout@v6
- uses: webfactory/ssh-agent@v0.9.1
if: ${{ inputs.git_ssh_key_setup == true }}
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: 🌱 Set environment variables
run: |
(
cat <<'_EOT'
${{ secrets.env-vars }}
_EOT
) >> "$GITHUB_ENV"
- name: 🟦 Install AWS CLI
if: ${{ inputs.provider == 'aws' }}
uses: aws-actions/configure-aws-credentials@v6
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }}
role-to-assume: ${{ secrets.BUILD_ROLE }}
aws-region: ${{ inputs.aws_region }}
role-duration-seconds: 900
role-skip-session-tagging: true
- name: ☁️ Install Azure CLI
if: ${{ inputs.provider == 'azurerm' }}
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: ☁️ Authenticate to Google Cloud
if: ${{ inputs.provider == 'gcp' }}
uses: 'google-github-actions/auth@v3'
with:
credentials_json: '${{ secrets.GCP_CREDENTIALS }}'
create_credentials_file: ${{ inputs.create_credentials_file }}
token_format: ${{ inputs.token_format }}
workload_identity_provider: ${{ secrets.WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ secrets.SERVICE_ACCOUNT }}
access_token_lifetime: ${{ inputs.access_token_lifetime }}
project_id: ${{ inputs.project_id }}
- name: 🟦 Install doctl
if: ${{ inputs.provider == 'digitalocean' }}
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: 🛠️ Set up Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform_version }}
- name: 🧹 Terraform Format
if: ${{ inputs.destroy != true }}
id: fmt
uses: 'dflook/terraform-fmt-check@v2'
with:
actions_subcommand: 'fmt'
path: ${{ inputs.working_directory }}
- name: 🏗️ terraform init
run: |
cd ${{ inputs.working_directory }}
terraform init
- name: 🔎 Terraform validate
if: ${{ inputs.destroy != true }}
id: validate
uses: dflook/terraform-validate@v2
with:
path: ${{ inputs.working_directory }}
# Standard Terraform Plan (when not using targeting)
- name: 📋 Terraform Plan
if: ${{ inputs.target == '' && inputs.target_file == '' }}
id: tf-plan
uses: dflook/terraform-plan@v2
with:
path: ${{ inputs.working_directory }}
var_file: ${{ inputs.var_file }}
env:
GITHUB_TOKEN: ${{ github.TOKEN }}
# Targeted Terraform Plan (when using targeting)
- name: 📋 Terraform Plan (Targeted)
if: ${{ inputs.target != '' || inputs.target_file != '' }}
id: tf-plan
run: |
export exitcode=0
cd ${{ inputs.working_directory }}
TARGET=""
TARGET_FILE_PATH="${{ github.workspace }}/${{ inputs.target_file }}"
if [ -n "${{ inputs.target }}" ]; then
TARGET="${{ inputs.target }}"
elif [ -n "${{ inputs.target_file }}" ] && [ -f "$TARGET_FILE_PATH" ]; then
TARGET=$(cat "$TARGET_FILE_PATH" | tr -d '\n' | xargs)
elif [ -f "target.txt" ]; then
TARGET=$(cat target.txt | tr -d '\n' | xargs)
fi
echo "Final Target Value: '$TARGET'"
PLAN_CMD="terraform plan -out=tfplan"
if [ "${{ inputs.destroy }}" = true ]; then
PLAN_CMD="terraform plan -destroy -out=tfplan"
fi
if [ -n "${{ inputs.var_file }}" ]; then
PLAN_CMD="$PLAN_CMD --var-file=${{ inputs.var_file }}"
fi
if [ -n "$TARGET" ]; then
PLAN_CMD="$PLAN_CMD --target=$TARGET"
fi
echo "Running: $PLAN_CMD"
eval "$PLAN_CMD" || export exitcode=$?
echo "exitcode=$exitcode" >> $GITHUB_OUTPUT
# Upload plan artifact when using targeting
- name: 📤 Publish Terraform Plan Artifact
if: ${{ inputs.target != '' || inputs.target_file != '' }}
uses: actions/upload-artifact@v6
with:
name: tfplan
path: ${{ inputs.working_directory }}/tfplan
# Create plan string output when using targeting
- name: 📝 Create Plan String Output
if: ${{ (inputs.target != '' || inputs.target_file != '') && inputs.plan_only != true }}
id: tf-plan-string
run: |
cd ${{ inputs.working_directory }}
TERRAFORM_PLAN=$(terraform show -no-color tfplan)
delimiter="$(openssl rand -hex 8)"
echo "summary<<${delimiter}" >> $GITHUB_OUTPUT
echo "## Terraform Plan Output" >> $GITHUB_OUTPUT
echo "<details><summary>Click to expand</summary>" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo '```terraform' >> $GITHUB_OUTPUT
echo "$TERRAFORM_PLAN" >> $GITHUB_OUTPUT
echo '```' >> $GITHUB_OUTPUT
echo "</details>" >> $GITHUB_OUTPUT
echo "${delimiter}" >> $GITHUB_OUTPUT
# Fetch PR comment when not using targeting
- name: Fetch latest Plan PR comment
if: ${{ inputs.target == '' && inputs.target_file == '' && github.event_name == 'pull_request' }}
id: pr_plan_comment
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
run: |
COMMENT=$(curl -s \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/$REPO/issues/$PR_NUMBER/comments \
| jq -r '
map(select(.user.login=="github-actions[bot]"))
| last
')
BODY=$(echo "$COMMENT" | jq -r '.body')
URL=$(echo "$COMMENT" | jq -r '.html_url')
SUMMARY=$(echo "$BODY" \
| grep -Eo 'Plan: [0-9]+ to add, [0-9]+ to change, [0-9]+ to destroy\.' \
| uniq \
| head -n 1)
{
echo "PLAN_SUMMARY<<EOF"
echo "$SUMMARY"
echo "EOF"
echo "PLAN_COMMENT_URL=$URL"
} >> $GITHUB_ENV
- name: ✅ Accept plan or deny
if: ${{ inputs.plan_only != true }}
uses: trstringer/manual-approval@v1
timeout-minutes: ${{ inputs.timeout }}
with:
secret: ${{ github.TOKEN }}
approvers: ${{ inputs.approvers }}
minimum-approvals: ${{ inputs.minimum-approvals }}
issue-title: "Terraform Plan in ${{inputs.working_directory}}${{ inputs.target != '' || inputs.target_file != '' && ' (Targeted)' || '' }}"
issue-body: |
${{ inputs.target != '' || inputs.target_file != '' && format('## Targeted Resource\nTarget: {0}\n\n', inputs.target || 'From file') || '' }}
## Terraform Plan Summary
```
${{ inputs.target != '' || inputs.target_file != '' && steps.tf-plan-string.outputs.summary || env.PLAN_SUMMARY }}
```
${{ inputs.target == '' && inputs.target_file == '' && format('🔎 **Full Terraform plan :**\n👉 {0}\n', env.PLAN_COMMENT_URL) || '' }}
Please approve to continue with `terraform apply`.
# Standard terraform apply (when not using targeting)
- name: 🚀 terraform apply
if: ${{ inputs.destroy != true && inputs.plan_only != true && inputs.target == '' && inputs.target_file == '' }}
run: |
if [ -n "${{ inputs.var_file }}" ]; then
cd ${{ inputs.working_directory }}
terraform apply -var-file="${{ inputs.var_file }}" -auto-approve
else
cd ${{ inputs.working_directory }}
terraform apply -auto-approve
fi
# Targeted terraform apply (when using targeting)
- name: 🚀 terraform apply (Targeted)
if: ${{ inputs.destroy != true && inputs.plan_only != true && (inputs.target != '' || inputs.target_file != '') }}
run: |
cd ${{ inputs.working_directory }}
terraform apply -auto-approve tfplan
- name: 🕵️ Find Errored Terraform State
if: ${{ always() }}
id: find_errored_tfstate
run: |
cd ${{ inputs.working_directory }}
if [ -f "errored.tfstate" ]; then
ls -la errored.tfstate
echo "errored_found=true" >> $GITHUB_OUTPUT
echo "Uploading errored.tfstate as artifact..."
else
echo "errored_found=false" >> $GITHUB_OUTPUT
echo "Errored Terraform state file not found."
fi
- name: 📤 Upload Errored Terraform State Artifact
if: ${{ always() && steps.find_errored_tfstate.outputs.errored_found == 'true' }}
uses: actions/upload-artifact@v6
with:
name: errored_tfstate
path: ${{ inputs.working_directory }}/errored.tfstate
# Standard terraform destroy (when not using targeting)
- name: 💣 Terraform destroy
if: ${{ inputs.destroy == true && inputs.target == '' && inputs.target_file == '' }}
id: destroy
run: |
if [ -n "${{ inputs.var_file }}" ]; then
cd ${{ inputs.working_directory }}
terraform destroy -var-file="${{ inputs.var_file }}" -auto-approve
else
cd ${{ inputs.working_directory }}
terraform destroy -auto-approve
fi
# Targeted terraform destroy (when using targeting)
- name: 💣 Terraform destroy (Targeted)
if: ${{ inputs.destroy == true && (inputs.target != '' || inputs.target_file != '') }}
id: destroy
run: |
cd ${{ inputs.working_directory }}
TARGET=""
TARGET_FILE_PATH="${{ github.workspace }}/${{ inputs.target_file }}"
if [ -n "${{ inputs.target }}" ]; then
TARGET="${{ inputs.target }}"
elif [ -n "${{ inputs.target_file }}" ] && [ -f "$TARGET_FILE_PATH" ]; then
TARGET=$(cat "$TARGET_FILE_PATH" | tr -d '\n' | xargs)
elif [ -f "target.txt" ]; then
TARGET=$(cat target.txt | tr -d '\n' | xargs)
fi
echo "Final Target Value: '$TARGET'"
if [ -n "$TARGET" ]; then
terraform destroy --target="$TARGET" -auto-approve
elif [ -n "${{ inputs.var_file }}" ]; then
terraform destroy -var-file="${{ inputs.var_file }}" -auto-approve
else
terraform destroy -auto-approve
fi