How can I remove all the extraneous output from redirected output in GitHub Actions?

Viewed 707

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???

1 Answers

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. ‍♂️

Related