How can I capture exit code of run command and reuse in another job ? #46992
-
Question BodyHi, I'm trying to use the exit code of a binary in order to decide to run next job or not and it doesn't work as expected. jobs:
plan:
name: Plan
runs-on: ubuntu-latest
outputs:
exitcode: ${{ steps.plan.outputs.exitcode }}
steps:
- name: Plan
id: plan
run: |
<some command here executed>
echo "exitcode=$?" >> $GITHUB_OUTPUT
continue-on-error: true
test:
name: Test
runs-on: ubuntu-latest
needs: [ plan ]
steps:
- run: echo ${{ needs.plan.outputs.exitcode }} But the echo in the test job doesn't print any value:
What am I doing wrong ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
By default the shell for You should be able to override that by setting the shell yourself, or (more obvious) disable the "exit on failure": - name: Plan
id: plan
run: |
set +e
<some command here executed>
echo "exitcode=$?" >> $GITHUB_OUTPUT Mind that this way the exit code of the last command will become the exit code of the step, so it won't be marked as failed. If you don't want that, you can carry the exit code over: - name: Plan
id: plan
run: |
set +e
<some command here executed>
exitcode="$?"
echo "exitcode=$exitcode" >> $GITHUB_OUTPUT
exit "$exitcode" |
Beta Was this translation helpful? Give feedback.
-
Another option that I've used is to trap EXIT at the start of the script. In this way, you can have multiple commands in the script, the script still exits when a command fails, and you get the exit code of the command. - id: my-step
run: |
trap 'echo "exit-code=$?" >> "$GITHUB_OUTPUT"' EXIT
cmd-1
cmd-2
... |
Beta Was this translation helpful? Give feedback.
By default the shell for
run
steps is configured to exit as soon as a command fails (that's the-e
in theshell
definition). So for any exit code != 0 theecho
to set the output will never run.You should be able to override that by setting the shell yourself, or (more obvious) disable the "exit on failure":
Mind that this way the exit code of the last command will become the exit code of the step, so it won't be marked as failed. If you don't want that, you can carry the exit code over: