Skip to content

Commit 3d0cf9f

Browse files
authored
ci: terraform output and interpolation fixups (#110)
1 parent da990a6 commit 3d0cf9f

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

.github/workflows/tf.yml

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ jobs:
118118
role-to-assume: ${{ env.CONFIGURE_AWS_ROLE }}
119119

120120
- name: Add PR label
121+
if: (!contains(join(github.event.pull_request.labels.*.name), format('terraform:{0}', matrix.in['terraform'])))
121122
uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1
122123
with:
123124
script: |
@@ -149,6 +150,7 @@ jobs:
149150
echo TF_CLI_CHDIR=$(echo ${{ matrix.in['chdir'] != '' && format('-chdir={0}{1}', env.CONFIGURE_TF_CHDIR_PREFIX, matrix.in['chdir']) || env.CONFIGURE_TF_CHDIR_PREFIX != '' && format('-chdir={0}', env.CONFIGURE_TF_CHDIR_PREFIX) || '' }} | sed 's/ /\\ /g') >> $GITHUB_ENV
150151
echo TF_CLI_PATH=$(echo ${{ matrix.in['chdir'] != '' && format('{0}{1}', env.CONFIGURE_TF_CHDIR_PREFIX, matrix.in['chdir']) || env.CONFIGURE_TF_CHDIR_PREFIX != '' && format('{0}', env.CONFIGURE_TF_CHDIR_PREFIX) || '' }}) >> $GITHUB_ENV
151152
echo TF_CLI_TFPLAN=$(echo ${{ format('{0} {1} {2} {3} {4} {5} {6} {7} tfplan', github.event.number || github.event.issue.number, matrix.in['backend-config'], matrix.in['chdir'], matrix.in['destroy'], matrix.in['replace'], matrix.in['target'], matrix.in['var-file'], matrix.in['workspace']) }} | sed 's/[[:space:][:punct:]]/-/g') >> $GITHUB_ENV
153+
echo TF_CLI_VAR_FILE=$(echo ${{ matrix.in['var-file'] != '' && matrix.in['terraform'] == 'plan' && format('-var-file={0}', matrix.in['var-file']) || matrix.in['var-file'] != '' && matrix.in['terraform'] == 'apply' && matrix.in['auto-approve'] != '' && format('-var-file={0}', matrix.in['var-file']) || '' }} | sed 's/ /\\ /g') >> $GITHUB_ENV
152154
# Commands
153155
echo TF_CLI_LOCK_ID=$(echo ${{ matrix.in['lock-id'] }} | sed 's/ /\\ /g') >> $GITHUB_ENV
154156
echo TF_CLI_WORKSPACE=$(echo ${{ matrix.in['workspace'] }} | sed 's/ /\\ /g') >> $GITHUB_ENV
@@ -166,7 +168,6 @@ jobs:
166168
echo TF_CLI_REFRESH=$(echo ${{ matrix.in['refresh'] != '' && format('-refresh={0}', matrix.in['refresh']) || '' }} | sed 's/ /\\ /g') >> $GITHUB_ENV
167169
echo TF_CLI_REPLACE=$(echo ${{ matrix.in['replace'] != '' && format('-replace={0}', matrix.in['replace']) || '' }} | sed 's/ /\\ /g') >> $GITHUB_ENV
168170
echo TF_CLI_TARGET=$(echo ${{ matrix.in['target'] != '' && format('-target={0}', matrix.in['target']) || '' }} | sed 's/ /\\ /g') >> $GITHUB_ENV
169-
echo TF_CLI_VAR_FILE=$(echo ${{ matrix.in['var-file'] != '' && format('-var-file={0}', matrix.in['var-file']) || '' }} | sed 's/ /\\ /g') >> $GITHUB_ENV
170171
# Boolean
171172
echo TF_CLI_COMPACT_WARNINGS=$(echo ${{ matrix.in['compact-warnings'] != '' && '-compact-warnings' || '' }} | sed 's/ /\\ /g') >> $GITHUB_ENV
172173
echo TF_CLI_DESTROY=$(echo ${{ matrix.in['destroy'] != '' && '-destroy' || '' }} | sed 's/ /\\ /g') >> $GITHUB_ENV
@@ -237,20 +238,31 @@ jobs:
237238
terraform_output: ${{ steps.terraform_apply.outputs.stderr || steps.terraform_apply.outputs.stdout || steps.terraform_plan.outputs.stderr || steps.terraform_plan.outputs.stdout || steps.terraform_force_unlock.outputs.stderr || steps.terraform_force_unlock.outputs.stdout || steps.terraform_workspace.outputs.stderr || steps.terraform_workspace.outputs.stdout || steps.terraform_init.outputs.stderr || steps.terraform_init.outputs.stdout }}
238239
with:
239240
script: |
240-
// Store only the first 64800 characters of the terraform output due to GitHub comment's character limit of 65536.
241-
const terraform_output = process.env.terraform_output.substring(0, 64800);
241+
// From the terraform output, remove any lines which are related to reading or refreshing state, and then
242+
// store only the first 64800 characters of the output due to GitHub comment's character limit of 65536.
243+
const terraform_output = process.env.terraform_output
244+
.split("\n")
245+
.filter(
246+
(line) =>
247+
!(
248+
line.includes(": Reading...") ||
249+
line.includes(": Read complete after") ||
250+
line.includes(": Refreshing state...")
251+
)
252+
)
253+
.join("\n")
254+
.substring(0, 64800);
242255
243256
// Display the terraform output change summary as the collapsible content's title.
244-
const comment_summary =
245-
terraform_output
246-
.split("\n")
247-
.find(
248-
(line) =>
249-
line.startsWith("Apply") ||
250-
line.startsWith("Plan") ||
251-
line.startsWith("Error") ||
252-
line.startsWith("No changes")
253-
) || "Terraform output.";
257+
const comment_summary = terraform_output
258+
.split("\n")
259+
.find(
260+
(line) =>
261+
line.startsWith("Plan") ||
262+
line.startsWith("Apply") ||
263+
line.startsWith("Error") ||
264+
line.startsWith("No changes")
265+
) || "Terraform output.";
254266
255267
// Display the terraform command authorship before the terraform output as the collapsible content's body.
256268
// Include the TFPLAN name in the hidden footer as a unique identifier for comment updates.
@@ -260,7 +272,9 @@ jobs:
260272
261273
###### ${{ github.workflow }} by @${{ github.actor }} via [${{ github.event_name }}](${{ github.event.repository.html_url }}/actions/runs/${{ github.run_id }}) at ${{ github.event.pull_request.updated_at || github.event.comment.updated_at }}.</summary>
262274
263-
\`\`\`hcl${terraform_output}\`\`\`
275+
\`\`\`hcl
276+
${terraform_output}
277+
\`\`\`
264278
</details>
265279
<!-- ${process.env.TF_CLI_TFPLAN} -->`;
266280

0 commit comments

Comments
 (0)