How to check status of previous step?

Hello,
I have a workflow that builds docker image and next action is creating comment on each workflow run.
However, I need to check if build step has failed or nott and take action.

Please note, in my build.sh script, i already set set -e to prevent the execution of bash script if i get any errors.

The following workflow doesn’t work for some reason, I think i am unable to read exit_code. Any help would be much appreciated.
Thanks.


      - name: Build Docker Image
        id: build
        continue-on-error: true
        run: |
          ./build.sh
          echo ::set-output name=exit_code::$?
          echo "Exited with $?"
        env:
          BUILD_ACTOR: ${{ github.event.client_payload.actor }}
          BUILD_INITIATOR: 'cmd'
          GITHUB_REF: $GITHUB_REF

      - name: check steps.build.outputs.exit_code
        run: echo ${{ steps.build.outputs.exit_code }}
        if: always()

      - name: Create comment if build is ok
        if: steps.build.outputs.exit_code == 0
        uses: peter-evans/create-or-update-comment@v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          repository: ${{ github.event.client_payload.repository }}
          issue-number: ${{ github.event.client_payload.issue_id }}
          body : |
            Docker image is now ready at

            ${{ github.event.client_payload.image_version }}

      - name: Create comment if build fails
        if: steps.build.outputs.exit_code != 0
        uses: peter-evans/create-or-update-comment@v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          repository: ${{ github.event.client_payload.repository }}
          issue-number: ${{ github.event.client_payload.issue_id }}
          body : |
            Docker build has been failed.

            [Debug output of workflow][1]

            [1]: ${{ steps.vars.outputs.run-url }}

It’s hard to say exactly what is causing your problem without a detailed description of the problem (and ideally a link to a workflow run), but I already see a few problems:

There are two problems here:

  1. If the build.sh script fails, the step will exit there, so the set-output will never be issued. See Exit codes and error action preference for details and the recommended workaround.

  2. The second echo will always write “Exited with 0”, because in Bash $? is always the exit status of the previous command. For the second echo that’s the first echo, and I don’t see how that could fail.

For the comment steps, note that the documentation on Job status check functions says this:

If your if expression does not contain any of the status functions it will automatically result with success().

For the “Create comment if build is ok” step that’s probably fine, but for the “Create comment if build fails” I assume you want a condition like this:

if: failure() && steps.build.outputs.exit_code != 0
1 Like

Hi @martyrs,

Glad to see you in Github Community Forum!

You can get the step status via step context, check the steps.<step id>.outcome value if it’s failure, success…etc.

In the latter step, check with if expression if: steps.build.outcome=='failure' for build fail.

Checked on my side:
image

Thanks

1 Like

Thank you all.
I resolved this with the following change:

      - name: Create comment if build is ok
        if: ${{ success() }}

.........

      - name: Create comment if build fails
        if: ${{ failure() }}
1 Like

Hi @martyrs,

Just one comment, ${{ failure() }} will return true when any previous step of a job fails.
Hence, if you use it to check the status for step ‘Build Docker Image’, please make sure only step ‘Build Docker Image’ is possible to fail.

Thanks

2 Likes