I have worked out a generic solution which should allow you do expose any output to a github-script via ::set-output
no matter what special characters it contains:
opened 02:11AM - 19 Jun 22 UTC
I have _terraform-compliance_ running successfully in a GitHub Actions validatio… n pipeline with a working feature. However, I would like to get the results of the execution added to Pull Requests. I have tried to do this in the style of the example at the bottom of this page:
https://github.com/hashicorp/setup-terraform. It works very nicely for the Terraform output itself, appearing as shown below with the outputs expanding on-click. However I cannot capture the output from _terraform-compliance_.
> Terraform Initialization ⚙️ ```success```
> Terraform Validation 🤖 ```success```
> Terraform Plan 📖 ```success```
> Show Plan
> Terraform Compliance 👺 ```failure```
> Show Output
After some experimentation I now understand that the _setup-terraform_ GitHub action captures and exposes these outputs this using a JavaScript wrapper, which wasn't obvious. I had naïvely expected that GitHub Actions would provide these **stdout** and **stderr** outputs as standard. I have tried various ways to achieve a similar result for _terraform-compliance_ myself, without success.
I tried a generic capture of **${{ steps.tfcompliance.outputs.response }}**, which ends up null.
I tried the ::set-output command:
```
name: Validation of plan by terraform-compliance
id: tfcompliance
run: |
echo ::set-output name=stdout::$(terraform-compliance -p ../../terraform/output.plan.json -f . --no-ansi)
```
I then try to retrieve this output by referencing **${{ steps.tfcompliance.outputs.stdout }}** but the above example results in the first line of output only, and it always finishes with successful status - because it's measuring the echo result, not the result of the terraform-compliance command. Furthermore I lose local echo of the results in the workflow log which isn't desirable.
I have tried to use this [apparently confirmed method from Stack Overflow](https://stackoverflow.com/a/66713349):
```
name: Validation of plan by terraform-compliance
id: tfcompliance
run: |
terraform-compliance -p ../../terraform/output.plan.json -f . --no-ansi | tee test.log
result_code=${PIPESTATUS[0]}
echo "::set-output name=stdout::$(cat test.log)"
exit $result_code
```
which results again in a null **${{ steps.tfcompliance.outputs.stdout }}** - it's like the _tee_ is completely ignored.
I then discovered that according to [this GitHub Community Forum post](https://github.community/t/set-output-truncates-multiline-strings/16852/3), _set-output_ cannot in fact deal with multiline strings, which seems pretty deficient. However, it transpires that you can substitute the line breaks (effectively turning it back into a single line string) in a way that will be automatically converted back when an output is interpolated later.
So I tried:
```
name: Validation of plan by terraform-compliance
id: tfcompliance
run: |
terraform-compliance -p ../../terraform/output.plan.json -f . --no-ansi > output.log
OUTPUT="$(cat output.log)"
OUTPUT="${OUTPUT//'%'/'%25'}"
OUTPUT="${OUTPUT//$'\n'/'%0A'}"
OUTPUT="${OUTPUT//$'\r'/'%0D'}"
echo "::set-output name=stdout::${OUTPUT}"
```
This didn't work at all either - still a completely blank **${{ steps.tfcompliance.outputs.stdout }}**
Reading the Radish docs section [describing console output](https://radish.readthedocs.io/en/stable/commandline.html#run-printing-results-to-console), it does appear that the console output is rendered in a manner that possibly complicates this (with overprinting of previously displayed text during scenario executions etc.). I tried to use the ```--write-steps-once``` option but it's not supported by _terraform-compliance_ despite a mention [in the documentation](https://terraform-compliance.com/pages/usage/additional_parameters.html) that options should be passed through to _radish_. EDIT - Ahh, I see that particular _radish_ option is enabled by default by _terraform-compliance_.
Considering all this, would you perhaps consider adding a JavaScript wrapper to capture and expose regular GitHub Actions outputs for **stderr**, **stdout**, and **exitcode** in the [same way that _setup-terraform_ does it](https://github.com/hashicorp/setup-terraform/blob/main/wrapper/terraform.js)? I think this is something that a lot of people would want, and it currently seems unnecessarily difficult to achieve independently.
In the meantime - am I missing something obvious to capture this output?
Thanks