'Error creating JSON string in github action

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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1