'Retry failed jobs in github actions

I'm trying to use GitHub Actions for CI testing, so far I have my test workflow as follows:

name: test

on: [push]

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-node@v1
        with:
          node-version: 12
      - run: npm install
      - name: test
        run: |
          npm run lint
          npm test
        env:
          CI: true

.github/workflows/test.yml

It is working fine, except I want to retry the test step (or the whole job) once if the tests fails.

Basically, the same behavior you get with travis-retry:

script:
  - npm run lint
  - travis_retry npm test

or with Gitlab CI:

test:
  stage: test
  retry: 1
  script:
    - npm run lint
    - npm test

Not sure if there is a way for this or a reasonably simple workaround



Solution 1:[1]

For your particular use case, just do:

npm test || npm test

Solution 2:[2]

Action Retry seems to work well in my testing

https://github.com/nick-invision/retry/

I was able to use it with multi-part commands as long as they were single line (for example do-this && do-that)

Solution 3:[3]

I would say that you need to manage the retry logic on code level. Meaning, implement/integrate such mechanic to handle and execute again only the failed tests. I'm afraid simply

want to retry the test step (or the whole job) once if the tests fails.

will execute all your tests, may even overwrite outputs, like reports and logs from the first run.

In my experience, I have used a wrapper (shell) script. Here is how it could be achieved:

#!/usr/bin/env bash
{ # try
    # your npm test command here, that saves as output -  failed tests
} || { # catch
    # retry failed tests
      if [ -f ./rerun-failed-file ]; then
        echo "============= [WARN] Rerun file found! =============="
        echo "============= [WARN] Rerunning FAILED tests mode. =============="
        # run npm test command that picks up the failed tests & aggregate test artifacts
      fi
} 

Solution 4:[4]

Unfortunately it doesn't look like the feature exists yet.

As a manual work around I've had to retrigger failed workflows with the re-run all checks button that you can find at the top right corner when viewing a failed workflow.

Solution 5:[5]

Look at wretry.action.

The action can retry single Github action or shell commands. But have some restrictions.

Solution 6:[6]

Note that you can rerun failed jobs from command line, using the GitHub CLI gh

Since gh 2.6.0:

Rerun failed jobs

gh run rerun has been augmented with two new flags:

You can now selectively rerun just failed jobs from a given workflow run!

Running gh run rerun --failed will prompt you to select a run to work with.


And this is supported from the Web GUI:

Save time with partial re-runs in GitHub Actions

It is now possible to re-run only failed jobs or a single job in GitHub Actions workflows.

If you have failing jobs in a workflow run, you’ll now see a new drop-down menu where you can choose “Re-run failed jobs” in addition to the existing “Re-run all jobs”.

For a completed run, each job listed in the sidebar has a re-run icon when you hover over it. Jobs can also be re-run directly from the logs view.

re-reun this job icon -- https://github.blog/wp-content/uploads/2022/03/partial-reruns-github-actions-3.png

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 smac89
Solution 2 Mike Hardy
Solution 3
Solution 4 kachow6
Solution 5 dmvict
Solution 6