yung peso
yung peso

Reputation: 1766

Github Actions - Export JSON object from CURL response to ENV variable

Trying to save API responses in their original JSON format as env variables. Getting formatting errors.

 echo "GIT_PR_OBJECT=$(curl -u USER:TOKEN https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.number }})" >> $GITHUB_ENV

Error from Github:

Error: Unable to process file command 'env' successfully.

Error: Invalid environment variable format ' "sha": "123456789",'

My plan is to access this object multiple times throughout the workflow, that way I only need to make one call to Github API rather than few. Here is how I access the data object saved in the env var.

 echo "GIT_EMAIL=$(${{ env.GIT_COMMIT_OBJECT }} | jq -r '.commit.author.email')" >> $GITHUB_ENV

How can I save my curl response as an env variable in it's JSON format?

Upvotes: 2

Views: 2493

Answers (1)

Samira
Samira

Reputation: 9751

Why not save json content to file instead? Saving to environment variable, while sounds fancy, also smells like a can of worms you don't have to deal with in a first place.

Taking your example:

- run:   |
         curl -u USER:TOKEN https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.number }} > $HOME/pr.json
         echo "GIT_EMAIL=$(jq -r '.commit.author.email' $HOME/pr.json)" >> $GITHUB_ENV
  shell: bash

While it is a working solution in my eyes, i fail to understand why it has to be environment variable, which is your main question here.

Upvotes: 4

Related Questions