0

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 2

1

run steps by default run with bash --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 like

if ./my-command; then
    result="Success"
else
    result="Failure"
fi
  
echo "result=$result" >> "$GITHUB_OUTPUT"

in the run step. A non-zero exit status in a conditional does not trigger error-on-exit behaviour of the -e option.

Sign up to request clarification or add additional context in comments.

Comments

0

In addition to what @benjamin-w wrote, you also have a variable named incorrectly in your YAML

jobs:
  job1:
    runs-on: ubuntu-latest
    # Map a step output to a job output
    outputs:
      # The variable name of the output here needs to match what you write to $GITHUB_OUTPUT
      output1: ${{ steps.step1.outputs.result }}
    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}}

Notice that I changed steps.step1.outputs.test to steps.step1.outputs.result. You used the name result when you wrote the value out to $GITHUB_OUTPUT.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.