Bright Labs

Does re-running a GitHub Actions job pick up the latest main? Why your PR is still red

The fix is merged to main, you hit re-run, and your pull request fails with the very same error. That's expected: a re-run uses the original run's commit SHA and ref, and target branch updates aren't visible to the workflow until the pull request is updated. To test against the fix, update your pull request branch with the Update branch button or gh pr update-branch, then check for a new run.

This one is easy to miss, because you can reasonably read re-run as "try again with how things are now". Read it as "try the same commit again". That's handy when you suspect a flake. If you need code that landed later, update the branch first.

What a re-run keeps from the original run

GitHub's re-run documentation says a re-run uses the same commit SHA and ref as the original event that triggered the run. So don't keep pressing the button in the hope of getting a newer main.

On a workflow triggered by pull_request, GITHUB_SHA is the last merge commit of the pull request's merge branch. Your CI tests run against the merged result, not only against your head branch.

Put those two facts together. For a pull_request run, that SHA is a merge commit, and the re-run uses the same SHA again. If the fix wasn't in that commit, don't expect the re-run to test it.

This behavior was reported in an actions/checkout issue titled "Checkout does not fetch the latest target branch when workflow re-run". A contributor there wrote that they want to preserve the deterministic nature of workflows with their context. The issue also says updates to the target branch aren't visible to the workflow until the pull request is updated.

Check whether your red run is older than the fix

Before you touch the branch, make sure this is your problem. Open the failing run and the pull request that fixed main.

If the run was triggered after the fix landed and it still fails, don't assume a stale merge. Read the log, and check the checkout step in your workflow, which is covered further down.

Update the pull request from the web

You can update the branch from the pull request page when there are no merge conflicts and the branch is behind the base branch. Open the pull request and click Update branch for a traditional merge.

If you'd rather keep a linear history, click Update with rebase, then click Rebase branch. Rebasing creates a linear history without a merge commit. If someone else is working on the same branch, tell them before you do this.

There are two catches. If there are conflicts, resolve them before updating the branch. And you may not be able to use the button if the head branch of your pull request is a protected branch.

Update the pull request with the gh CLI

If you live in the terminal, gh pr update-branch updates a pull request branch with the latest changes of the base branch. Without an argument, it selects the pull request that belongs to your current branch. By default it updates with a merge commit.

Example you write: update the current branch's pull request
gh pr update-branch

If you want a rebase instead, use the command's rebase option. It updates the pull request branch by rebasing on top of the latest base branch.

Pull the updated branch before you make your next local commit. Then open the pull request and look for a new run. Leave the old one alone.

If the new run never starts

By default, a pull_request workflow runs only for certain activity types. If a branch update starts nothing and your workflow sets its own types list, read that list first.

Next, look for conflicts. Workflows don't run on pull_request activity if the pull request has a merge conflict. You have to resolve the conflict first, then look for the run.

If a bot updates your branches with GITHUB_TOKEN, look for runs waiting on you. For opened, synchronize and reopened pull request events created with that token, GitHub creates the runs in an approval-required state. A user with write access to the repository can start them by selecting Approve workflows to run.

Read the checkout step if updating didn't help

The actions/checkout README shows how to check out the head commit, under "Checkout pull request HEAD commit instead of merge commit". Search your workflow file for this line.

From the actions/checkout README
ref: ${{ github.event.pull_request.head.sha }}

With that line, the job checks out your head commit instead of the merge commit. So check that your branch contains the fix, and update it if it doesn't. If the fix is in your history and the job still fails, treat it as a real failure. Start from the first error in the log.

When a re-run is still the right button

Re-run when you want to test the same commit again. A network timeout or a test you know is flaky are fair reasons. If you want different code tested, update the branch.

Which action fits your red check
Your situationWhat to do
You suspect a flake and need no new codeRe-run the failed jobs
The fix landed on the base branch after the run was triggeredUpdate the branch and check for a new run
The pull request has merge conflictsResolve the conflicts, then check for a new run
The base branch is still failing with the same errorWait for the fix or help fix it, then update the branch
The branch has the fix and the error remainsRead the log and treat it as a real failure until shown otherwise

Re-runs have limits. You can re-run for up to 30 days after the initial run, and a workflow run can be re-run a maximum of 50 times. When you re-run failed jobs, GitHub also re-runs the jobs that depend on them.

If you keep having to tell a flake from a real failure, FlakeRadar is one option. It's our Chrome extension, and it labels each failing GitHub Actions job on a pull request as Known flake, New failure, Also failing on the default branch, or Not enough history. It reads the repository's Actions history with a GitHub token you create. The label is evidence to start from, and you still update the branch yourself. The Free plan has labels for failing checks on public repositories, 30 days of history, and one-click re-run if you turn it on. Private repositories need Pro, at $19/month. FlakeRadar is not affiliated with or endorsed by GitHub.

Sources

Questions

Does Re-run all jobs use the latest main?

No. A re-run uses the same commit SHA and ref as the original event. Update the pull request branch to test against the fix.

Will updating the branch with a rebase work too?

Yes. The rebase option of gh pr update-branch rebases the branch on top of the latest base branch. On the web, click Update with rebase, then Rebase branch.

Whose permissions does a re-run use?

Re-runs use the privileges of the actor who initially triggered the workflow. If your job failed on permissions, check that person's privileges first.

Should I check out the head commit to avoid this?

Not for this problem. That option checks out the pull request's head commit instead of the merge commit, so you'd still want the fix in your branch. Start by updating the branch.

Related