parse error when trying to parse terraform output in GitHub Actions workflow with jq

Viewed 591

I have a GitHub Actions workflow that reads output from a terraform configuration. I'm trying to do this:

terraform -chdir=terraform/live/dev output -json > /tmp/output.json

APP_URL=$(cat /tmp/output.json | jq -r '.app_url.value')

I'm getting the following error in the GitHub Action logs:

parse error: Invalid numeric literal at line 1, column 9

I added the following to debug this:

# debugging output.json file
echo "output.json:"
cat /tmp/output.json

And I'm finding that output of cat /tmp/output.json is:

/home/runner/work/_temp/2b622f60-be99-4a29-a295-593b06dde9a8/terraform-bin -chdir=terraform/live/dev output -json
{
  "app_url": {
    "sensitive": false,
    "type": "string",
    "value": "https://app.example.com"
  }
}

This tells me that jq can't parse the temporary file that I wrote the terraform JSON output to because it seems to be adding the command to the file itself:

/home/runner/work/_temp/2b622f60-be99-4a29-a295-593b06dde9a8/terraform-bin -chdir=terraform/live/dev output -json

How can I get the terraform output as JSON and write it to a file without the extra header line that is causing the parse error?

When I run the same commands locally, I do not get this parse error.

Here's the code for the section of my GitHub Action workflow that is producing this error: https://github.com/briancaffey/django-step-by-step/blob/main/.github/workflows/terraform_frontend_update.yml#L72-L74

Things I have tried

  • using cd terraform/live/dev instead of -chdir=terraform/live/dev - this resulted in the same error
2 Answers

I was able to fix this issue by adding the following setting to the setup-terraform/@v1 action:

      - uses: hashicorp/setup-terraform@v1
        with:
          terraform_version: 1.1.7
          terraform_wrapper: false <-- added this

More documentation about this setting can be found here: https://github.com/hashicorp/setup-terraform#inputs

Whilst @briancaffey's answer is exactly right, if you need to use the terraform_wrapper for other parts of your workflow, you can switch out the problematic terraform calls with terraform-bin instead.

If you also want to run the script outside GitHub Actions, the following workaround will do the trick:

tf=terraform-bin
type "$tf" >/dev/null 2>&1 || tf=terraform

$tf output -json > /tmp/output.json

See https://github.com/hashicorp/setup-terraform/issues/167#issuecomment-1090760365 for an issue that mentions the same workaround.

Related