I have a GitHub Actions workflow that uses Terraform for its deployment.
When Terraform is done, I want to take the Terraform output and send it to the next job in the workflow so that pieces can be extracted an used. Specifically, my Terraform deploys an Azure Function and then outputs the function app name. This then gets used to tell the next job where to deploy the Function code.
However, when I redirect the output of terraform output
like so:
- name: save tf output
run: terraform output -json > tfoutput.json
shell: bash
working-directory: terraform
and then put it into a job artifact
- name: Upload output file
uses: actions/upload-artifact@v2
with:
name: terraform-output
path: terraform/tfoutput.json
the content resulting file looks like this:
[command]/home/runner/work/_temp/fb419afc-033e-4058-b5f3-c44b90cb0bd0/terraform-bin output -json
{
"functionappname": {
"sensitive": false,
"type": "string",
"value": "telemetry-function"
}
}
::debug::Terraform exited with code 0.
::debug::stdout: {%0A "functionappname": {%0A "sensitive": false,%0A "type": "string",%0A "value": "telemetry-function"%0A }%0A}%0A
::debug::stderr:
::debug::exitcode: 0
::set-output name=stdout::{%0A "functionappname": {%0A "sensitive": false,%0A "type": "string",%0A "value": "telemetry-function"%0A }%0A}%0A
::set-output name=stderr::
::set-output name=exitcode::0
Which means, of course, it's definitely not machine readable as JSON output from Terraform should be.
I've yet to find any way to get all that extraneous junk to be removed. It's worth noting that in Azure DevOps this flow of work performs exactly as one would expect.
I took the approach of putting everything in one job (to avoid redirecting output and passing an artifact) and then using terraform output | jq -r ...
to get the output from terraform into my jq statement to pull out the value and it still doesn't work. It appears the output from this command really is all that junk, for some reason.
Not sure if this is something I can work around, a bug in the terraform action, or a bug in GH Actions in general.
Additionally, where do I file bugs on GH Actions???
The solution is to add terraform_wrapper: false
to your Setup Terraform step:
- name: Setup terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: ${{ env.TERRAFORM_VERSION }}
terraform_wrapper: false
as, by default, the Terraform Action will wrap all its output in this junk. 🤷🏻♂️
This happens exactly for cases where a given step executes a terraform output and uses that output in subsequent calls to other steps or job.
The documentation for the setup-terraform action makes this very clear:
Installing a wrapper script to wrap subsequent calls of the terraform binary and expose its STDOUT, STDERR, and exit code as outputs named stdout, stderr, and exitcode respectively. (This can be optionally skipped if subsequent steps in the same job do not need to access the results of Terraform commands.)
terraform_wrapper - (optional) Whether to install a wrapper to wrap subsequent calls of the terraform binary and expose its STDOUT, STDERR, and exit code as outputs named stdout, stderr, and exitcode respectively. Defaults to true.
To solve this error, just add one of the available inputs equal to false:
- name: Set up Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.3.0
terraform_wrapper: false
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With