5

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
3
  • 1
    There MUST be a better way to prevent this line to be included in the output, but for the sake of completeness, here's how to ignore a first line if it is already present: Either use tail -n +2 instead of cat to start printing at the second line: APP_URL=$(tail -n +2 /tmp/output.json | jq -r '.app_url.value'), or have jq itself get rid of the first line by interpreting only the rest as JSON: APP_URL=$(jq -Rr '[inputs] | join("") | fromjson.app_url.value' /tmp/output.json) Commented Mar 14, 2022 at 3:56
  • Thanks for the suggestion @pmf that makes sense, I tried that and got another error, here's the link github.com/briancaffey/django-step-by-step/runs/… If you want to see the error in the logs I think you will need to be signed in to github. The error new error I get is: parse error: Expected string key before ':' at line 33, column 1 Commented Mar 14, 2022 at 4:20
  • I might need to set terraform_wrapper to false: github.com/hashicorp/setup-terraform/issues/… Commented Mar 14, 2022 at 4:54

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

@saranicole nice
3

Whilst @briancaffey's answer is exactly right, if you need to use the terraform_wrapper for other parts of your workflow (e.g. using the output), 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.

This issue is also fixed in v3.0.0.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.