Learning center

An image built on a pull request names a commit that is on no branch

On a pull_request run, github.sha is a merge commit that GitHub created for the run. The common Docker metadata action writes it into the image's org.opencontainers.image.revision label. That commit is on neither branch, and the commit that later lands on the base branch is a different one. Before you ship or debug such an image, read the label, then the commit's parents, and decide whether the image is a release candidate at all.

Where the label comes from#

GitHub's documentation gives GITHUB_SHA for pull_request as the "Last merge commit on the GITHUB_REF branch", where the ref is refs/pull/PULL_REQUEST_NUMBER/merge. For pull_request_target it is the last commit on the default branch, which is not the pull request's code either. Events that trigger workflows

docker/metadata-action reads that SHA from the workflow context by default and writes it into the revision label and into type=sha tags such as sha-860c190. Setting DOCKER_METADATA_PR_HEAD_SHA: true makes it use the pull request's head SHA instead. metadata-action environment variables The OCI annotation defines the label as the "Source control revision identifier for the packaged software", so a reader takes it as the commit the image was built from. OCI pre-defined annotation keys

The head SHA option fixes the label only if the build also checked out the head. With the default checkout, the files still come from the merge commit, so the label then names a commit whose files the image does not contain. Which commit a pull request's tests ran on explains the checkout.

Read the label and the commit's parents#

This fixture builds the merge commit that GitHub would create, labels an image with it as the action does, and reads it back (Git 2.43.0, Docker 29.1.3):

$ docker inspect fx-label:pr --format '{{index .Config.Labels "org.opencontainers.image.revision"}}'
b636259b9eda7dadd9dd246cf6a19c3714006542
$ git branch -a --contains b636259b9eda7dadd9dd246cf6a19c3714006542
(exit 0)
$ git log -1 --format='%H parents %P' b636259b9eda7dadd9dd246cf6a19c3714006542
b636259b9eda7dadd9dd246cf6a19c3714006542 parents be2f6be6fcfd64cebab2cef1e8727326bc142b4c 81f67a151f5330c6580ab0a171280610bb3e9ceb
$ git rev-parse main feature
be2f6be6fcfd64cebab2cef1e8727326bc142b4c
81f67a151f5330c6580ab0a171280610bb3e9ceb

git branch --contains prints nothing and still exits 0, so a script that checks only the exit status accepts the label. The parents identify the inputs. The first is the base tip when GitHub computed the merge and the second is the pull request's head.

An empty branch list has other causes. A head that was force-pushed away and a branch deleted after merge also leave a commit on no branch. Two parents, with the second equal to a pull request head, mark a CI merge commit. A single parent points to one of the other cases. If git log cannot find the commit, fetch it by SHA before you conclude anything.

Decide whether to ship it#

Image built onRevision label namesShip it as a release?
pull_request, default settingsThe CI merge commitNo. The commit that lands on the base branch has a different ID and may have a different base
pull_request with the head SHA option and default checkoutThe head, but the files came from the merge commitNo. The label and the contents disagree
push to the release branchThe pushed commitYes, if this digest is the one your tests ran on

Build release images from the commit on the release branch, and keep pull request images under tags that cannot be mistaken for releases. If you must keep a pull request image, add the two parents as extra labels, so a reader does not have to recover them from Git.

Worked example#

This example is fictional. During an incident, Dara reads the revision label on the running image and cannot find the commit on main. Its parents are a main commit from Tuesday and the head of Chen's pull request. The image came from a preview deployment that someone promoted by tag. The code on main came from a later merge with a newer base, so Dara compares the tree of that merge with the tree of the labelled commit before she uses main to explain the failure.

For how to check that a merged commit matches the tested one, read the merge queue guide. For the digest a deployment runs, read which image digest is running.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading