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
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 }}
>> $GITHUB_ENV to set the environement variable. - 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:
at.json.<key>=<value>. Then append this text to the github environment variable 'list/text/whatever'. Now -> here's the kicker => to calculate the value part of this key/value, run some command (or commands) inside this bit $( .. ). So for me, I'm running jq command and extracting the value of the authorizationToken from the json text, from inside the file at.json. Phew!and that's it!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With