-
Notifications
You must be signed in to change notification settings - Fork 0
253 lines (211 loc) · 7.63 KB
/
pipeline.yml
File metadata and controls
253 lines (211 loc) · 7.63 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
# NOTE: Shared workflow for other repo
name: Terraform CI
on:
workflow_call:
inputs:
terraform_directory:
type: string
description: "Path to the directory where terraform is setup"
required: true
terraform_secrets_template_path:
type: string
description: "Path to the file for secrets.template.tfvars.tmpl relative to terraform_directory"
required: true
terragrunt_plan:
type: boolean
description: "Run terraform plan"
default: false
terragrunt_apply:
type: boolean
description: "Run terraform apply"
default: false
# AWS
aws_credential_setup:
type: boolean
description: "Setup credentials for AWS"
default: false
aws_credential_role_to_assume:
type: string
description: "Role to assume for the AWS"
aws_credential_region:
type: string
description: "AWS region"
secrets:
ENV_VARS:
description: "ENV Vars passed to terraform_secrets_template_path and terragrunt commands"
required: true
jobs:
pre_commit_check:
name: Pre-Commit check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
with:
submodules: true
- name: Install Terragrunt and OpenTofu
uses: gruntwork-io/terragrunt-action@v3
- name: Install TFLint
run: |
curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bash
echo "$HOME/.tflint.d/bin" >> "$GITHUB_PATH"
# 3. Run pre-commit hooks with built-in caching
- uses: pre-commit/action@v3.0.1
terragrunt-plan:
name: 'Terragrunt Plan'
runs-on: ubuntu-latest
needs: pre_commit_check
if: ${{ inputs.terragrunt_plan }}
defaults:
run:
working-directory: ${{ inputs.terraform_directory }}
permissions:
id-token: write # This is required for requesting the JWT
contents: read
pull-requests: write
steps:
- uses: actions/checkout@v6
with:
token: ${{ secrets.GITHUB_TOKEN }}
submodules: true
- name: Install Terragrunt and OpenTofu
uses: gruntwork-io/terragrunt-action@v3
- name: Load ENV_VARS
run: |
while IFS= read -r line; do
# Skip empty lines or comments
[[ -z "$line" || "$line" == \#* ]] && continue
key="${line%%=*}"
value="${line#*=}"
echo "::add-mask::$value"
# expose as outputs
echo "$key=$value" >> "$GITHUB_ENV"
done <<< "${{ secrets.ENV_VARS }}"
- name: Render tfvars file
run: |
envsubst < "${{ inputs.terraform_secrets_template_path }}" > terraform.auto.tfvars
echo "Generated tfvars:"
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v6.0.0
if: ${{ inputs.aws_credential_setup }}
with:
role-to-assume: ${{ inputs.aws_credential_role_to_assume }}
aws-region: ${{ inputs.aws_credential_region }}
role-session-name: GitHub_to_AWS_via_FederatedOIDC
- name: Terragrunt Init
id: init
run: terragrunt init
continue-on-error: true
- name: Terragrunt Plan
if: steps.init.outcome == 'success'
id: plan
run: |
set +e
terragrunt plan -out=tfplan -no-color -input=false \
> plan.txt 2>&1
PLAN_EXIT_CODE=$?
echo "PLAN<<EOF" >> "$GITHUB_OUTPUT"
if [ "$PLAN_EXIT_CODE" -eq 0 ]; then
# Replace raw output with formatted plan
terragrunt show -no-color tfplan >> plan.txt
cat plan.txt >> "$GITHUB_OUTPUT"
else
# Show the error output captured in plan.txt
cat plan.txt >> "$GITHUB_OUTPUT"
fi
echo "EOF" >> "$GITHUB_OUTPUT"
exit "$PLAN_EXIT_CODE"
continue-on-error: true
- name: Add Plan Comment
uses: actions/github-script@v8
env:
INIT_OUTCOME: ${{ steps.init.outcome }}
PLAN_OUTCOME: ${{ steps.plan.outcome }}
PLAN_OUTPUT: ${{ steps.plan.outputs.PLAN }}
GITHUB_ACTOR: ${{ github.actor }}
EVENT_NAME: ${{ github.event_name}}
GITHUB_WORKFLOW: ${{ github.workflow }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// unique identifier to reliably find the comment
const BOT_ID = '<!--terra-bot-->';
// 1. Retrieve existing bot comments for the PR
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
})
// Search for the comment using the unique ID instead of fragile text
const botComment = comments.find(comment => {
return comment.user.type === 'Bot' && comment.body.includes(BOT_ID)
})
// 2. Prepare format of the comment
// Use step outcomes (success/failure) in the main header for clarity
const output = `#### Terragrunt Initialization ⚙️\`${ process.env.INIT_OUTCOME }\`
#### Terragrunt Plan 📖\`${ process.env.PLAN_OUTCOME }\`
<details><summary>Show Plan</summary>
\`\`\`\n
${process.env.PLAN_OUTPUT}
\`\`\`
</details>
*Pusher: @${ process.env.GITHUB_ACTOR }, Action: \`${ process.env.EVENT_NAME }\`, Workflow: \`${ process.env.GITHUB_WORKFLOW }\`*
${BOT_ID}`; // <-- Append the unique ID to the output
// 3. If we have a comment, update it, otherwise create a new one
if (botComment) {
github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: output
})
} else {
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
}
terragrunt-apply:
name: Terragrunt Apply
runs-on: ubuntu-latest
if: ${{ inputs.terragrunt_apply }}
permissions:
id-token: write # This is required for requesting the JWT
contents: read
pull-requests: write
defaults:
run:
working-directory: ${{ inputs.terraform_directory }}
steps:
- uses: actions/checkout@v6
with:
token: ${{ secrets.GITHUB_TOKEN }}
submodules: true
- name: Install Terragrunt and OpenTofu
uses: gruntwork-io/terragrunt-action@v3
- name: Load ENV_VARS
run: |
while IFS= read -r line; do
# Skip empty lines or comments
[[ -z "$line" || "$line" == \#* ]] && continue
key="${line%%=*}"
value="${line#*=}"
echo "::add-mask::$value"
# expose as outputs
echo "$key=$value" >> "$GITHUB_ENV"
done <<< "${{ secrets.ENV_VARS }}"
- name: Render tfvars file
run: |
envsubst < "${{ inputs.terraform_secrets_template_path }}" > terraform.auto.tfvars
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v6.0.0
if: ${{ inputs.aws_credential_setup }}
with:
role-to-assume: ${{ inputs.aws_credential_role_to_assume }}
aws-region: ${{ inputs.aws_credential_region }}
role-session-name: GitHub_to_AWS_via_FederatedOIDC
- name: Terragrunt Init
run: terragrunt init
- name: Terragrunt Apply
run: terragrunt apply -auto-approve -input=false -no-color