generated from stackxcloud/template-terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
288 lines (240 loc) Β· 9.41 KB
/
pr-terraform.yml
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
name: Terraform
on:
pull_request:
paths:
- 'examples/**'
- 'tests/**'
- '**.tf'
repository_dispatch:
types: [ ok-to-test-command ]
jobs:
# Branch-based pull request
terraform-pr-branch:
name: terraform-pr-branch
runs-on: ubuntu-latest
permissions:
id-token: write
pull-requests: write
timeout-minutes: 15
env:
TF_IN_AUTOMATION: true
TFDIR: examples
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
steps:
- name: Branch based PR checkout
uses: actions/checkout@v4
- name: Configure AWS credentials
if: startsWith(github.repository, 'ventx/terraform-aws-')
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ secrets.AWS_DEFAULT_REGION }}
role-to-assume: ${{ secrets.AWS_GH_OIDC }}
- name: Set Versions
run: |
TFVER=$(grep .tool-versions -e "terraform" | sed "s/terraform \(.*\)/\1/")
echo "TFVERSION=$TFVER" >> $GITHUB_ENV
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: ${{ env.TFVERSION }}
- name: Terraform Format
id: fmt
run: terraform fmt -check -recursive
continue-on-error: true
- name: Terraform Init (test)
id: init-test
run: terraform init
- name: Terraform Test
id: test
run: terraform test -verbose
continue-on-error: true
- name: Terraform Init (examples)
id: init
run: terraform -chdir=${{ env.TFDIR }} init
- name: Terraform Validate
id: validate
run: terraform -chdir=${{ env.TFDIR }} validate -no-color
- name: Terraform Plan
id: plan
run: terraform -chdir=${{ env.TFDIR }} plan -no-color -input=false
continue-on-error: true
- name: Update Pull Request
uses: actions/github-script@v6
if: github.event_name == 'pull_request'
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `#### Terraform Format and Style π\`${{ steps.fmt.outcome }}\`
#### Terraform Initialization βοΈ\`${{ steps.init.outcome }}\`
#### Terraform Plan π\`${{ steps.plan.outcome }}\`
#### Terraform Validation π€\`${{ steps.validate.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`\n
${process.env.PLAN}
\`\`\`
</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
})
# User with write access has commented /ok-to-test on a (fork-based) pull request
terraform-pr-fork:
name: terraform-pr-fork
runs-on: ubuntu-latest
permissions:
checks: write
id-token: write
pull-requests: write
timeout-minutes: 15
env:
TF_IN_AUTOMATION: true
TFDIR: examples
if: |
github.event_name == 'repository_dispatch' &&
github.event.client_payload.slash_command.args.named.sha != '' &&
contains(
github.event.client_payload.pull_request.head.sha,
github.event.client_payload.slash_command.args.named.sha
)
steps:
- name: Fork based /ok-to-test checkout
uses: actions/checkout@v4
with:
ref: 'refs/pull/${{ github.event.client_payload.pull_request.number }}/merge'
- name: Update skipped check run
uses: actions/github-script@v6
env:
job: ${{ github.job }}
number: ${{ github.event.client_payload.pull_request.number }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { data: pull } = await github.rest.pulls.get({
...context.repo,
pull_number: process.env.number
});
const ref = pull.head.sha;
const { data: checks } = await github.rest.checks.listForRef({
...context.repo,
ref
});
// Filter for the check run with a specific name and a 'skipped' conclusion
const check = checks.check_runs.filter(c => c.name === process.env.job && c.conclusion === "skipped");
if (check.length > 0) {
console.log(`Skipped check run found with name: ${check[0].name}`);
console.log(`context.repo: ${JSON.stringify(context.repo, 0, 2)}`);
// Update the check run to 'in_progress'
const { data: result } = await github.rest.checks.update({
...context.repo,
check_run_id: check[0].id,
status: 'in_progress',
conclusion: ''
});
console.log(`update check-run result is: ${JSON.stringify(result, 0, 2)}`);
console.log(`Successfully updated check run to 'in_progress'. Name: ${result.name}`);
return result;
} else {
console.log('No skipped check runs found with the specified name.');
}
- name: Configure AWS credentials
if: startsWith(github.repository, 'ventx/terraform-aws-')
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ secrets.AWS_DEFAULT_REGION }}
role-to-assume: ${{ secrets.AWS_GH_OIDC }}
- name: Set Versions
run: |
TFVER=$(grep .tool-versions -e "terraform" | sed "s/terraform \(.*\)/\1/")
echo "TFVERSION=$TFVER" >> $GITHUB_ENV
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: ${{ env.TFVERSION }}
- name: Terraform Format
id: fmt
run: terraform fmt -check -recursive
continue-on-error: true
- name: Terraform Init (test)
id: init-test
run: terraform init
- name: Terraform Test
id: test
run: terraform test -verbose
continue-on-error: true
- name: Terraform Init (examples)
id: init
run: terraform -chdir=${{ env.TFDIR }} init
- name: Terraform Validate
id: validate
run: terraform -chdir=${{ env.TFDIR }} validate -no-color
- name: Terraform Plan
id: plan
run: terraform -chdir=${{ env.TFDIR }} plan -no-color -input=false
continue-on-error: true
- name: Update Pull Request
uses: actions/github-script@v6
if: github.event_name == 'pull_request'
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `#### Terraform Format and Style π\`${{ steps.fmt.outcome }}\`
#### Terraform Initialization βοΈ\`${{ steps.init.outcome }}\`
#### Terraform Plan π\`${{ steps.plan.outcome }}\`
#### Terraform Validation π€\`${{ steps.validate.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`\n
${process.env.PLAN}
\`\`\`
</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
})
# Update check run called "terraform-fork"
- uses: actions/github-script@v6
id: update-check-run
if: ${{ always() }}
env:
job: ${{ github.job }}
number: ${{ github.event.client_payload.pull_request.number }}
# Conveniently, job.status maps to https://developer.github.com/v3/checks/runs/#update-a-check-run
conclusion: ${{ job.status }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { data: pull } = await github.rest.pulls.get({
...context.repo,
pull_number: process.env.number
});
const ref = pull.head.sha;
const { data: checks } = await github.rest.checks.listForRef({
...context.repo,
ref
});
// Filter for the check run with a specific name and a 'skipped' conclusion
const check = checks.check_runs.filter(c => c.name === process.env.job && c.conclusion === "skipped");
if (check.length > 0) {
console.log(`Skipped check run found with name: ${check[0].name}`);
// Update the check run to 'in_progress'
const { data: result } = await github.rest.checks.update({
...context.repo,
check_run_id: check[0].id,
status: 'completed',
conclusion: process.env.conclusion
});
console.log(`update check-run result is: ${JSON.stringify(result, 0, 2)}`);
console.log(`Successfully updated check run to 'in_progress'. Name: ${result.name}`);
return result;
} else {
console.log('No skipped check runs found with the specified name.');
}