Skip to main content

docker

docker

Prepare Docker build variables to match the legacy bash script.

Classes

Name Description
RegistryTag Published state of an image tag in a registry.

RegistryTag

docker.RegistryTag(last_updated, platforms)

Published state of an image tag in a registry.

Attributes

Name Type Description
last_updated str ISO8601 time the tag was last published, or an empty string when the registry does not report it.
platforms set[str] Published os/arch[/variant] platforms.

Functions

Name Description
base_image_name Get base image names from a Dockerfile.
dockerfile_last_commit_iso Get the last git commit timestamp for the Dockerfile.
dockerhub_tag_info Get the published state of a Docker Hub tag.
dockerhub_tag_last_updated Get Docker Hub tag last_updated timestamp.
evaluate_docker_build_staleness Decide whether to build a Docker image based on Dockerfile git history and tag freshness.
evaluate_docker_build_staleness_and_set_outputs Evaluate Docker build staleness and set step outputs.
format_platform Format a platform as os/arch[/variant].
ghcr_tag_info Get the published state of a GitHub Container Registry tag.
image_tag_from_image_name Extract the tag suffix from a Docker image name.
parse_iso_timestamp Parse an ISO timestamp and normalize it to UTC.
platform_is_published Check whether a requested platform is among the published platforms.
prepare_docker_build_variables Prepare Docker build variables and optionally write them to GITHUB_ENV.
registry_build_reason Explain why a registry tag needs rebuilding.
tag_from_dockerfile Extract the tag from the Dockerfile name (last extension).

base_image_name

docker.base_image_name(dockerfile)

Get base image names from a Dockerfile.

Matches the bash behavior: only lines starting with “FROM” are used and the literal “FROM” substring is removed everywhere on that line.

dockerfile_last_commit_iso

docker.dockerfile_last_commit_iso(dockerfile_path)

Get the last git commit timestamp for the Dockerfile.

Parameters

Name Type Description Default
dockerfile_path str Path to the Dockerfile in the repository. required

Returns

Name Type Description
str str ISO8601 timestamp from git %cI format, or an empty string
str when the file is missing, untracked, or has no git history.

dockerhub_tag_info

docker.dockerhub_tag_info(
    dockerhub_namespace,
    repo_name,
    image_tag,
    timeout=20,
    session=requests,
)

Get the published state of a Docker Hub tag.

Parameters

Name Type Description Default
dockerhub_namespace str Docker Hub namespace/org. required
repo_name str Docker Hub repository name. required
image_tag str Docker image tag. required
timeout int HTTP timeout in seconds. 20
session Object with a requests-compatible get method. requests

Returns

Name Type Description
RegistryTag | None RegistryTag or None: Tag state, or None when the tag is absent.

dockerhub_tag_last_updated

docker.dockerhub_tag_last_updated(
    dockerhub_namespace,
    repo_name,
    image_tag,
    timeout=20,
    session=requests,
)

Get Docker Hub tag last_updated timestamp.

Parameters

Name Type Description Default
dockerhub_namespace str Docker Hub namespace/org. required
repo_name str Docker Hub repository name. required
image_tag str Docker image tag. required
timeout int HTTP timeout in seconds. 20
session Object with a requests-compatible get method. requests

Returns

Name Type Description
str | None str or None: ISO8601 last_updated value, or None when tag is absent.

evaluate_docker_build_staleness

docker.evaluate_docker_build_staleness(
    dockerfile_path,
    image_name,
    dockerhub_namespace,
    repo_name,
    registries='dockerhub',
    ghcr_namespace='',
    platforms='',
    github_token='',
)

Decide whether to build a Docker image based on Dockerfile git history and tag freshness.

The build is skipped only when every selected registry already has the tag, the tag is at least as new as the Dockerfile’s last commit, and it includes every requested platform.

Parameters

Name Type Description Default
dockerfile_path str Path to Dockerfile in repository. required
image_name str Target image name, including tag. required
dockerhub_namespace str Docker Hub namespace/org. required
repo_name str Image repository name. required
registries str Comma-separated registries to check (dockerhub, ghcr). 'dockerhub'
ghcr_namespace str GHCR owner or organization. ''
platforms str Comma-separated requested platforms; empty to skip platform checks. ''
github_token str Token for reading private GHCR packages. ''

Returns

Name Type Description
dict dict[str, str] Build decision fields suitable for GitHub Action outputs.

evaluate_docker_build_staleness_and_set_outputs

