> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trunk.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Re-run failed checks

> Retry a pull request's failed GitHub Actions or Buildkite jobs in the merge queue, automatically for flaky jobs or by hand, without resubmitting it or losing its place.

When a required check fails in the queue because of a flaky test or a CI problem, Trunk can re-run just the failed jobs and test the pull request again. The pull request keeps its place, and nobody resubmits it.

Trunk can re-run a pull request only while it waits in **Pending Failure**: its tests failed, but Trunk hasn't removed it, because the pull requests ahead of it are still testing. The first pull request in the queue has nothing ahead of it, so with default settings it's removed the moment its tests fail. Only [Automatic Re-runs](#automatic-re-runs) act in time for it; the button and re-runs you start in your CI work for pull requests further back. [More on Pending Failure](https://docs.trunk.io/merge-queue/optimizations/pending-failure-depth.md).

| How | Starts | Best for |
| - | - | - |
| [From Trunk](#re-run-from-trunk) | When you click **Re-run failed checks** | A one-off failure you want to try again |
| [Automatic Re-runs](#automatic-re-runs) | When a job you named fails | Jobs you know are flaky |
| [In your CI](#re-run-in-your-ci) | When you re-run it there | Any CI, including CI that Trunk can't re-run |

A re-run keeps the jobs that passed. To start over instead, use [**Restart tests**](https://docs.trunk.io/merge-queue/using-the-queue/handle-failed-pull-requests.md#manually-restarting-failed-pull-requests), which runs every check again.

## What Trunk can re-run

* **GitHub Actions**: jobs in the pull request's repository, including checks those workflows report as commit statuses.
* **Buildkite**: builds in a Buildkite organization that you [connect to Trunk](#connect-buildkite).

Trunk can't re-run checks from any other CI system, such as CircleCI or Jenkins, or from a workflow in another repository. Re-run those in their own CI.

Trunk starts re-runs only on GitHub. On GitLab, turn on the setting below and re-run failed jobs in GitLab CI: Trunk tests the merge request again.

## Turn it on

An organization admin turns on **Allow Re-running Failed Checks** in **Merge Queue** → **\[your repository]** → **Settings** → **General**. It's off by default, and all three ways need it. With it on, when a pull request's tests fail, Trunk leaves its CI in place instead of canceling it, so the failed jobs can be re-run.

Before you turn it on:

* With default settings, the pull requests behind one that's being re-run don't start testing or merge until its re-run finishes. The re-run must start within the [Re-run Time Limit](#while-trunk-waits-for-the-re-run), 30 minutes by default.
* A re-run retries every failed job in the workflow run or Buildkite build, not only the one you care about.
* A pull request gets two re-runs each time it starts testing, shared by Automatic Re-runs and the button. **Restart tests** or a resubmission starts a new count.
* Keep a [**Timeout for Tests to Complete**](https://docs.trunk.io/merge-queue/administration/advanced-settings.md#timeout-for-tests-to-complete) on the queue (new queues have 5 hours). With none, a re-run can leave a pull request testing indefinitely, for example when a required check is a commit status posted by a separate workflow. A pull request that runs past the timeout isn't re-run.

### Connect Buildkite

To re-run Buildkite jobs, an organization admin adds a Buildkite API key in your organization's **Settings** → **Integrations** → **Buildkite**. The key needs the scopes `read_builds`, `write_builds`, `read_pipelines` and `read_organizations`, and its user needs Build and Read access to your pipelines. If your Buildkite organization allows API keys only from certain IP addresses, contact Trunk support.

## Re-run from Trunk

Open the pull request in Trunk from **Merge Queue** → **\[your repository]**. A pull request in Pending Failure shows **Re-run failed checks**. Clicking it re-runs the failed checks now, after you confirm. While CI is still running, the menu beside the button also offers **Re-run when CI finishes**.

![The Re-run failed checks button with its menu open, offering Re-run now and Re-run when CI finishes.](https://mintcdn.com/trunk-4cab4936/nw3AhjIQ7agfFJMq/assets/merge-queue/using-the-queue/rerun-menu-light.png?fit=max\&auto=format\&n=nw3AhjIQ7agfFJMq\&q=85\&s=6c9dcf1d79e46d3ee4b2035a1820a622)

The menu beside Re-run failed checks.

## Automatic Re-runs

In **Merge Queue** → **\[your repository]** → **Settings** → **General**, under **Automatic Re-runs**, click **Add rule**, fill it in, then click **Update**. Each rule has:

* the jobs that start a re-run when they fail ([how to name them](#naming-jobs));
* when to re-run: **Re-run when CI finishes** or **Re-run now** (see [Choose when to re-run](#choose-when-to-re-run));
* how many times: **1 time** or **2 times**. **2 times** uses both re-runs, which leaves none for the button.

![The Allow Re-running Failed Checks setting enabled, a Re-run Time Limit of 30 minutes, and an Automatic Re-runs rule naming integration-tests and buildkite/app, set to re-run when CI finishes, 2 times.](https://mintcdn.com/trunk-4cab4936/nw3AhjIQ7agfFJMq/assets/merge-queue/using-the-queue/rerun-settings-light.png?fit=max\&auto=format\&n=nw3AhjIQ7agfFJMq\&q=85\&s=1e7144872a168abc40c86f4ac79fc811)

Allow Re-running Failed Checks, the Re-run Time Limit and an Automatic Re-runs rule in a queue's settings.

When a required check fails, Trunk looks for a failed job from your rules in the same GitHub Actions workflow run or Buildkite build. The job you name doesn't have to be a required check. If it finds one, Trunk re-runs every failed job in that run or build. If the re-run fails, Trunk tries again while the rule has re-runs left; then the pull request fails.

If both a GitHub Actions run and a Buildkite build failed, one re-run retries both and counts once. If any failed required check comes from CI Trunk can't re-run, Trunk re-runs nothing, and the pull request fails as usual.

If failed jobs match more than one rule, the rule highest in the list decides when to re-run and how many times. A job can be in only one rule.

### Naming jobs

A rule names checks exactly as GitHub shows them on the pull request. Click **Select jobs** and pick from the checks Trunk has seen on the repository, or type a name for a job that hasn't run yet. When you save, Trunk refuses a name whose latest result came from CI it can't re-run.

* **GitHub Actions**: the job's `name:` if it has one, else its key, without the workflow name or event, such as `integration`. A job with `name: Playwright` shows as `Playwright`, not by its key. A matrix job has one check per combination, such as `e2e (1)`, `e2e (2)` and `e2e (3)`, or `e2e (1, linux)` with several matrix keys. Name each one: `e2e` alone doesn't match them. When one shard fails, GitHub's re-run also re-runs the shards it canceled because of it.
* **Buildkite**: the commit status. A pipeline's build status is `buildkite/<pipeline-slug>` by default, such as `buildkite/app`, and a rule on it re-runs every failed job in the build. To start a re-run only when one step fails, make that step post its own status with Buildkite's `notify: github_commit_status` (set its name with `context`), and name that status.

### Buildkite's own retries

If your pipeline uses Buildkite's automatic retries, Buildkite reports a failure while its own retry still runs, and Trunk sees that failure straight away. Add a rule with **Re-run when CI finishes**: Trunk then waits for the build, and if Buildkite's retry passed, Trunk retries nothing and tests the pull request again. This uses one of the pull request's two re-runs.

On Buildkite, Trunk retries command steps that failed, timed out or were canceled. It doesn't retry soft-failed steps, or trigger and block steps. If the whole build was canceled, Trunk can't re-run it: rebuild it in Buildkite.

## Choose when to re-run

| | Re-run when CI finishes | Re-run now |
| - | - | - |
| **GitHub Actions** | Waits for the workflow run to finish, then re-runs its failed jobs and the jobs that depend on them. | Stops the workflow run's running jobs at once, then re-runs them with the failed jobs and the jobs that depend on them. |
| **Buildkite** | Waits for the build to finish, then retries its failed jobs. | Retries the jobs that have failed so far, at once. Jobs that are still running are not stopped, and a job that fails later is not retried. |

Choose **Re-run when CI finishes** when:

* your CI finishes well within the [Re-run Time Limit](#while-trunk-waits-for-the-re-run), which starts counting when the re-run is requested;
* on GitHub Actions, the rest of the run is likely to pass, so its jobs run once instead of being stopped and run again;
* on Buildkite, more jobs may still fail, or your pipeline uses Buildkite's automatic retries.

Choose **Re-run now** when the failed job is early and the rest of the run takes long, or when the rest of CI takes longer than the time limit. The pull request and the pull requests behind it wait less.

If CI has already finished, both re-run at once.

## While Trunk waits for the re-run

From the moment a re-run is requested (for Automatic Re-runs, the moment the job fails), Trunk keeps the pull request in Pending Failure and waits for the re-run to start. It waits up to the queue's **Re-run Time Limit**: 30 minutes by default, 60 at most. To stop waiting for CI, click **Re-run now** on the pull request's page.

Two clocks apply. The **Re-run Time Limit** covers the wait for the re-run to start. Once it starts, the pull request is testing again and gets the queue's full **Timeout for Tests to Complete**, counted from that moment: with a 60-minute timeout and a failure at minute 50, the re-run still has 60 minutes.

![A pull request page showing a Re-run requested badge and a Re-run now button above the failed required check integration-tests.](https://mintcdn.com/trunk-4cab4936/nw3AhjIQ7agfFJMq/assets/merge-queue/using-the-queue/rerun-held-light.png?fit=max\&auto=format\&n=nw3AhjIQ7agfFJMq\&q=85\&s=ec0b9d553dc8fd6b7a5e091663c937d3)

A pull request waiting for its re-run to start.

What happens to the pull requests behind it depends on your queue's settings:

* **By default**, they don't start testing or merge until the re-run finishes. Those already testing carry on.
* **With [anti-flake protection](https://docs.trunk.io/merge-queue/optimizations/anti-flake-protection.md)**, where the queue can merge a failed pull request once one behind it passes, they keep testing. If one passes first, both merge without waiting for the re-run.

If the re-run doesn't start in time, or the CI refuses it, Trunk stops waiting and the pull request fails as it would have without the re-run. Trunk's comment on the pull request says why.

## If a re-run runs out of time or gets stuck

A re-run can fail at the time limit, or never finish, when:

* **CI takes longer than the Re-run Time Limit.** With **Re-run when CI finishes**, waiting for the rest of CI counts toward the **Re-run Time Limit**. Raise it (up to 60 minutes), or use **Re-run now**.
* **A GitHub Actions required check and a Buildkite one both failed, with Re-run when CI finishes.** Trunk may not see the re-run start until the GitHub Actions re-run passes, so the pull request fails if that hasn't happened by the **Re-run Time Limit**. This is a known issue: set the limit to cover the rest of CI plus the GitHub Actions re-run.
* **The queue has no Timeout for Tests to Complete.** A re-run may never finish, as described [above](#turn-it-on). Set a timeout.

## Re-run in your CI

You can also re-run failed checks in your CI, for example with **Re-run failed jobs** in GitHub Actions, **Retry** in Buildkite, or **Retry** on a GitLab CI job. Trunk tests the pull request again once you've re-run every failed required check. For a commit status, such as Buildkite's, it waits until the status passes.

> [!NOTE]
>
> Trunk doesn't wait for a re-run you start yourself. Start it while the pull request is still in Pending Failure, or it fails first.

If Trunk is already waiting to re-run the pull request, a re-run you start in your CI counts as that re-run. A re-run you start in your CI works even after Trunk's two re-runs are used.

## When Trunk can't re-run

The **Re-run failed checks** button doesn't appear when:

* the queue doesn't allow re-runs;
* the pull request isn't in Pending Failure;
* it failed for a reason other than a required check, such as a timeout;
* the repository isn't on GitHub;
* you can't submit to the queue.

When the button is disabled, hover over it to see why. The API answers the same refusals with a `422` and the code shown.

| Message | API code | What to do |
| - | - | - |
| Trunk cannot re-run `name`. Re-run it in its own CI, or use Restart tests. | `PR_RERUN_CHECK_NOT_RERUNNABLE` | Re-run it in its CI. |
| Trunk cannot re-run `name` because Trunk has no valid API key for its Buildkite organization. Add one in Settings → Integrations → Buildkite. | `PR_RERUN_BUILDKITE_NOT_CONNECTED` | [Connect Buildkite](#connect-buildkite). |
| These checks were already re-run two times. Use Restart tests. | `PR_RERUN_HOLD_LIMIT_REACHED` | Re-run in your CI, or use **Restart tests**. |
| These checks cannot be re-run now. Use Restart tests. | `PR_RERUN_RUN_TORN_DOWN` | Trunk didn't keep this pull request's CI, for example because re-runs were turned on after it failed. Use **Restart tests**. |

If Trunk starts a re-run but can't finish it, Trunk's comment on the pull request says why, for example when Buildkite refuses the API key.

## Using the API

**Turn re-runs on and add a rule** with [`PATCH /v2/merge-queues/{id}/config`](https://docs.trunk.io/api/mergequeues/update-a-merge-queues-configuration), which needs the **Update Merge Queue settings** permission ([authentication](https://docs.trunk.io/merge-queue/using-the-queue/api.md#authentication), [finding the queue's id](https://docs.trunk.io/merge-queue/using-the-queue/api.md#finding-ids)). One request can do both:

```json theme={"system"}
{
  "failedTestRunTeardown": "keepForRerun",
  "autoRerunRules": [
    { "checks": ["e2e (1)", "e2e (2)", "e2e (3)", "buildkite/app"], "mode": "wait", "maxReruns": 1 }
  ]
}
```

`failedTestRunTeardown: "keepForRerun"` is **Allow Re-running Failed Checks**. Send `maxHoldMinutes` only to change the **Re-run Time Limit**. In a rule, `mode` is `wait` for **Re-run when CI finishes** or `now` for **Re-run now**, and `maxReruns` is **1 time** or **2 times**.

`autoRerunRules` replaces the queue's whole list of rules. To add a rule, read the current list from `config.autoRerunRules` in [`GET /v2/merge-queues/{id}`](https://docs.trunk.io/api/mergequeues/get-a-merge-queue), then send it with your rule added. A rule naming a check Trunk can't re-run, such as a Buildkite status before you [connect Buildkite](#connect-buildkite), is refused with `400 VALIDATION_FAILED`, and so are rules on a queue without `keepForRerun`.

**Re-run a pull request's failed checks** with [`POST /v2/pull-requests/{id}/rerun-failed-checks`](https://docs.trunk.io/api/pullrequests/re-run-a-pull-requests-failed-checks) and `{"mode": "wait"}` or `{"mode": "now"}`. It needs the **Merge and cancel PRs** permission. `{id}` is the pull request's id, not its number: see [Finding IDs](https://docs.trunk.io/merge-queue/using-the-queue/api.md#finding-ids). A repeated request is safe, and `now` switches a waiting re-run to start at once. Besides the codes in the table above, a `422` can be:

* `PR_RERUN_QUEUE_DOES_NOT_KEEP_FAILED_RUNS`: turn on **Allow Re-running Failed Checks**.
* `PR_RERUN_NOT_PENDING_FAILURE`: the pull request isn't in Pending Failure, for example because it's still testing. Try again once it is. With default settings that window lasts only while the pull requests ahead of it test, so use an Automatic Re-runs rule rather than polling.
* `PR_RERUN_NOT_A_REQUIRED_CHECK_FAILURE`: it failed for another reason, such as a timeout. Use **Restart tests**.
* `PR_RERUN_FORGE_NOT_SUPPORTED`: the repository isn't on GitHub.
* `PR_RERUN_FEATURE_DISABLED`: re-runs aren't available for this queue. Contact Trunk support.

A `409 PR_NOT_IN_ACTIVE_STATE` means the pull request has already failed or left the queue: resubmit it. A `404` means the pull request or its queue wasn't found. To restart instead, use [`POST /v2/pull-requests/{id}/restart-tests`](https://docs.trunk.io/api/pullrequests/restart-a-queued-pull-requests-tests).
