hashicorp / setup-terraform

Sets up Terraform CLI in your GitHub Actions workflow.
https://developer.hashicorp.com/terraform/tutorials/automation/github-actions
Mozilla Public License 2.0
1.35k stars 237 forks source link

Fix output malformed when wrapper enabled #367

Closed OJFord closed 10 months ago

OJFord commented 10 months ago

Presently using a command such as terraform output -json | jq does not work with the wrapper enabled, as it is by default.

In order to consume terraform's output having set it up with this Action, it is necessary either to disable the wrapper (with: terraform_wrapper: false) or run it in its own Actions step with an explicit id (e.g. id: foo) so that it can be referred to and consumed (${{steps.foo.outputs.stdout}} et al.) in later steps.

This seems to be the result of much confusion (issues passim) and is not at all easy (#338) to debug/diagnose and come to the realisation that it's due to the wrapper, or even that such a thing exists.

@austinvalle identified the issue as being due to the @actions/exec package writing the spawned command to stdout (along with then its actual stdout). This has previously been reported upstream in actions/toolkit#649; I've proposed actions/toolkit#1573 to fix it.

This commit aims to address the issue for setup-terraform in the meantime by silencing @actions/exec and then writing out to stdout & stderr from the listener buffers, which it writes to without this additional logging.

Closes #20, #80, #85, #149, #338, #42, #167

OJFord commented 10 months ago

Thanks! Will have a look.

OJFord commented 10 months ago

Thanks for that - I've worked around it as you suggested and proposed a fix (write it to stderr, not stdout) upstream. I've updated the commit message/PR description to reflect that too, sorry that slightly breaks the context of your comment.

austinvalle commented 10 months ago

Alright, more weirdness coming from the wrapper (some of this is mentioned in the GH issues linked in the description).

So I added a simple test with JQ to this PR so we can prove the STDOUT is being printed without any unwanted characters.

You can see with the wrapper, we still get an error (although JQ still processes the command properly), however the actual output looks correct 🤔 (vs. the non-wrapper equivalent that is successful).

After some debugging and looking through the linked issues, I found that there actually is hidden output because of the core.debug lines in the wrapper (which GH action's logs hides by default 😄 ) https://github.com/austinvalle/terraform-provider-sandbox/actions/runs/6660856553/job/18102722958

$ terraform output -json

{
  "pet_name": {
    "sensitive": false,
    "type": "string",
    "value": "balanced-marmot"
  }
}
::debug::Terraform exited with code 0.
::debug::stdout: {%0A  "pet_name": {%0A    "sensitive": false,%0A    "type": "string",%0A    "value": "balanced-marmot"%0A  }%0A}%0A
::debug::stderr: 
::debug::exitcode: 0

These core.debug lines aren't really needed imo, so I just removed them in fd5f36a60aa2009c8f7252c9d32f519369786fcb 🙂. And now we get all green 🥳

github-actions[bot] commented 3 months ago

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.