Github.ref and failure() cannot be applied together?

I was trying to add some slack notifications into my github action job, but found that gihtub.ref check cannot be used with success().

The code looks like below and both success() and failure() got executed all the time.

  job:
    ...
    steps:
      ...
      - name: success run notification
        if: github.ref == 'refs/heads/master' && ${{ success() }}
        env:
          SLACK_WEBHOOK: ${{ secrets.LAUNCHING_PD_SLACK_WEBHOOK }}
        uses: Ilshidur/action-slack@2.1.0
        with:
          args: |
            success run!
      - name: failure run notification
        if: github.ref == 'refs/heads/master' && ${{ failure() }}
        env:
          SLACK_WEBHOOK: ${{ secrets.LAUNCHING_PD_SLACK_WEBHOOK }}
        uses: Ilshidur/action-slack@2.1.0
        with:
          args: |
            failure run

The workaround I did is to move those two slack notification steps into separate job, so that I can do something like this, and it seems working for my favor.

  slack-notif:
    runs-on: ubuntu-latest
    needs: job
    if: github.ref == 'refs/heads/master'
    steps:
      - name: success run notification
        if: ${{ success() }}
        env:
          SLACK_WEBHOOK: ${{ secrets.LAUNCHING_PD_SLACK_WEBHOOK }}
        uses: Ilshidur/action-slack@2.1.0
        with:
          args: |
            success run!
      - name: failure run notification
        if: ${{ failure() }}
        env:
          SLACK_WEBHOOK: ${{ secrets.LAUNCHING_PD_SLACK_WEBHOOK }}
        uses: Ilshidur/action-slack@2.1.0
        with:
          args: |
            failure run!

You need to wrap the whole condition expression in ${{ ... }}, or omit it altogether (see jobs.<job_id>.steps.if):

        if: ${{ github.ref == 'refs/heads/master' && success() }}

Same for the failure() version.

Thanks, it works for me.

1 Like