-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
action.yml
440 lines (402 loc) · 20.4 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
name: 'GitHub Action Atmos Terraform Apply'
description: 'GitHub Action Atmos Terraform Apply'
author: [email protected]
branding:
icon: 'server'
color: 'white'
inputs:
component:
description: "The name of the component to apply."
required: true
stack:
description: "The stack name for the given component."
required: true
sha:
description: "Commit SHA to apply. Default: github.sha"
required: true
default: "${{ github.event.pull_request.head.sha }}"
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'
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
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 || '' }}
outputs:
status:
description: Apply Status. Either 'succeeded' or 'failed'
value: ${{ steps.apply.outputs.status }}
runs:
using: "composite"
steps:
- name: Install Node
uses: actions/setup-node@v4
with:
node-version: 20
- name: Checkout
if: ${{ inputs.skip-checkout != 'true' }}
uses: actions/checkout@v4
- 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.role.apply
outputPath: terraform-apply-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: v4.14.0
terraform-docs/terraform-docs: v0.18.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-apply-role != '' &&
fromJson(steps.atmos-settings.outputs.settings).terraform-apply-role != 'null' }}
with:
aws-region: ${{ fromJson(steps.atmos-settings.outputs.settings).aws-region }}
role-to-assume: ${{ fromJson(steps.atmos-settings.outputs.settings).terraform-apply-role }}
role-session-name: "atmos-terraform-apply-gitops"
mask-aws-account-id: "no"
- name: Define Job Control State Variables
shell: bash
run: |
echo "DEBUG_ENABLED=${{ inputs.debug }}" >> $GITHUB_ENV
echo "ACTIONS_ENABLED=false" >> $GITHUB_ENV
echo "INFRACOST_ENABLED=false" >> $GITHUB_ENV
- name: Check If GitHub Actions is Enabled For Component
shell: bash
run: |
if [[ "${{ fromJson(steps.atmos-settings.outputs.settings).enabled }}" == "true" ]]; then
echo "ACTIONS_ENABLED=true" >> $GITHUB_ENV
else
echo "ACTIONS_ENABLED=false" >> $GITHUB_ENV
fi
- name: Set atmos cli base path vars
if: env.ACTIONS_ENABLED == 'true'
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: Define Job Variables
if: env.ACTIONS_ENABLED == 'true'
id: vars
shell: bash
run: |
STACK_NAME=$(echo "${{ inputs.stack }}" | sed 's#/#_#g')
COMPONENT_PATH=$( realpath ${{ 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="${COMPONENT_PATH}/$COMPONENT_SLUG-${{ inputs.sha }}.planfile"
LOCK_FILE="${COMPONENT_PATH}/.terraform.lock.hcl"
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
- name: Configure Plan 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' ) &&
env.ACTIONS_ENABLED == 'true' &&
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: Retrieve Plan
if: ${{ env.ACTIONS_ENABLED == 'true' }}
uses: cloudposse/github-action-terraform-plan-storage@v1
continue-on-error: true
with:
action: getPlan
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: Retrieve Lockfile
if: ${{ env.ACTIONS_ENABLED == 'true' }}
uses: cloudposse/github-action-terraform-plan-storage@v1
continue-on-error: true
with:
action: getPlan
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: 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-apply-role != '' &&
fromJson(steps.atmos-settings.outputs.settings).terraform-apply-role != 'null' }}
with:
aws-region: ${{ fromJson(steps.atmos-settings.outputs.settings).aws-region }}
role-to-assume: ${{ fromJson(steps.atmos-settings.outputs.settings).terraform-apply-role }}
role-session-name: "atmos-terraform-apply-gitops"
mask-aws-account-id: "no"
- name: Check Whether Infracost is Enabled
if: env.ACTIONS_ENABLED == 'true'
shell: bash
run: |
if [[ "${{ fromJson(steps.atmos-settings.outputs.settings).enable-infracost }}" == "true" ]]; then
echo "INFRACOST_ENABLED=true" >> $GITHUB_ENV
else
echo "INFRACOST_ENABLED=false" >> $GITHUB_ENV
fi
- name: Setup Infracost
if: env.INFRACOST_ENABLED == '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: env.INFRACOST_ENABLED == 'true'
shell: bash
run: |
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
- name: Debug Infracost
if: env.INFRACOST_ENABLED == 'true' && env.DEBUG_ENABLED == 'true'
shell: bash
run: |
cat ${{ steps.vars.outputs.plan_file }}.json
cat /tmp/infracost.txt
cat /tmp/infracost.json
- name: Set Infracost Variables
if: env.INFRACOST_ENABLED == 'true'
id: infracost-diff
shell: bash
run: |
if [[ "${{ fromJson(steps.atmos-settings.outputs.settings).enable-infracost }}" == "true" ]]; then
INFRACOST_DIFF_TOTAL_MONTHLY_COST=$(cat /tmp/infracost.json | jq --raw-output .diffTotalMonthlyCost)
INFRACOST_DETAILS_DIFF_BREAKDOWN="$(cat /tmp/infracost.txt | base64 --wrap 0)"
else
INFRACOST_DIFF_TOTAL_MONTHLY_COST="0"
INFRACOST_DETAILS_DIFF_BREAKDOWN=""
fi
echo "infracost_details_diff_breakdown=$INFRACOST_DETAILS_DIFF_BREAKDOWN" >> "$GITHUB_OUTPUT"
echo "infracost_diff_total_monthly_cost=$INFRACOST_DIFF_TOTAL_MONTHLY_COST" >> "$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: Terraform Apply
if: env.ACTIONS_ENABLED == 'true'
id: apply
shell: bash
working-directory: ${{ steps.vars.outputs.component_path }}
run: |
set +e
# Remove the environment file from the cache to avoid conflicts with workspace select
rm -f ./.terraform/environment
TERRAFORM_OUTPUT_FILE="./terraform-${GITHUB_RUN_ID}-output.txt"
tfcmt \
--config "${GITHUB_ACTION_PATH}/config/atmos_github_summary.yaml" \
-var "target:${{ inputs.stack }}-${{ inputs.component }}" \
-var "component:${{ inputs.component }}" \
-var "stack:${{ inputs.stack }}" \
-var "job:${{ github.job }}" \
-var "infracost_details_diff_breakdown:${{ steps.infracost-diff.outputs.infracost_details_diff_breakdown }}" \
-var "infracost_total_monthly_cost:${{ steps.infracost-diff.outputs.infracost_diff_total_monthly_cost }}" \
-var "logoImage:${{ inputs.branding-logo-image }}" \
-var "logoUrl:${{ inputs.branding-logo-url }}" \
--output "${{ github.workspace }}/atmos-apply-summary.md" \
--log-level $([[ "${{ inputs.debug }}" == "true" ]] && echo "DEBUG" || echo "INFO") \
apply -- \
atmos terraform apply ${{ inputs.component }} \
--stack ${{ inputs.stack }} \
-auto-approve \
-input=false \
-no-color \
&> ${TERRAFORM_OUTPUT_FILE}
TERRAFORM_RESULT=$?
set -e
cat "${TERRAFORM_OUTPUT_FILE}"
atmos terraform output ${{ inputs.component }} --stack ${{ inputs.stack }} --skip-init -- -json 1> output_values.json
terraform-docs -c ${GITHUB_ACTION_PATH}/config/tfdocs-config.yaml --output-file ${{ github.workspace }}/atmos-apply-summary.md ./
sed -i "s#\`<sensitive>\`#![Sensitive](https://img.shields.io/badge/sensitive-c40000?style=for-the-badge)#g" ${{ github.workspace }}/atmos-apply-summary.md
sed -i "s#\`\"#\`#g" ${{ github.workspace }}/atmos-apply-summary.md
sed -i "s#\"\`#\`#g" ${{ github.workspace }}/atmos-apply-summary.md
sed -i "s#|--#|:-#g" ${{ github.workspace }}/atmos-apply-summary.md
cat "${{ github.workspace }}/atmos-apply-summary.md" >> $GITHUB_STEP_SUMMARY
if [[ "${TERRAFORM_RESULT}" == "0" ]]; then
echo "status=succeeded" >> $GITHUB_OUTPUT
echo "Terraform apply executed successfully"
else
echo "status=failed" >> $GITHUB_OUTPUT
echo "Terraform apply failed"
fi
# Link to a job that executed this action
echo "[Job](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})" >> "${{ github.workspace }}/atmos-apply-summary.md"
rm -f ${TERRAFORM_OUTPUT_FILE}
exit $TERRAFORM_RESULT