Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 318
angrykoala Avatar asked Dec 16 '19 12:12

angrykoala


People also ask

How do I rerun a single job in GitHub actions?

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.

How do I rerun check in GitHub?

Click on the pull request number, to the right of the pull request branch name. To re-run failed checks, click Re-run and select Re-run Failed Checks. To re-run individual checks, hover over the individual check you want to re-run and select the icon to re-run the check.

What happens when a GitHub job exceeds an execution time of six hours?

Job execution time - Each job in a workflow can run for up to 6 hours of execution time. If a job reaches this limit, the job is terminated and fails to complete.

What are jobs in GitHub actions?

Each job runs in a runner environment specified by runs-on . You can run an unlimited number of jobs as long as you are within the workflow usage limits. For more information, see "Usage limits and billing" for GitHub-hosted runners and "About self-hosted runners" for self-hosted runner usage limits.


3 Answers

For your particular use case, just do:

npm test || npm test
like image 87
smac89 Avatar answered Oct 19 '22 22:10

smac89


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)

like image 39
Mike Hardy Avatar answered Oct 19 '22 21:10

Mike Hardy


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
} 
like image 2
ekostadinov Avatar answered Oct 19 '22 21:10

ekostadinov