-1

I'm saving a JSON field, .body, to an ENV variable. The content of this field will always be changing with multiple strings and characters, it's the body message of a Pull Request in GitHub. So markdown will also get passed:

Below would be an accurate example extracted from .body

#Title

## SubTitle

*date*

1. Item 1
2. Item 2
3. Item 3

When I parse the JSON using jq and -r for its raw content:

echo "PR_BODY=$(jq -r '.body' $HOME/pr.json)" >> $GITHUB_ENV

I get errors from GitHub actions:

enter image description here

For reference, my plan is to save .body data into a markdown file like so after managing to save it to an env variable:

      run: |
        ed changelog.md <<'END_ED'
        1i

        ${{ env.PR_BODY }}

        .
        wq
        END_ED
        echo >> changelog.md

How can I go about extracting the data in .body without empty spaces throwing errors? Perhaps an ENV variable is not the way to go about this?

EDIT: Here is an original raw JSON in the .body:

"body": "Manually configuring ports for DEV, CAT/QA environments. Migrating from managed service to self-serve \"GCP resource\". \r\n\r\n- [x] DEV\r\n- [x] CAT/QA\r\n- [ ] PROD\r\n\r\n```\r\n# Allow healthcheck on ports 80, 443\r\nresource \"google_compute_firewall\" \"allow-healthcheck\" {\r\n  name = \"${format(\"%s\",\"${var.gcp_resource_name}-${var.gcp_env}-fw-allow-healthcheck\")}\"\r\n  network = \"${google_compute_network.vpc.name}\"\r\n  allow {\r\n    protocol = \"tcp\"\r\n    ports    = [\"80\",\"443\"]\r\n  }\r\n  source_ranges = [\"2.2.0.0/16\", \"1.1.0.0/22\"]\r\n}\r\n```\r\nConfiguring for bug, link here:\r\n[Link To Terraform provider update](https://www.terraform.io/)"
8
  • 1
    I don't see any json
    – jesse_b
    Jun 16 at 18:23
  • Please see my new update! Ive added the key and value for .body
    – andres
    Jun 16 at 18:29
  • 1
    is $GITHUB_ENV a shell script, or something else? echo "PR_BODY=$(jq -r '.body' $HOME/pr.json)" >> $GITHUB_ENV will run the command substitution right then and there, so if the output of jq has multiple lines, they'll go into multiple lines in the output. That might break the expected syntax at the other end.
    – ilkkachu
    Jun 16 at 18:33
  • 1
    Also I'm not sure why you're saying it's Bash throwing errors? That doesn't look like any error Bash would give, and you're getting it from some Github... thing.
    – ilkkachu
    Jun 16 at 18:37

1 Answer 1

2

Based on "Workflow commands for GitHub Actions", it looks like the syntax for multiline values in $GITHUB_ENV would be something like:

PR_BODY<<EOF
multiline string
here
...
EOF

With the value coming from the command substitution you had there, you could produce that from the shell with e.g.:

printf "PR_BODY<<EOF\n%s\nEOF\n" "$(jq -r '.body' $HOME/pr.json)"

(You most likely want to use printf instead of echo there, since it makes it easier to insert newlines while not mangling the data from the command substitution. See Why is printf better than echo? for discussion.)

2
  • 1
    @roaima, well... what exactly reads that file and how was the thing I tried to ask the original poster. They didn't answer and I'm still not sure, but at least the example I linked to there is not shell syntax. (There's no = there, and you can't assign directly from a here-doc a variable like that anyway.)
    – ilkkachu
    Jun 16 at 22:42
  • Sorry, you're right it's not shell syntax. Deleting...
    – roaima
    Jun 17 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.