32

I'm trying to find a way to achieve the following functionality: Whenever a step fails, it will show that it failed (will provide correct feedback) but will still continue to other steps.

At the moment, failure causes the step to stop:

Step's failure prevents next step from starting

I've seen the most popular suggestion is to use continue-on-error, but that seems to make the step's conclusion 'Success', and will not show it failed unless you go into the logs.

Failed step appears to be successful

In the screenshot above, the "Secrets" step failed, and yet it appears to be successful unless entering the logs.

When reading this thread, I came to suspect this feature might not exist yet in GH actions.

I've also tried using conditionals for each step, and/or for the job. For example, I've tried: if: ${{ success() }} || ${{ failure() }} - this simply did not provide the needed functionality, the step failed and the next step did not start.

if: succeeded() || failed() - took this syntax from the GitHub community thread above, but it generated a syntax error (which makes sense, since it isn't compatible with the syntax specified here).

To conclude, I'm looking for a way to make steps that fail indicate they failed, and still make the workflow continue to the next step.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
georgek24
  • 521
  • 1
  • 4
  • 7
  • 4
    instead of `succeeded() || failure()`, try [`always()`](https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#always). Use this on the subsequent steps that need to run regardless of if a previous step fails – smac89 May 27 '20 at 15:32
  • always() seems to work, thanks! – georgek24 May 31 '20 at 07:31
  • 1
    Does this answer your question? [How to run a github-actions step, even if the previous step fails, while still failing the job](https://stackoverflow.com/questions/58858429/how-to-run-a-github-actions-step-even-if-the-previous-step-fails-while-still-f) – A Jar of Clay Mar 30 '23 at 13:46

3 Answers3

20

To my surprise, and thanks to @smac89 for suggesting it - adding if: always() seems to do the trick. I'm still wondering why if: ${{ success() }} || ${{ failure() }} failed, but the solution seems to work for the moment. Thank you for the help!

georgek24
  • 521
  • 1
  • 4
  • 7
  • 9
    i think `if: ${{ success() || failure() }}` would work. Note only one set of braces. From my reading of the docs `if: always()` runs when the job when cancelled as well as if it passes or fails. – John Walker Jun 25 '20 at 06:41
  • this doesn't work for me, the step still fails and the next steps get cancelled – knocte Dec 27 '22 at 07:49
  • Regarding the comment by @knocte , you need to add the line with 'if' to each step _after_ the failing step. See this answer for a worked example: https://stackoverflow.com/a/58859404/2792414 – Hermann.Gruber Feb 22 '23 at 12:57
  • 1
    thanks for the clarification @hermann-gruber, I might give this a go; but it's such an ugly solution nonetheless, I hope GitHub implements a better of way of showing warnings or something – knocte Feb 23 '23 at 01:06
12

The continue-on-error is indeed popular, and part of the reason is perhaps that you can set it for each step, or for the whole job. I’m not sure you are aware of this, and this solution was the answer I was looking for.

So this will continue all steps, if either fails:

Job:
  MyRun:
    runs-on: ubuntu-latest

    continue-on-error: true
    
    steps:
      - name: Step One
        run: curl --fail -o dates.csv https://doesnotexist.com/dates.csv
      - name: Step Two
        run: date >> dates.csv

And this will only continue if “Step One” fails, it will break and stop if e.g. “Step Two” fails:

Job:
  MyRun:
    runs-on: ubuntu-latest
    
    steps:
      - name: Step One
        run: curl --fail -o dates.csv https://doesnotexist.com/dates.csv
        continue-on-error: true
      - name: Step Two
        run: date >> dates.csv
MS Berends
  • 4,489
  • 1
  • 40
  • 53
  • 10
    Thanks for the detailed answer, but I believe `continue-on-error: true` still marks the failing step as "succeeded" with a faux green checkmark. What the op (as well as myself) is looking for is to continue execution of the workflow on error but show that the step failed with a red X. The other answer, [using always()](https://stackoverflow.com/a/62112985/552792) achieves this, but it involves putting `if: always()` on every subsequent step, so it's not without its own headaches. – jamesmortensen Oct 20 '22 at 07:37
0

Just adding on to this old conversation:

  1. The behavior has changed. My job marked with continue-on-error: true does show a "failure" icon, not a faux "success" icon. The workflow continues on afterward.

  2. If a job punts to an external workflow via uses:, the continue-on-error must go in the jobs in the external file. It cannot go on the job in the main workflow. That is, the following gives a syntax error:

jobs:
  my_continue_on_error_job:
    uses: ./.github/workflows/other_workflow.yml
    continue-on-error: true

It fails with:

The workflow is not valid. .github/workflows/main_workflow.yml (Line: 201, Col: 5): Unexpected value 'continue-on-error'

It worked when I moved the continue-on-error into the jobs in the other_workflow.

Noach Magedman
  • 2,313
  • 1
  • 23
  • 18