Home > Software engineering >  How to safely write json output to a file in a github workflow?
How to safely write json output to a file in a github workflow?

Time:01-23

I have a github workflow that obtains information about all Github Releases in my repo and then processes the JSON response using jq. The issue is the shell code I have doesn't handle single-quotes in the JSON data (which it does have sometimes). How can I pipe out steps.release.outputs.data safely to a file without the quotations and other characters being interpreted by the shell?

- name: Get Release Info
  uses: octokit/[email protected]
  id: release
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  with:
    route: GET /repos/{org_repo}/releases/tags/{tag}

- name: Get Information from Release
  run: |
    echo '${{ steps.release.outputs.data }}' > release.json
    jq -r '.assets[].browser_download_url' release.json > assets.txt
    jq -r '.body' release.json > changelog.txt

The part that fails above is the line with echo in the second step. Because the steps.release.outputs.data content has single-quotes in it, that breaks it.

Note that, I only write the JSON data to a file (release.json in the example above) in an attempt to bypass shell processing special characters in the data. If there's a better way to do this without writing to a file I'd prefer that. The part that makes this challenging is that the response JSON gets placed into the final shell script as a literal string instead of as a bash variable.

CodePudding user response:

You can use an environment variable:

- name: Get Information from Release
  env:
    DATA: ${{ steps.release.outputs.data }}
  run: |
    jq -r '.assets[].browser_download_url' <<<"$DATA" > assets.txt
    jq -r '.body' <<<"$DATA" > changelog.txt
  • Related