Home > database >  Github Action output not saving
Github Action output not saving

Time:01-28

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?

CodePudding user response:

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.

CodePudding user response:

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.

  • Related