terraform-pr-commenter
terraform-pr-commenter copied to clipboard
Run `show` internally; fix yaml diffs
Fixes #27 and #8
It's a little rough around the edges but this fork does three big things:
- for the case of a
plan
, it runsterraform show
internally to build its comments; this saves us from the 64k character limit - for the case of a
plan
whose output exceeds 64k, it splits the plan comment into as many as are necessary to capture all the output (and, correspondingly, it deletes all matching plan comments now, instead of just the first) - any lines in the plan diff starting with a red
-
are known to be diff-removal lines; lines with a non-red-
are known to be text contents; therefore when we find a red one, we temporarily swap it out for an intermediate representation (a literal 😅 in high unicode) before swapping it back at the end in the final diff.
It now accepts a terraform version argument (for plan compatibility purposes) and it expects auth to be available on the environment (to an s3 backend; other cloud providers not tested). Further, it assumes that if you have a plan, it is named tfplan
. I didn't want to mess with the externally-facing API, so I leave it to @robburger to decide how to accept a plan
file name and what to do with the COMMENTER_INPUT env var that I have used to make the command line invocation more legible.
I apologize for the commit noise; I forgot to set the merge method to "squash" when I forked/merged a couple PRs on my end.
@robburger can you take a look at this?
@robburger would be great if this one will be merged
@robburger please take a look at this
Refactored the bash into functions, cleaned up the comment count, and added a "Changes to Outputs" comment with a flag to enable it.
Problem: usage gets a little messier if you do have really long diffs. The step that generates the plan is responsible for trimming the output string to avoid passing a too-long variable into bash on the next step.
We must continue to propagate the text output from the plan step for the "plan failed" case, but handling that case brings back the "argument too long" problems on the success case. So we bite the bullet and handle it.
- name: TF Plan - Run
id: plan
# have to use /bin/bash because GHA runs by default with `set -e` to end execution on any error.
# we want to capture the error instead.
shell: '/bin/bash {0}'
run: |
# a lot going on here. copy the stdout file handle to fd5.
exec 5>&1
# merge stderr into stdout and print it to fd5 (parent shell's stdout); exit with the code from terraform plan
OUTPUT=$(terraform plan -lock=false -input=false -out=tfplan 2>&1 | tee /dev/fd/5; exit ${PIPESTATUS[0]})
# store the exit code here
EXITCODE=$?
# github actions doesn't allow us to set a multiline output so we cat it to the environment
echo "PLAN_OUTPUT<<EOF" >> $GITHUB_ENV
echo "${OUTPUT::65000}" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
# set exit code for pickup later, and make sure we exit with same code
echo "::set-output name=exit::${EXITCODE}"
exit $EXITCODE
- name: Post TF Plan Comment
if: ${{ always() && github.ref != format('refs/heads/{0}', github.event.repository.default_branch) && inputs.enable_plan_commenter == 'true' }}
uses: GetTerminus/terraform-pr-commenter@master
env:
GITHUB_TOKEN: ${{ inputs.github_token }}
EXPAND_SUMMARY_DETAILS: "false"
TF_WORKSPACE: ${{ inputs.tf_workspace }}
with:
commenter_type: plan
commenter_input: ${{ env.PLAN_OUTPUT }}
commenter_plan_path: tfplan
commenter_exitcode: ${{ steps.plan.outputs.exit }}
terraform_version: ${{ inputs.terraform_version }}
@jcogilvie if this is not the right place to report this , pls redirect to the best way for this communication.
I am using your forked repo action:
- name: PR COMMENT -Plan - ${{ matrix['env'] }}
id: planprcomment
if: github.event_name == 'pull_request'
uses: GetTerminus/terraform-pr-commenter@master
continue-on-error: true
with:
commenter_type: plan
commenter_input: ${{ format('{0}{1}', steps.plan.outputs.stdout, steps.plan.outputs.stderr) }}
commenter_exitcode: ${{ steps.plan.outputs.exitcode }}
terraform_version: "1.4.5" # this version is just used for this action and not in terraform execution.
env:
TF_WORKSPACE: ${{ matrix['env'] }}
for 1 repo i have 3 envs running in parallel . but for only 1 env i never get plan PR comment .
logs for the PR comments that worked:
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
╷
│ Warning: Experimental feature "module_variable_optional_attrs" is active
│
│ on .terraform/modules/node_groups_2/node-group-2/variables.tf line 42, in terraform:
│ 42: experiments = [module_variable_optional_attrs]
│
│ Experimental features are subject to breaking changes in future minor or
│ patch releases, based on feedback.
│
│ If you have feedback on the design of this feature, please open a GitHub
│ issue to discuss it.
│
│ (and 2 more similar warnings elsewhere)
╵
Releasing state lock. This may take a few moments...
INFO: Found no tfplan. Proceeding with input argument.
INFO: Found 1 page(s) of comments at https://api.github.com/repos/monacohq/oex-terraform-exchange/issues/313/comments?per_page=100.
INFO: Looking for an existing plan PR comment.
INFO: No existing plan PR comment found.
INFO: Found 1 page(s) of comments at https://api.github.com/repos/monacohq/oex-terraform-exchange/issues/313/comments?per_page=100.
INFO: Looking for an existing outputs PR comment.
INFO: No existing outputs PR comment found.
INFO: Writing 1 plan comment(s)
INFO: Adding plan comment to PR.
INFO: Writing 0 outputs comment(s)
logs for the PR comments that didn't work :
INFO: Found no tfplan. Proceeding with input argument.
INFO: Found 1 page(s) of comments at https://api.github.com/repos/monacohq/oex-terraform-exchange/issues/313/comments?per_page=100.
INFO: Looking for an existing plan PR comment.
INFO: No existing plan PR comment found.
INFO: Found 1 page(s) of comments at https://api.github.com/repos/monacohq/oex-terraform-exchange/issues/313/comments?per_page=100.
INFO: Looking for an existing outputs PR comment.
INFO: No existing outputs PR comment found.
INFO: Writing 0 plan comment(s)
INFO: Writing 0 outputs comment(s)
plan output:
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
╷
│ Warning: Experimental feature "module_variable_optional_attrs" is active
│
│ on .terraform/modules/node_groups_2/node-group-2/variables.tf line 42, in terraform:
│ 42: experiments = [module_variable_optional_attrs]
│
│ Experimental features are subject to breaking changes in future minor or
│ patch releases, based on feedback.
│
│ If you have feedback on the design of this feature, please open a GitHub
│ issue to discuss it.
│
│ (and 2 more similar warnings elsewhere)
╵
Releasing state lock. This may take a few moments...
Hey there @sunnyoswalcro -- I just enabled issues on the forked repo, if you want to put this over there.