Skip to content

The agent that grows with you

fix(docker): stamp a commit-only identity when no release tag is reachable

ethernet86f42bf3b6ee
diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml
index a0c12e7d672..1d363717a5b 100644
--- a/.github/workflows/docker.yml
+++ b/.github/workflows/docker.yml
@@ -169,14 +169,17 @@ jobs:
run: |
display="$RELEASE_VERSION"
if [ -z "$display" ]; then
- display="$(python3 -c 'from pathlib import Path; from scripts.releases.distance import dev_version; print(dev_version(Path.cwd()))')"
+ display="$(python3 -m scripts.releases.distance)"
+ fi
+ identity=(--commit "$GITHUB_SHA")
+ if [ -n "$display" ]; then
+ base="${display%%+*}"
+ development="${display#*+}"
+ if [ "$development" = "$display" ]; then distance=0; else distance="${development%%.*}"; fi
+ identity+=(--base-version "$base" --display-version "$display" --distance "$distance")
fi
- base="${display%%+*}"
- development="${display#*+}"
- if [ "$development" = "$display" ]; then distance=0; else distance="${development%%.*}"; fi
python3 scripts/write_install_stamp.py --output install-stamp.json \
- --distribution docker --update-mechanism external --source ci \
- --base-version "$base" --display-version "$display" --distance "$distance"
+ --distribution docker --update-mechanism external --source ci "${identity[@]}"
- name: Reject profile exports in the build context
run: python3 scripts/ci/check_profile_archive_boundary.py
@@ -348,13 +351,16 @@ jobs:
- name: Write install stamp
run: |
- display="$(python3 -c 'from pathlib import Path; from scripts.releases.distance import dev_version; print(dev_version(Path.cwd()))')"
- base="${display%%+*}"
- development="${display#*+}"
- if [ "$development" = "$display" ]; then distance=0; else distance="${development%%.*}"; fi
+ display="$(python3 -m scripts.releases.distance)"
+ identity=(--commit "$GITHUB_SHA")
+ if [ -n "$display" ]; then
+ base="${display%%+*}"
+ development="${display#*+}"
+ if [ "$development" = "$display" ]; then distance=0; else distance="${development%%.*}"; fi
+ identity+=(--base-version "$base" --display-version "$display" --distance "$distance")
+ fi
python3 scripts/write_install_stamp.py --output install-stamp.json \
- --distribution docker --update-mechanism external --source ci \
- --base-version "$base" --display-version "$display" --distance "$distance"
+ --distribution docker --update-mechanism external --source ci "${identity[@]}"
- name: Reject profile exports in the build context
run: python3 scripts/ci/check_profile_archive_boundary.py
diff --git a/scripts/releases/distance.py b/scripts/releases/distance.py
index f9bf51aad48..a895de535e1 100644
--- a/scripts/releases/distance.py
+++ b/scripts/releases/distance.py
@@ -16,14 +16,26 @@ def _git(repo: Path, *args: str) -> str:
return subprocess.check_output(["git", *args], cwd=repo, text=True, encoding="utf-8").strip()
+class NoReleaseTag(ValueError):
+ """HEAD reaches no final release tag, so there is no base to count from."""
+
+
def dev_version(repo: Path) -> str:
"""``<release>+<distance>.g<short>`` from the highest reachable release tag."""
tags = [line for line in _git(repo, "tag", "--merged", "HEAD", "--list", "v[0-9]*").splitlines() if line]
releases = [version for version in (version_from_tag(tag) for tag in tags) if version]
if not releases:
- raise ValueError("no reachable release tag")
+ raise NoReleaseTag("no reachable release tag")
version = max(releases, key=lambda value: [int(part) for part in value.split(".")])
distance = _git(repo, "rev-list", "--count", f"v{version}..HEAD")
if distance == "0":
return version
return f"{version}+{distance}.g{_git(repo, 'rev-parse', '--short=7', 'HEAD')}"
+
+
+if __name__ == "__main__":
+ # Packagers stamp a commit-only identity when no release is reachable.
+ try:
+ print(dev_version(Path.cwd()))
+ except NoReleaseTag:
+ pass
diff --git a/scripts/write_install_stamp.py b/scripts/write_install_stamp.py
index d46fde30713..1ead30ca376 100644
--- a/scripts/write_install_stamp.py
+++ b/scripts/write_install_stamp.py
@@ -16,9 +16,10 @@ Usage::
--commit <sha> --branch <name> --dirty \\
--base-version 0.19.0 --distance 42 --source nix --distribution nix
- # Docker (identity admitted by the workflow):
+ # Docker (identity admitted by the workflow; with no reachable release the
+ # commit alone is the identity and the runtime reports base "unknown"):
python scripts/write_install_stamp.py --output install-stamp.json \\
- --commit <sha> --base-version 0.19.0 --distance 0 \\
+ --commit <sha> [--base-version 0.19.0 --distance 0] \\
--source ci --distribution docker --update-mechanism external
"""
@@ -141,6 +142,8 @@ def build_stamp(
f"write_install_stamp: invalid --update-mechanism {update_mechanism!r} "
f"(expected one of {', '.join(UPDATE_MECHANISMS)})"
)
+ # Only a caller-supplied commit may stand in for a missing release version.
+ commit_admitted = commit is not None
if channel_request is not None:
from scripts.bundles.desktop_prepare import git, require_source, validate_channel_request
channel_request = validate_channel_request(channel_request)
@@ -189,11 +192,14 @@ def build_stamp(
if dirty is None:
dirty = _resolve_dirty_from_git()
- if base_version is None:
+ if base_version is None and not commit_admitted:
raise ValueError(
- "install stamps require an explicit base version; leave local development trees "
- "unstamped so runtime identity can come from Git"
+ "install stamps require an explicit base version or commit; leave local development "
+ "trees unstamped so runtime identity can come from Git"
)
+ if base_version is None and display_version is None:
+ # No reachable release tag: record the commit, the way a tagless checkout reads from Git.
+ display_version = f"git.{commit[:7]}{'.dirty' if dirty else ''}"
# Commit date: explicit > git
if commit_date is None:
diff --git a/tests/scripts/test_write_install_stamp.py b/tests/scripts/test_write_install_stamp.py
index 632acee8a79..599f17cb597 100644
--- a/tests/scripts/test_write_install_stamp.py
+++ b/tests/scripts/test_write_install_stamp.py
@@ -80,18 +80,24 @@ def test_invalid_variant_cannot_emit_stamp(tmp_path, variant, tag, error):
assert not out.exists()
-def test_packaged_identity_requires_a_direct_version_and_ignores_project_metadata(tmp_path, monkeypatch):
+def test_packaged_identity_never_falls_back_to_project_metadata(tmp_path, monkeypatch):
from scripts import write_install_stamp
+ from hermes_cli.version_info import _stamp_version_info
(tmp_path / "hermes_cli").mkdir()
(tmp_path / "hermes_cli" / "_version.py").write_text('__version__ = "9.9.9"\n', encoding="utf-8")
(tmp_path / "pyproject.toml").write_text('[project]\nversion = "8.8.8"\n', encoding="utf-8")
monkeypatch.setattr(write_install_stamp, "_REPO_ROOT", tmp_path)
+ monkeypatch.setenv("HERMES_INSTALL_ROOT", str(tmp_path))
+
+ # No reachable release (a tagless docker build): the admitted commit is the whole identity.
+ write_install_stamp.write_stamp(
+ tmp_path / "install-stamp.json", update_mechanism="external", commit="d" * 40, source="ci", dirty=False,
+ )
+ info = _stamp_version_info()
+ assert info is not None
+ assert (info.base_version, info.derived_version, info.commit) == ("unknown", "git.ddddddd", "d" * 40)
- with pytest.raises(ValueError, match="base version"):
- write_install_stamp.build_stamp(
- update_mechanism="external", commit="d" * 40, source="ci",
- )
stamp = write_install_stamp.build_stamp(
update_mechanism="external", commit="d" * 40, source="ci",
base_version="1.2.3", distance=0,