From 76da863a021b64acecc9532d5986f30a0d923405 Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Sun, 21 Jun 2026 02:53:28 -0700 Subject: [PATCH] fix docker image commit metadata --- .github/workflows/docker.yml | 1 + Dockerfile | 5 ++++- archivebox/config/version.py | 6 +++--- archivebox/tests/test_version_metadata.py | 19 +++++++++++++++++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index ffd2b936..21e4ffef 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -187,6 +187,7 @@ jobs: labels: ${{ steps.docker_meta.outputs.labels }} build-args: | ABX_DL_IMAGE=${{ steps.abx_dl_image.outputs.image }} + ARCHIVEBOX_COMMIT_HASH=${{ github.sha }} cache-from: type=gha,scope=${{ matrix.cache_scope }} cache-to: type=gha,mode=max,scope=${{ matrix.cache_scope }} pull: true diff --git a/Dockerfile b/Dockerfile index 5ede94c7..241f8584 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,6 +22,7 @@ ARG TARGETPLATFORM ARG TARGETOS ARG TARGETARCH ARG TARGETVARIANT +ARG ARCHIVEBOX_COMMIT_HASH="" ENV TZ=UTC \ LANGUAGE=en_US:en \ @@ -165,7 +166,9 @@ COPY --chown=root:root --chmod=755 "." "$CODE_DIR/" RUN --mount=type=cache,target=/root/.cache/uv,sharing=locked,id=uv-$TARGETARCH$TARGETVARIANT \ echo "[*] Installing ArchiveBox Python source code from $CODE_DIR..." \ && COMMIT_HASH="$( \ - if [[ -f "$CODE_DIR/.git/HEAD" ]]; then \ + if [[ "$ARCHIVEBOX_COMMIT_HASH" =~ ^[0-9a-fA-F]{40}$ ]]; then \ + echo "$ARCHIVEBOX_COMMIT_HASH"; \ + elif [[ -f "$CODE_DIR/.git/HEAD" ]]; then \ HEAD_REF="$(cat "$CODE_DIR/.git/HEAD")"; \ if [[ "$HEAD_REF" =~ ^[0-9a-fA-F]{40}$ ]]; then \ echo "$HEAD_REF"; \ diff --git a/archivebox/config/version.py b/archivebox/config/version.py index 81f4f721..eeb526cd 100644 --- a/archivebox/config/version.py +++ b/archivebox/config/version.py @@ -50,9 +50,9 @@ def get_COMMIT_HASH() -> str | None: if IN_DOCKER: try: version_text = Path("/VERSION.txt").read_text() - match = re.search(r"^COMMIT_HASH=([0-9a-fA-F]{40})$", version_text, re.MULTILINE) - if match: - return match.group(1) + matches = re.findall(r"^COMMIT_HASH=([0-9a-fA-F]{40})$", version_text, re.MULTILINE) + if matches: + return matches[-1] except Exception: pass diff --git a/archivebox/tests/test_version_metadata.py b/archivebox/tests/test_version_metadata.py index d112eaed..c60577f1 100644 --- a/archivebox/tests/test_version_metadata.py +++ b/archivebox/tests/test_version_metadata.py @@ -34,6 +34,25 @@ def test_get_commit_hash_from_docker_version_file_ignores_short_hash(monkeypatch assert version.get_COMMIT_HASH() == commit_hash +def test_get_commit_hash_from_docker_version_file_prefers_last_full_hash(monkeypatch) -> None: + base_hash = "a" * 40 + archivebox_hash = "b" * 40 + + class VersionPath: + def __init__(self, path: str): + self.path = path + + def read_text(self) -> str: + assert self.path == "/VERSION.txt" + return f"COMMIT_HASH={base_hash}\nCOMMIT_HASH={archivebox_hash}\n" + + monkeypatch.setattr(version, "IN_DOCKER", True) + monkeypatch.setattr(version, "Path", VersionPath) + version.get_COMMIT_HASH.cache_clear() + + assert version.get_COMMIT_HASH() == archivebox_hash + + def test_get_commit_hash_from_detached_head(monkeypatch, tmp_path) -> None: commit_hash = "a" * 40 package_dir = tmp_path / "archivebox"