2

In one of my steps in a github action, I am trying to create a JSON string that I can use in a later step.

I get an error on the following step,

- name: Generate JSON string
        run: echo "JSON_STRING=$( jq -n \
                --arg image "foo" \
                --arg region "us-west-2" \
                --arg secret "mysecret" \
                --arg env "prod" \
                '{"flask":{"image": $image,"ports":{"5000":"HTTP"},"environment":{"FLASK_ENV":$env,"AWS_SECRET_NAME": $secret,"AWS_REGION_NAME": $region}}}' )" >> $GITHUB_ENV

In a terminal shell (ubuntu), I am able to run this command and generate a JSON string.

spitfiredd
  • 2,897
  • 5
  • 32
  • 75
  • Which error do you get? – pmf Oct 01 '21 at 18:18
  • It looks like a syntax error; "You have an error in your yaml syntax on line 41" . I am not too sure of the syntax for JSON in Yaml files. – spitfiredd Oct 01 '21 at 18:25
  • Things I would try for testing: Eliminate the backslashes by putting all into one line and see if the error has gone. If not, also eliminate the call to `jq` by assigning the JSON object directly: `JSON_STRING='{"flask":{"image":"foo","ports":{"5000":"HTTP"},"environment":{"FLASK_ENV":"prod","AWS_SECRET_NAME":"mysecret","AWS_REGION_NAME":"us-west-2"}}}'` – pmf Oct 01 '21 at 18:32
  • My suggestion is that your command substitution `$(...)` is not expanding as in the terminal. – pmf Oct 01 '21 at 18:37
  • I found this: https://stackoverflow.com/questions/65159967/gitlab-ci-cd-to-amazon-lightsail; so far this work run: echo `"{\"flask\":{\"image\":\"$PIPELINE_IMAGE_TAG\",\"ports\":{\"5000\":\"HTTP\"},\"environment\":{\"FLASK_ENV\":\"${{ secrets.FLASK_ENV }}\",\"AWS_SECRET_NAME\":\"${{ secrets.AWS_SECRETS_NAME }}\",\"AWS_REGION_NAME\":\"${{ secrets.AWS_REGION }}\"}}}"` – spitfiredd Oct 01 '21 at 18:57

1 Answers1

0

Your step appends JSON_STRING=value to $GITHUB_ENV. This causes the JSON_STRING environment variable to be set in subsequent steps.

Normally GITHUB_ENV contains one VAR=value per line. Though, multiline values can also be added if they're surrounded by delimiters.

So the problem is that jq formats its output on multiple lines by default. A simple fix would be to pass --compact-output / -c to jq so that your JSON string fits on a single line.

Alex Jasmin
  • 39,094
  • 7
  • 77
  • 67