-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
action.yml
531 lines (482 loc) · 24.6 KB
/
action.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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
name: 'GitHub Action Atmos Terraform Plan'
description: 'GitHub Action Atmos Terraform Plan'
author: [email protected]
branding:
icon: 'server'
color: 'white'
inputs:
component:
description: "The name of the component to plan."
required: true
stack:
description: "The stack name for the given component."
required: true
sha:
description: "Commit SHA to plan. Default: github.sha"
required: true
default: "${{ github.event.pull_request.head.sha }}"
drift-detection-mode-enabled:
description: "Indicate whether this action is used in drift detection workflow."
required: true
default: 'false'
atmos-version:
description: The version of atmos to install
required: false
default: ">= 1.99.0"
atmos-config-path:
description: The path to the atmos.yaml file
required: true
infracost-api-key:
description: "Infracost API key"
required: false
metadata-retention-days:
description: "Infracost API key"
required: false
default: "1"
branding-logo-image:
description: "Branding logo image url"
required: false
default: "https://cloudposse.com/logo-300x69.svg"
branding-logo-url:
description: "Branding logo url"
required: false
default: "https://cloudposse.com/"
debug:
description: "Enable action debug mode. Default: 'false'"
default: 'false'
required: false
token:
description:
Used to pull node distributions for Atmos from Cloud Posse's GitHub repository. Since there's a default, this is typically
not supplied by the user. When running this action on github.com, the default value is sufficient. When running on
GHES, you can pass a personal access token for github.com if you are experiencing rate limiting.
default: ${{ github.server_url == 'https://github.com' && github.token || '' }}
skip-checkout:
description: "Disable actions/checkout. Useful for when the checkout happens in a previous step and file are modified outside of git through other actions"
required: false
default: 'false'
pr-comment:
description: "Set to 'true' to create a PR comment with the summary of the plan"
required: false
default: 'false'
outputs:
summary:
description: "Summary"
value: "${{ steps.summary.outputs.result }}"
plan_file:
description: "Path to the terraform plan file"
value: "${{ steps.vars.outputs.plan_file }}"
plan_json:
description: "Path to the terraform plan in JSON format"
value: "${{ steps.vars.outputs.plan_file }}.json"
runs:
using: "composite"
steps:
- name: Checkout
if: ${{ inputs.skip-checkout != 'true' }}
uses: actions/checkout@v4
with:
ref: ${{ inputs.sha }}
- name: Set atmos cli config path vars
shell: bash
run: |-
echo "ATMOS_CLI_CONFIG_PATH=$(realpath ${{ inputs.atmos-config-path }})" >> $GITHUB_ENV
- name: Install Atmos
uses: cloudposse/github-action-setup-atmos@v2
with:
atmos-version: ${{ inputs.atmos-version }}
token: ${{ inputs.token }}
install-wrapper: false
- name: Get atmos settings
id: atmos-settings
uses: cloudposse/github-action-atmos-get-setting@v2
with:
settings: |
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: settings.github.actions_enabled
outputPath: enabled
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: component_info.component_path
outputPath: component-path
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: atmos_cli_config.base_path
outputPath: base-path
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: command
outputPath: command
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: settings.integrations.github.gitops.opentofu-version
outputPath: opentofu-version
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: settings.integrations.github.gitops.terraform-version
outputPath: terraform-version
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: settings.integrations.github.gitops.infracost-enabled
outputPath: enable-infracost
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: settings.integrations.github.gitops.role.plan
outputPath: terraform-plan-role
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: settings.integrations.github.gitops.artifact-storage.region
outputPath: aws-region
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: settings.integrations.github.gitops.artifact-storage.role
outputPath: terraform-state-role
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: settings.integrations.github.gitops.artifact-storage.table
outputPath: terraform-state-table
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: settings.integrations.github.gitops.artifact-storage.bucket
outputPath: terraform-state-bucket
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: settings.integrations.github.gitops.artifact-storage.plan-repository-type
outputPath: plan-repository-type
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: settings.integrations.github.gitops.artifact-storage.blob-account-name
outputPath: blob-account-name
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: settings.integrations.github.gitops.artifact-storage.blob-container-name
outputPath: blob-container-name
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: settings.integrations.github.gitops.artifact-storage.metadata-repository-type
outputPath: metadata-repository-type
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: settings.integrations.github.gitops.artifact-storage.cosmos-container-name
outputPath: cosmos-container-name
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: settings.integrations.github.gitops.artifact-storage.cosmos-database-name
outputPath: cosmos-database-name
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: settings.integrations.github.gitops.artifact-storage.cosmos-endpoint
outputPath: cosmos-endpoint
- name: Install Terraform
if: ${{ fromJson(steps.atmos-settings.outputs.settings).terraform-version != '' && fromJson(steps.atmos-settings.outputs.settings).terraform-version != 'null' }}
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ fromJson(steps.atmos-settings.outputs.settings).terraform-version }}
terraform_wrapper: false
- name: Install Dependencies
uses: cloudposse-github-actions/install-gh-releases@v1
with:
cache: true
config: |-
opentofu/opentofu:
tag: ${{ startsWith(fromJson(steps.atmos-settings.outputs.settings).opentofu-version, 'v') && fromJson(steps.atmos-settings.outputs.settings).opentofu-version || format('v{0}', fromJson(steps.atmos-settings.outputs.settings).opentofu-version) }}
skip: ${{ fromJson(steps.atmos-settings.outputs.settings).opentofu-version == '' || fromJson(steps.atmos-settings.outputs.settings).opentofu-version == 'null' }}
suzuki-shunsuke/tfcmt:
tag: v4.14.0
- name: Configure Plan AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
if: ${{ fromJson(steps.atmos-settings.outputs.settings).aws-region != '' &&
fromJson(steps.atmos-settings.outputs.settings).aws-region != 'null' &&
fromJson(steps.atmos-settings.outputs.settings).terraform-plan-role != '' &&
fromJson(steps.atmos-settings.outputs.settings).terraform-plan-role != 'null' }}
with:
aws-region: ${{ fromJson(steps.atmos-settings.outputs.settings).aws-region }}
role-to-assume: ${{ fromJson(steps.atmos-settings.outputs.settings).terraform-plan-role }}
role-session-name: "atmos-terraform-plan-gitops"
mask-aws-account-id: "no"
- name: Set atmos cli base path vars
if: ${{ fromJson(steps.atmos-settings.outputs.settings).enabled }}
shell: bash
run: |-
# Set ATMOS_BASE_PATH allow `cloudposse/utils` provider to read atmos config from the correct path
ATMOS_BASE_PATH="${{ fromJson(steps.atmos-settings.outputs.settings).base-path }}"
echo "ATMOS_BASE_PATH=$(realpath ${ATMOS_BASE_PATH:-./})" >> $GITHUB_ENV
- name: Prepare Artifacts Directory
if: ${{ fromJson(steps.atmos-settings.outputs.settings).enabled }}
shell: bash
run: |
mkdir -p metadata
- name: Define Job Variables
if: ${{ fromJson(steps.atmos-settings.outputs.settings).enabled }}
id: vars
shell: bash
run: |
STACK_NAME=$(echo "${{ inputs.stack }}" | sed 's#/#_#g')
COMPONENT_PATH=${{ fromJson(steps.atmos-settings.outputs.settings).component-path }}
COMPONENT_NAME=$(echo "${{ inputs.component }}" | sed 's#/#_#g')
COMPONENT_SLUG="$STACK_NAME-$COMPONENT_NAME"
COMPONENT_CACHE_KEY=$(basename "${{ fromJson(steps.atmos-settings.outputs.settings).component-path }}")
PLAN_FILE="$( realpath ${COMPONENT_PATH})/$COMPONENT_SLUG-${{ inputs.sha }}.planfile"
LOCK_FILE="$( realpath ${COMPONENT_PATH})/.terraform.lock.hcl"
ISSUE_SUMMARY_FILE="$GITHUB_WORKSPACE/metadata/issue-description-${COMPONENT_SLUG}.md"
STEP_SUMMARY_FILE="$GITHUB_WORKSPACE/metadata/step-summary-${COMPONENT_SLUG}.md"
SUMMARY_FILE="$GITHUB_WORKSPACE/metadata/summary-${COMPONENT_SLUG}.md"
echo "stack_name=${STACK_NAME}" >> $GITHUB_OUTPUT
echo "component_name=${COMPONENT_NAME}" >> $GITHUB_OUTPUT
echo "component_slug=${COMPONENT_SLUG}" >> $GITHUB_OUTPUT
echo "component_path=${COMPONENT_PATH}" >> $GITHUB_OUTPUT
echo "cache-key=${COMPONENT_CACHE_KEY}" >> $GITHUB_OUTPUT
echo "plan_file=${PLAN_FILE}" >> $GITHUB_OUTPUT
echo "lock_file=${LOCK_FILE}" >> $GITHUB_OUTPUT
echo "summary_file=${SUMMARY_FILE}" >> $GITHUB_OUTPUT
echo "step_summary_file=${STEP_SUMMARY_FILE}" >> $GITHUB_OUTPUT
echo "issue_file=${ISSUE_SUMMARY_FILE}" >> $GITHUB_OUTPUT
- name: Cache .terraform
id: cache
uses: actions/cache@v4
if: ${{ fromJson(steps.atmos-settings.outputs.settings).enabled }}
with:
path: |
./${{ steps.vars.outputs.component_path }}/.terraform
key: ${{ steps.vars.outputs.cache-key }}
- name: Atmos Terraform Plan
if: ${{ fromJson(steps.atmos-settings.outputs.settings).enabled }}
id: atmos-plan
shell: bash
run: |
set +e
# Remove the environment file from the cache to avoid conflicts with workspace select
rm -f ./${{ steps.vars.outputs.component_path }}/.terraform/environment
TERRAFORM_OUTPUT_FILE="./terraform-${GITHUB_RUN_ID}-output.txt"
tfcmt \
--config ${GITHUB_ACTION_PATH}/config/summary.yaml \
-owner "${{ github.repository_owner }}" \
-repo "${{ github.event.repository.name }}" \
-var "target:${{ steps.vars.outputs.component_slug }}" \
-var "component:${{ inputs.component }}" \
-var "componentPath:${{ steps.vars.outputs.component_path }}" \
-var "commitSHA:${{ inputs.sha }}" \
-var "stack:${{ inputs.stack }}" \
-var "job:${{ github.job }}" \
-var "logoImage:${{ inputs.branding-logo-image }}" \
-var "logoUrl:${{ inputs.branding-logo-url }}" \
-var "infracost_enabled:${{ fromJson(steps.atmos-settings.outputs.settings).enable-infracost }}" \
-var "driftModeEnabled:${{ inputs.drift-detection-mode-enabled }}" \
$([[ "${{ inputs.pr-comment }}" == "false" ]] && echo "--output ${{ steps.vars.outputs.summary_file }}") \
--log-level $([[ "${{ inputs.debug }}" == "true" ]] && echo "DEBUG" || echo "INFO") \
plan \
$([[ "${{ inputs.pr-comment }}" == "true" ]] && echo "-patch") \
-- \
atmos terraform plan ${{ inputs.component }} \
--stack ${{ inputs.stack }} \
-out="${{ steps.vars.outputs.plan_file }}" \
-lock=false \
-input=false \
-no-color \
&> ${TERRAFORM_OUTPUT_FILE}
TERRAFORM_RESULT=$?
if [[ "${{ inputs.pr-comment }}" == "true" ]]; then
tfcmt \
--config ${GITHUB_ACTION_PATH}/config/summary.yaml \
-owner "${{ github.repository_owner }}" \
-repo "${{ github.event.repository.name }}" \
-var "target:${{ steps.vars.outputs.component_slug }}" \
-var "component:${{ inputs.component }}" \
-var "componentPath:${{ steps.vars.outputs.component_path }}" \
-var "commitSHA:${{ inputs.sha }}" \
-var "stack:${{ inputs.stack }}" \
-var "job:${{ github.job }}" \
-var "logoImage:${{ inputs.branding-logo-image }}" \
-var "logoUrl:${{ inputs.branding-logo-url }}" \
-var "infracost_enabled:${{ fromJson(steps.atmos-settings.outputs.settings).enable-infracost }}" \
-var "driftModeEnabled:${{ inputs.drift-detection-mode-enabled }}" \
--output ${{ steps.vars.outputs.summary_file }} \
--log-level $([[ "${{ inputs.debug }}" == "true" ]] && echo "DEBUG" || echo "INFO") \
plan \
-- \
bash -c "cat ${TERRAFORM_OUTPUT_FILE}"
fi
set -e
if [[ "${{ inputs.drift-detection-mode-enabled }}" == "true" ]]; then
# Split summary to 2 files - issue and step summary files
# Remove \0 at the end of the grep output
grep -Pzo '(.|\n)*(?=_______________ISSUE-MARKDOWN_______________\n)' ${{ steps.vars.outputs.summary_file }} | grep -Pav "\x00" > ${{ steps.vars.outputs.step_summary_file }}
grep -Pzo '(?<=_______________ISSUE-MARKDOWN_______________\n)(.|\n)*' ${{ steps.vars.outputs.summary_file }} | grep -Pav "\x00" > ${{ steps.vars.outputs.issue_file }}
rm -f ${{ steps.vars.outputs.summary_file }}
else
mv ${{ steps.vars.outputs.summary_file }} ${{ steps.vars.outputs.step_summary_file }}
fi
cat "${TERRAFORM_OUTPUT_FILE}"
HAS_CHANGES=false
HAS_NO_CHANGES=false
HAS_ERROR=false
if [[ "${TERRAFORM_RESULT}" == "0" ]]; then
if grep -q '^No changes. Your infrastructure matches the configuration.' ${TERRAFORM_OUTPUT_FILE} ; then
echo "No changes found"
HAS_NO_CHANGES=true
else
HAS_CHANGES=true
echo "Found changes"
fi
else
HAS_ERROR=true
echo "Error appears"
fi
# One of changes, no-changes, error should be true
echo "changes=${HAS_CHANGES}" >> $GITHUB_OUTPUT
echo "no-changes=${HAS_NO_CHANGES}" >> $GITHUB_OUTPUT
echo "error=${HAS_ERROR}" >> $GITHUB_OUTPUT
echo "result=${TERRAFORM_RESULT}" >> $GITHUB_OUTPUT
rm -f ${TERRAFORM_OUTPUT_FILE}
- name: Configure State AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
if: ${{ ( fromJson(steps.atmos-settings.outputs.settings).plan-repository-type == 's3' ||
fromJson(steps.atmos-settings.outputs.settings).plan-repository-type == '' ||
fromJson(steps.atmos-settings.outputs.settings).plan-repository-type == 'null' ) &&
steps.atmos-plan.outputs.error == 'false' &&
fromJson(steps.atmos-settings.outputs.settings).aws-region != '' &&
fromJson(steps.atmos-settings.outputs.settings).aws-region != 'null' &&
fromJson(steps.atmos-settings.outputs.settings).terraform-state-role != '' &&
fromJson(steps.atmos-settings.outputs.settings).terraform-state-role != 'null' }}
with:
aws-region: ${{ fromJson(steps.atmos-settings.outputs.settings).aws-region }}
role-to-assume: ${{ fromJson(steps.atmos-settings.outputs.settings).terraform-state-role }}
role-session-name: "atmos-terraform-state-gitops"
mask-aws-account-id: "no"
- name: Store New Plan
if: ${{ steps.atmos-plan.outputs.error == 'false' }}
uses: cloudposse/github-action-terraform-plan-storage@v1
with:
action: storePlan
commitSHA: ${{ inputs.sha }}
planPath: ${{ steps.vars.outputs.plan_file }}
component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
planRepositoryType: ${{ fromJson(steps.atmos-settings.outputs.settings).plan-repository-type || 's3' }}
metadataRepositoryType: ${{ fromJson(steps.atmos-settings.outputs.settings).metadata-repository-type || 'dynamo' }}
blobAccountName: ${{ fromJson(steps.atmos-settings.outputs.settings).blob-account-name }}
blobContainerName: ${{ fromJson(steps.atmos-settings.outputs.settings).blob-container-name }}
cosmosContainerName: ${{ fromJson(steps.atmos-settings.outputs.settings).cosmos-container-name }}
cosmosDatabaseName: ${{ fromJson(steps.atmos-settings.outputs.settings).cosmos-database-name }}
cosmosEndpoint: ${{ fromJson(steps.atmos-settings.outputs.settings).cosmos-endpoint }}
tableName: ${{ fromJson(steps.atmos-settings.outputs.settings).terraform-state-table }}
bucketName: ${{ fromJson(steps.atmos-settings.outputs.settings).terraform-state-bucket }}
- name: Store Lockfile for New Plan
if: ${{ steps.atmos-plan.outputs.error == 'false' }}
uses: cloudposse/github-action-terraform-plan-storage@v1
with:
action: storePlan
commitSHA: ${{ inputs.sha }}
planPath: ${{ steps.vars.outputs.lock_file }}
component: ${{ inputs.component }}
stack: ${{ inputs.stack }}-lockfile
planRepositoryType: ${{ fromJson(steps.atmos-settings.outputs.settings).plan-repository-type || 's3' }}
metadataRepositoryType: ${{ fromJson(steps.atmos-settings.outputs.settings).metadata-repository-type || 'dynamo' }}
blobAccountName: ${{ fromJson(steps.atmos-settings.outputs.settings).blob-account-name }}
blobContainerName: ${{ fromJson(steps.atmos-settings.outputs.settings).blob-container-name }}
cosmosContainerName: ${{ fromJson(steps.atmos-settings.outputs.settings).cosmos-container-name }}
cosmosDatabaseName: ${{ fromJson(steps.atmos-settings.outputs.settings).cosmos-database-name }}
cosmosEndpoint: ${{ fromJson(steps.atmos-settings.outputs.settings).cosmos-endpoint }}
tableName: ${{ fromJson(steps.atmos-settings.outputs.settings).terraform-state-table }}
bucketName: ${{ fromJson(steps.atmos-settings.outputs.settings).terraform-state-bucket }}
- name: Setup Infracost
if: ${{ fromJson(steps.atmos-settings.outputs.settings).enable-infracost == true && steps.atmos-plan.outputs.changes == 'true' }}
uses: infracost/actions/setup@v3
with:
api-key: ${{ inputs.infracost-api-key }}
- name: Convert PLANFILE to JSON
if: ${{ fromJson(steps.atmos-settings.outputs.settings).enable-infracost == true && steps.atmos-plan.outputs.changes == 'true' }}
shell: bash
working-directory: ./${{ steps.vars.outputs.component_path }}
run: |
${{ fromJson(steps.atmos-settings.outputs.settings).command }} show -json "${{ steps.vars.outputs.plan_file }}" > "${{ steps.vars.outputs.plan_file }}.json"
- name: Generate Infracost Diff
if: ${{ fromJson(steps.atmos-settings.outputs.settings).enable-infracost == true && steps.atmos-plan.outputs.changes == 'true' }}
id: infracost
shell: bash
run: |
echo "Running Infracost diff"
infracost diff \
--path="${{ steps.vars.outputs.plan_file }}.json" \
--format=diff \
--project-name "${{ inputs.stack }}-${{ inputs.component }}" \
--out-file=/tmp/infracost.txt
infracost diff \
--path="${{ steps.vars.outputs.plan_file }}.json" \
--format=json \
--project-name "${{ inputs.stack }}-${{ inputs.component }}" \
--out-file=/tmp/infracost.json
echo "finished=true" >> $GITHUB_OUTPUT
- name: Debug Infracost
if: ${{ steps.infracost.outputs.finished == 'true' && inputs.debug == 'true' }}
shell: bash
run: |
cat ${{ steps.vars.outputs.plan_file }}.json
cat /tmp/infracost.txt
cat /tmp/infracost.json
- name: Set Infracost Variables
if: ${{ steps.infracost.outputs.finished == 'true' }}
id: infracost-diff
shell: bash
run: |
INFRACOST_DIFF_TOTAL_MONTHLY_COST=$(cat /tmp/infracost.json | jq --raw-output .diffTotalMonthlyCost)
INFRACOST_DETAILS_DIFF_BREAKDOWN="$(cat /tmp/infracost.txt)"
if [[ "${INFRACOST_DIFF_TOTAL_MONTHLY_COST}" == "0" ]]; then
INFRA_COST_SUMMARY="Infracost Estimate: monthly cost will not change"
elif [[ "${INFRACOST_DIFF_TOTAL_MONTHLY_COST:0:1}" == "-" ]]; then
INFRA_COST_SUMMARY="Infracost Estimate: monthly cost will decrease by ${INFRACOST_DIFF_TOTAL_MONTHLY_COST} 📉"
else
INFRA_COST_SUMMARY="Infracost Estimate: monthly cost will increase by ${INFRACOST_DIFF_TOTAL_MONTHLY_COST} 📈"
fi
sed -i "s/%INFRACOST_SUMMARY%/${INFRA_COST_SUMMARY}/" ${{ steps.vars.outputs.step_summary_file }}
sed -i -e '/%INFRACOST_DIFF%/{r /tmp/infracost.txt' -e 'd}' ${{ steps.vars.outputs.step_summary_file }}
- name: Store Component Metadata to Artifacts
if: ${{ fromJson(steps.atmos-settings.outputs.settings).enabled && inputs.drift-detection-mode-enabled == 'true' }}
shell: bash
run: |
echo -n '{ "stack": "${{ inputs.stack }}", "component": "${{ inputs.component }}", "componentPath": "${{ steps.vars.outputs.component_path }}", "drifted": '"${{ steps.atmos-plan.outputs.changes }}"', "error": '"${{ steps.atmos-plan.outputs.error }}"' }' > "metadata/${{ steps.vars.outputs.component_slug }}.metadata.json"
- name: Publish Summary or Generate GitHub Issue Description for Drift Detection
id: summary
shell: bash
working-directory: ./${{ steps.vars.outputs.component_path }}
run: |
if [[ "${{ inputs.drift-detection-mode-enabled }}" == "true" ]]; then
STEP_SUMMARY_FILE="${{ steps.vars.outputs.issue_file }}"
if [[ "${{ steps.atmos-plan.outputs.no-changes }}" == "true" ]]; then
rm -f ${STEP_SUMMARY_FILE}
fi
else
STEP_SUMMARY_FILE="${{ steps.vars.outputs.step_summary_file }}"
fi
if [ -f ${STEP_SUMMARY_FILE} ]; then
echo "${STEP_SUMMARY_FILE} found"
STEP_SUMMARY=$(cat ${STEP_SUMMARY_FILE} | jq -Rs .)
echo "result=${STEP_SUMMARY}" >> $GITHUB_OUTPUT
if [[ "${{ inputs.drift-detection-mode-enabled }}" == "false" ]]; then
echo "Drift detection mode disabled"
cat $STEP_SUMMARY_FILE >> $GITHUB_STEP_SUMMARY
fi
else
echo "${STEP_SUMMARY_FILE} not found"
echo "result=\"\"" >> $GITHUB_OUTPUT
fi
- name: Generate a unique id
id: generate
shell: bash
run: |
echo "rand=$(openssl rand -hex 5)" >> "$GITHUB_OUTPUT"
- name: Upload Artifacts
if: ${{ fromJson(steps.atmos-settings.outputs.settings).enabled && inputs.drift-detection-mode-enabled == 'true' }}
uses: actions/upload-artifact@v4
with:
# The name of the artifact needs to be unique for every job run!
# This name is filtered in cloudposse/github-action-atmos-terraform-drift-detection by the "metadata-*" prefix
name: metadata-${{ github.run_id }}-${{ steps.generate.outputs.rand }}
path: metadata
retention-days: ${{ inputs.metadata-retention-days }}
- name: Exit status
if: ${{ fromJson(steps.atmos-settings.outputs.settings).enabled }}
shell: bash
run: |
exit ${{ steps.atmos-plan.outputs.result }}