Home > front end >  Github Action - After using jq to extract a json value the following bash commands still attempt to
Github Action - After using jq to extract a json value the following bash commands still attempt to

Time:12-13

So I have a bash command like

- name: Get waiting pull_request_id
        id: get_pr_id
        run: |
          prId='${{ steps.get_in_progress_workflow_run.outputs.data }}' | jq '.workflow_runs' | jq '.[] | first(select(.status=="in_progress"))' | jq '.pull_requests' | jq '. | first' | jq '.number' \
          echo "$prId" \
          echo "prId=$prId" >> $GITHUB_OUTPUT

I am using this to lookup a workflow that is in progress and extract the pull request Id. I want to output the pull requestId to use in the next step

The error is

jq: error: Could not open file echo: No such file or directory
jq: error: Could not open file : No such file or directory
jq: error: Could not open file echo: No such file or directory
jq: error: Could not open file prId=: No such file or directory

So it seems that even with the \ I am still in the context of the previous line of script. I have tried semicolons to no avail. How can I make this work?

If I remove the backslashes, then the first line never seems to query anything or even execute as I get no output at all

I have verified that the first cmd line does return the prId if I echo it instead of assigning to a variable. I have tried to echo into the github output directly from that 1st line, but it requires quotation marks and I can't figure out how to do a jq select without using quotes.

CodePudding user response:

prId='${{ steps.get_in_progress_workflow_run.outputs.data }}' | jq ...

Does not provide any input to jq. It sets a variable, then pipes nothing to jq. Setting a variable does not produce any output.

If you want to feed the variable to jq, you have to output it explicitly:

prId='${{ steps.get_in_progress_workflow_run.outputs.data }}';
printf '%s\n' "$prId" | jq ...

If that does not work, you could try the following:

prId='${{ steps.get_in_progress_workflow_run.outputs.data }}';
echo '${{ steps.get_in_progress_workflow_run.outputs.data }}' | jq ...

... | jq '.number' \
          echo "$prId" \
          echo "prId=$prId" >> $GITHUB_OUTPUT

Backslashes at the end of a line escape the newline character and continues the line to the next line. So the 3 lines above are parsed as a single line by your shell interpreter.


Also note that there's no need to start that many separate jq processes. Your shell pipeline can easily be merged into a single jq invocation:

... | jq '.workflow_runs[] | first(select(.status=="in_progress")) | .pull_requests[0] | .number'

After several comments and trying to understand the problem from a different angle, here's what are probably really trying to do:

prId="$(echo '${{ steps.get_in_progress_workflow_run.outputs.data }}' | jq '.workflow_runs[] | first(select(.status=="in_progress")) | .pull_requests[0] | .number')";
echo "$prId";
echo "prId=$prId" >> "$GITHUB_OUTPUT"

The magic word you are looking for here is command substitution

  • Related