How to Retry Flaky Jobs in GitLab CI/CD
The retry keyword re-runs a job up to a set number of times, optionally only for specific failure reasons.
Set retry:max to the number of extra attempts and retry:when to the failure reasons that should trigger a retry, so you re-run on infrastructure flakes but not on real test failures.
Steps
- Add
retry:max(0 to 2) for the number of automatic re-runs. - Scope it with
retry:whento specific failure reasons. - Reserve retries for transient causes, not genuine bugs.
Pipeline
.gitlab-ci.yml
e2e:
script: npm run test:e2e
retry:
max: 2
when:
- runner_system_failure
- stuck_or_timeout_failure
- api_failureGotchas
retry:maxis capped at 2, so a job runs at most three times.- Retrying real test failures masks bugs; restrict
whento infrastructure reasons. Latchkey managed runners auto-retry transient infrastructure failures for you.
Related guides
How to Cancel Redundant Pipelines With interruptible in GitLab CI/CDAuto-cancel superseded GitLab CI/CD pipelines by marking jobs interruptible, so a new push cancels older runn…
How to Set a Job Timeout in GitLab CI/CDCap how long a GitLab CI/CD job can run with the timeout keyword, killing a hung job before it consumes the p…