-
Notifications
You must be signed in to change notification settings - Fork 4
237 lines (207 loc) · 8.14 KB
/
Copy pathtf-plan.yaml
File metadata and controls
237 lines (207 loc) · 8.14 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
name: Terraform Plan
on:
workflow_call:
inputs:
commit_sha:
type: string
description: 'The commit SHA to promote'
required: true
gcss_ref:
type: string
description: "GCSS ref to checkout"
required: false
default: "main"
tfc_org:
type: string
description: 'The Terraform Cloud organization'
required: true
secrets:
app_private_key:
required: true
gh_token:
required: true
tfc_token:
required: true
jobs:
terraform-plan:
runs-on: ubuntu-latest
environment: plan
permissions:
pull-requests: write
contents: read
steps:
- name: Generate a token
uses: actions/create-github-app-token@v2
id: generate-token
with:
app-id: ${{ vars.APP_ID }}
private-key: ${{ secrets.app_private_key }}
owner: ${{ github.repository_owner }}
- name: Create in-progress check-run
uses: actions/github-script@v7
env:
COMMIT_SHA: ${{ inputs.commit_sha }}
with:
github-token: ${{ steps.generate-token.outputs.token }}
script: |
const detailsUrl = `${context.serverUrl}/${context.payload.repository.full_name}/actions/runs/${context.runId}`;
await github.rest.checks.create({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
name: "Terraform plan",
head_sha: process.env.COMMIT_SHA,
status: "in_progress",
details_url: detailsUrl,
output: {
title: "Terraform Plan running",
summary: `Follow workflow logs for details: ${detailsUrl}`
}
});
- name: Checkout GCSS
uses: actions/checkout@v6
with:
repository: G-Research/github-terraformer
ref: ${{ inputs.gcss_ref }}
persist-credentials: false
- name: GCSS config setup
uses: ./.github/actions/gcss-config-setup
with:
checkout-sha: ${{ inputs.commit_sha }}
checkout-token: ${{ secrets.gh_token }}
- name: Setup terraform and run plan
uses: ./.github/actions/graformer
id: graformer
with:
tfc-token: ${{ secrets.tfc_token }}
tfc-organization: ${{ inputs.tfc_org }}
tfc-workspace: ${{ vars.WORKSPACE }}
- name: Post plan summary and handle check-run
if: always()
uses: actions/github-script@v8
env:
PLAN_SUMMARY: ${{ steps.graformer.outputs.plan-summary }}
PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }}
PLAN_EXITCODE: ${{ steps.graformer.outputs.plan-exitcode }}
COMMIT_SHA: ${{ inputs.commit_sha }}
with:
github-token: ${{ steps.generate-token.outputs.token }}
script: |
function isPlanSuccessful() {
let error = false;
let planSummary = null;
if(process.env.PLAN_EXITCODE === "1"){
error = true;
} else {
try {
planSummary = JSON.parse(process.env.PLAN_SUMMARY);
} catch(e){
error = true;
console.log("Error parsing plan summary:", e);
}
}
return { error, planSummary };
}
function formatResources(arr) {
return arr?.length ? arr.join("\n") : "";
}
const detailsUrl = `${context.serverUrl}/${context.payload.repository.full_name}/actions/runs/${context.runId}`;
let conclusion, title, summary, text, body;
const { error, planSummary } = isPlanSuccessful();
if (error) {
conclusion = "failure";
title = "Terraform Plan Failed";
summary = `The Terraform plan did not complete successfully. Please check workflow logs for details: ${detailsUrl}`;
text = "Error running terraform plan";
body = `Error running terraform plan. Please check workflow logs for details: ${detailsUrl}`;
} else {
const recreateCount = planSummary.recreate?.length || 0;
const updateCount = planSummary.update?.length || 0;
const importCount = planSummary.import?.length || 0;
const createCount = (planSummary.create?.length || 0) + recreateCount;
const deleteCount = (planSummary.delete?.length || 0) + recreateCount;
// build body dynamically. indentation inside if statements is important because if text is indented as it should be (4 spaces), it will be rendered as code block in markdown
body = `
### Terraform Plan Result
Terraform plan: ${importCount} to import, ${createCount} to add, ${updateCount} to change, ${deleteCount} to destroy.
`;
if (deleteCount > 0) {
body += `
Resources to delete:
\`\`\`
${formatResources(planSummary?.delete)}
\`\`\`
`;
}
if (createCount > 0) {
body += `
Resources to create:
\`\`\`
${formatResources(planSummary?.create)}
\`\`\`
`;
}
if (updateCount > 0) {
body += `
Resources to update:
\`\`\`
${formatResources(planSummary?.update)}
\`\`\`
`;
}
if (importCount > 0) {
body += `
Resources to import:
\`\`\`
${formatResources(planSummary?.import)}
\`\`\`
`;
}
if (recreateCount > 0) {
body += `
Resources to recreate:
\`\`\`
${formatResources(planSummary?.recreate)}
\`\`\`
`;
}
body = body.trim();
conclusion = "success";
switch (process.env.PLAN_EXITCODE) {
case "0":
title = "Terraform Plan Succeeded (no changes)";
summary = `Terraform executed successfully with **no changes** required. Please check workflow logs for details: ${detailsUrl}`;
text = "No infrastructure changes detected";
break;
case "2":
title = "Terraform Plan Succeeded (with changes)";
summary = `Terraform executed successfully and **changes are required**. Please check workflow logs for details: ${detailsUrl}`;
text = body;
if (text.length > 65535) {
text = "Log output is too big. Please check the workflow logs for details.";
}
break;
}
}
const prNumber = process.env.PR_NUMBER;
if (prNumber) {
await github.rest.issues.createComment({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
issue_number: prNumber,
body,
});
}
await github.rest.checks.create({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
name: "Terraform plan",
head_sha: process.env.COMMIT_SHA,
status: "completed",
conclusion: conclusion,
details_url: detailsUrl,
output: {
title: title,
summary: summary,
text: text
}
});