'Trying to parse JSON output in a Github Action
I'm trying to extract the value of a HTTP request in a GitHub action and then use that value in another step.
This is the current code:
- run: call some https endpoint
and that returns to the console output:
{
"authorizationToken": "<snip>",
"expiration": "2021-02-26T18:18:38+00:00"
}
and I'm trying to extract the authorizationToken value and then use it in the next step, like
-name: Get auth token
run: call some https endpoint
-name: set something which uses the token
run: set blah --token $token_from_previous_step
~ - run: call some https endpoint | echo "jq '.authorizationToken'"~
which errors with:
jq '.authorizationToken'
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
BrokenPipeError: [Errno 32] Broken pipe
I'm now able to extract the key/value .. but not sure how to set it as an environment variable, to be used in other steps.
This works:
- run: call some https endpoint > at.json
- run: jq '.authorizationToken' at.json
so that get the json output from that https endpoint result and saves the result into a file called at.json (which is the json text, above).
Next, I then run the jq command, extracting the authorizationToken value. which works!
Now I need to set this value as a env-var. Reading the docs on this, it looks like it can be done, but I'm not sure how to call jq and set the key/value into an env-var.
I feel like I need to do something like this:
- run: echo "AUTH_TOKEN=run the jq command here" >> $GITHUB_ENV
Something like this (which totally fails):
- run: echo "AUTH_TOKEN=${{ jq '.authorizationToken' at.json }}" >> $GITHUB_ENV
Solution 1:[1]
I will just add here some more solutions. Like you discovered yourself, you can pass it as an environment variable:
steps:
- run: |
token=$( callEndpoint | jq '.authorizationToken' )
echo "AUTH_TOKEN=$token" >> "$GITHUB_ENV"
- run: echo "$AUTH_TOKEN"
You can also use a step output:
steps:
- id: get-token
run: |
token=$( callEndpoint | jq '.authorizationToken' )
echo "::set-output name=auth_token::$token"
- run: echo ${{ steps.get-token.outputs.auth_token }}
Both of these solutions will work for passing values between steps of the same job.
If you need to pass values between steps of different jobs, you can use job outputs:
jobs:
job-a:
runs-on: ubuntu-latest
outputs:
auth_token: ${{ steps.get-token.outputs.auth_token }}
steps:
- id: get-token
run: |
token=$( callEndpoint | jq '.authorizationToken' )
echo "::set-output name=auth_token::$token"
job-b:
runs-on: ubuntu-latest
needs: job-a
steps:
- run: echo ${{ needs.job-a.outputs.auth_token }}
Solution 2:[2]
- I needed to use
>> $GITHUB_ENVto set the environement variable. - I needed to get the correct syntax to run a set of command line programs, inline.
- run: call some https endpoint > at.json
- run: echo "AUTH_TOKEN= $(jq '.authorizationToken' at.json)" >> $GITHUB_ENV
- run: echo "${{ env.AUTH_TOKEN }}"
Details to the above solution:
- Line 1: some command which calls some external service via HTTPS. Response payload is JSON. this json data is then stored straight away into a file called
at.json. - Line 2: display some text in the format
<key>=<value>. Then append this text to the github environment variable 'list/text/whatever'. Now -> here's the kicker => to calculate thevaluepart of this key/value, run some command (or commands) inside this bit$( .. ). So for me, I'm runningjqcommand and extracting the value of theauthorizationTokenfrom the json text, from inside the fileat.json. Phew! - Line3: just dump the value of my environment variable, to the log output. (yes, yes .. this is all insecure. I'm doing this as a test.)
and that's it!
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 | David Reed |
| Solution 2 | Pure.Krome |
