I’m attempting to save the result of a GitHub Actions job to be used by another job, like this:
jobs:
job1:
runs-on: ubuntu-latest
# Map a step output to a job output
outputs:
output1: ${{ steps.step1.outputs.test }}
steps:
- id: step1
run: |
./my-command
[ $? == 0 ] && result="Success" || result="Failure"
echo "result=$result" >> $GITHUB_OUTPUT
job2:
runs-on: ubuntu-latest
needs: job1
steps:
- run: echo ${{needs.job1.outputs.output1}}
The issue I’m having is this will never show "Failure" as the output, only ever "Success". If ./my-command
has a non-zero exit code, it will show nothing.
Am I missing something?
2
Answers
run
steps by default run withbash --noprofile --norc -eo pipefail {0}
under the hood (see here); this includes the "error on exit" option, which makes the run step abort if./my-command
isn’t successful. To avoid that, you can use a construct likein the
run
step. A non-zero exit status in a conditional does not trigger error-on-exit behaviour of the-e
option.In addition to what @benjamin-w wrote, you also have a variable named incorrectly in your YAML
Notice that I changed
steps.step1.outputs.test
tosteps.step1.outputs.result
. You used the nameresult
when you wrote the value out to$GITHUB_OUTPUT
.