docker.evaluate_docker_build_staleness_and_set_outputs(
    dockerfile_path,
    image_name,
    dockerhub_namespace,
    repo_name,
    registries='dockerhub',
    ghcr_namespace='',
    platforms='',
    github_token='',
)

Evaluate Docker build staleness and set step outputs.

Parameters

Name Type Description Default
dockerfile_path str Path to Dockerfile in repository. required
image_name str Target image name, including tag. required
dockerhub_namespace str Docker Hub namespace/org. required
repo_name str Image repository name. required
registries str Comma-separated registries to check (dockerhub, ghcr). 'dockerhub'
ghcr_namespace str GHCR owner or organization. ''
platforms str Comma-separated requested platforms. ''
github_token str Token for reading private GHCR packages. ''

Returns

Name Type Description
dict dict[str, str] Build decision fields written to GitHub Action outputs.

format_platform

docker.format_platform(os_name, architecture, variant='')

Format a platform as os/arch[/variant].

Parameters

Name Type Description Default
os_name str Operating system, e.g. linux. required
architecture str CPU architecture, e.g. arm64. required
variant str CPU variant, e.g. v7. ''

Returns

Name Type Description
str str Platform string.

ghcr_tag_info

docker.ghcr_tag_info(
    ghcr_namespace,
    repo_name,
    image_tag,
    github_token='',
    timeout=20,
    session=requests,
)

Get the published state of a GitHub Container Registry tag.

The publish time is the oldest image config created time among the tag’s platform images, which buildx sets to the build time.

Parameters

Name Type Description Default
ghcr_namespace str GHCR owner or organization. required
repo_name str GHCR image repository name. required
image_tag str Docker image tag. required
github_token str Token for private packages; public packages are read anonymously when empty. ''
timeout int HTTP timeout in seconds. 20
session Object with a requests-compatible get method. requests

Returns

Name Type Description
RegistryTag | None RegistryTag or None: Tag state, or None when the tag is absent.

image_tag_from_image_name

docker.image_tag_from_image_name(image_name)

Extract the tag suffix from a Docker image name.

Parameters

Name Type Description Default
image_name str Docker image reference, optionally including a tag. required

Returns

Name Type Description
str str Tag string, or an empty string if no tag is present.

parse_iso_timestamp

docker.parse_iso_timestamp(raw)

Parse an ISO timestamp and normalize it to UTC.

Parameters

Name Type Description Default
raw str ISO8601 timestamp (for example 2026-01-01T12:00:00Z). required

Returns

Name Type Description
datetime.datetime datetime.datetime: Timezone-aware UTC datetime.

platform_is_published

docker.platform_is_published(requested, published)

Check whether a requested platform is among the published platforms.

A requested platform without a variant matches any published variant of the same OS and architecture (e.g. linux/arm64 matches linux/arm64/v8).

Parameters

Name Type Description Default
requested str Requested os/arch[/variant] platform. required
published set[str] Published platforms. required

Returns

Name Type Description
bool bool Whether the requested platform is published.

prepare_docker_build_variables

docker.prepare_docker_build_variables(
    dockerfile,
    suffix,
    dockerhub_account,
    github_env=None,
    now=None,
)

Prepare Docker build variables and optionally write them to GITHUB_ENV.

For a root-level Dockerfile, derive the image repository name from GITHUB_REPOSITORY and write the README path relative to the workspace.

Parameters

Name Type Description Default
dockerfile str Path to the Dockerfile. required
suffix str Suffix for the image tag (e.g., “dev”, “main”). required
dockerhub_account str Docker Hub account/namespace. required
github_env str Path to the GitHub Actions env file. None
now datetime Override current time for deterministic output. None

Returns

Name Type Description
dict dict[str, str] Mapping of variable names to their values.

registry_build_reason

docker.registry_build_reason(tag, dockerfile_last_commit, platforms)

Explain why a registry tag needs rebuilding.

Parameters

Name Type Description Default
tag RegistryTag or None Published tag state, or None when absent. required
dockerfile_last_commit str ISO8601 time of the Dockerfile’s last commit. required
platforms list[str] Requested platforms; empty to skip platform checks. required

Returns

Name Type Description
str str Build reason, or an empty string when the tag is up-to-date.

tag_from_dockerfile

docker.tag_from_dockerfile(dockerfile)

Extract the tag from the Dockerfile name (last extension).

Mirrors bash ${bn_dockerfile##*.} behavior.