From 8c7028327845bf280a56c0d98e8a2f3b88031a7b Mon Sep 17 00:00:00 2001 From: XZH Date: Sun, 23 Aug 2026 21:56:52 -0700 Subject: [PATCH 1/3] ci: ignore non-version release tags --- .github/workflows/docker-release.yml | 22 ++++++++++++++++++++++ .github/workflows/pypi-release.yml | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/.github/workflows/docker-release.yml b/.github/workflows/docker-release.yml index a6ad73fb7..4a993617e 100644 --- a/.github/workflows/docker-release.yml +++ b/.github/workflows/docker-release.yml @@ -19,8 +19,30 @@ permissions: packages: write jobs: + validate-release-tag: + name: Validate release tag + if: startsWith(github.event.release.tag_name, 'v') + runs-on: ubuntu-latest + steps: + - name: Require an image version tag + shell: python + run: | + import os + import re + + tag = os.environ["RELEASE_TAG"] + pattern = r"^v\d+\.\d+\.\d+(?:[0-9A-Za-z._+-]*)?$" + if not re.fullmatch(pattern, tag): + raise SystemExit( + f"Release tag {tag!r} is not an image version tag " + "(expected vX.Y.Z with an optional suffix)." + ) + env: + RELEASE_TAG: ${{ github.event.release.tag_name }} + build-and-push: name: Build and Push Multi-Platform Docker Image + needs: validate-release-tag runs-on: ubuntu-latest steps: diff --git a/.github/workflows/pypi-release.yml b/.github/workflows/pypi-release.yml index 5394bcfa0..f3fdcde62 100644 --- a/.github/workflows/pypi-release.yml +++ b/.github/workflows/pypi-release.yml @@ -26,8 +26,30 @@ concurrency: cancel-in-progress: false jobs: + validate-release-tag: + name: Validate release tag + if: startsWith(github.event.release.tag_name, 'v') + runs-on: ubuntu-latest + steps: + - name: Require a package version tag + shell: python + run: | + import os + import re + + tag = os.environ["RELEASE_TAG"] + pattern = r"^v\d+\.\d+\.\d+(?:[0-9A-Za-z._+-]*)?$" + if not re.fullmatch(pattern, tag): + raise SystemExit( + f"Release tag {tag!r} is not a package version tag " + "(expected vX.Y.Z with an optional suffix)." + ) + env: + RELEASE_TAG: ${{ github.event.release.tag_name }} + build-and-publish: name: Build and Publish PyPI Package + needs: validate-release-tag runs-on: ubuntu-latest environment: name: pypi From 40e35a79b345d3c4e574f783f3a3fc9357366f56 Mon Sep 17 00:00:00 2001 From: XZH Date: Mon, 24 Aug 2026 07:38:32 -0700 Subject: [PATCH 2/3] test(ci): cover release tag classification --- tests/test_release_workflow_guards.py | 91 +++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 tests/test_release_workflow_guards.py diff --git a/tests/test_release_workflow_guards.py b/tests/test_release_workflow_guards.py new file mode 100644 index 000000000..1b1e82ed8 --- /dev/null +++ b/tests/test_release_workflow_guards.py @@ -0,0 +1,91 @@ +"""Contract tests for release-tag gating in publish workflows.""" + +from __future__ import annotations + +import os +from pathlib import Path +import subprocess +import sys + +import pytest +import yaml + +REPOSITORY_ROOT = Path(__file__).resolve().parents[1] +RELEASE_WORKFLOWS = { + "docker": ( + REPOSITORY_ROOT / ".github" / "workflows" / "docker-release.yml", + "build-and-push", + ), + "pypi": ( + REPOSITORY_ROOT / ".github" / "workflows" / "pypi-release.yml", + "build-and-publish", + ), +} + + +def _workflow(publication: str) -> tuple[dict, str]: + workflow_path, publish_job_name = RELEASE_WORKFLOWS[publication] + with workflow_path.open(encoding="utf-8") as file: + return yaml.safe_load(file), publish_job_name + + +def _validator_script(publication: str) -> str: + document, _ = _workflow(publication) + validator = document["jobs"]["validate-release-tag"] + return validator["steps"][0]["run"] + + +@pytest.mark.parametrize("publication", RELEASE_WORKFLOWS) +def test_non_version_release_tags_skip_publication(publication: str) -> None: + document, publish_job_name = _workflow(publication) + validator = document["jobs"]["validate-release-tag"] + + assert validator["if"] == "startsWith(github.event.release.tag_name, 'v')" + assert document["jobs"][publish_job_name]["needs"] == "validate-release-tag" + + +@pytest.mark.parametrize( + ("publication", "tag"), + [ + ("docker", "v1.2.3"), + ("docker", "v1.2.3rc1"), + ("docker", "v1.2.3+build.1"), + ("pypi", "v1.2.3"), + ("pypi", "v1.2.3rc1"), + ("pypi", "v1.2.3+build.1"), + ], +) +def test_version_release_tags_pass_the_guard(publication: str, tag: str) -> None: + result = subprocess.run( + [sys.executable, "-c", _validator_script(publication)], + env={"RELEASE_TAG": tag, "PATH": os.environ["PATH"]}, + capture_output=True, + text=True, + check=False, + ) + + assert result.returncode == 0, result.stderr + + +@pytest.mark.parametrize( + ("publication", "tag"), + [ + ("docker", "vmain"), + ("docker", "v1.2"), + ("docker", "v1.2.x"), + ("pypi", "vmain"), + ("pypi", "v1.2"), + ("pypi", "v1.2.x"), + ], +) +def test_malformed_version_tags_fail_the_guard(publication: str, tag: str) -> None: + result = subprocess.run( + [sys.executable, "-c", _validator_script(publication)], + env={"RELEASE_TAG": tag, "PATH": os.environ["PATH"]}, + capture_output=True, + text=True, + check=False, + ) + + assert result.returncode != 0 + assert repr(tag) in result.stderr From 48c39084cfff7597a7650cb15135d90cfe923a39 Mon Sep 17 00:00:00 2001 From: "Bingxi Zhao (Frank)" <13051345080@163.com> Date: Wed, 26 Aug 2026 02:26:56 +0800 Subject: [PATCH 3/3] fix(ci): validate release tags and protect latest --- .github/workflows/docker-release.yml | 30 +++++++++++--- .github/workflows/pypi-release.yml | 8 +++- tests/test_release_workflow_guards.py | 58 +++++++++++++++++++-------- 3 files changed, 73 insertions(+), 23 deletions(-) diff --git a/.github/workflows/docker-release.yml b/.github/workflows/docker-release.yml index 4a993617e..a0e2d9b95 100644 --- a/.github/workflows/docker-release.yml +++ b/.github/workflows/docker-release.yml @@ -23,20 +23,39 @@ jobs: name: Validate release tag if: startsWith(github.event.release.tag_name, 'v') runs-on: ubuntu-latest + outputs: + image_tag: ${{ steps.validate.outputs.image_tag }} + is_stable: ${{ steps.validate.outputs.is_stable }} steps: - name: Require an image version tag + id: validate shell: python run: | import os import re tag = os.environ["RELEASE_TAG"] - pattern = r"^v\d+\.\d+\.\d+(?:[0-9A-Za-z._+-]*)?$" + pattern = ( + r"^v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)" + r"(?:(?:a|b|rc)(?:0|[1-9]\d*))?" + r"(?:\.post(?:0|[1-9]\d*))?" + r"(?:\.dev(?:0|[1-9]\d*))?" + r"(?:\+[0-9A-Za-z]+(?:[._-][0-9A-Za-z]+)*)?$" + ) if not re.fullmatch(pattern, tag): raise SystemExit( f"Release tag {tag!r} is not an image version tag " "(expected vX.Y.Z with an optional suffix)." ) + # '+' is valid version metadata but not valid in an OCI tag. + image_tag = tag[1:].replace("+", "-") + with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output: + output.write(f"image_tag={image_tag}\n") + output.write( + "is_stable=" + + str(bool(re.fullmatch(r"v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)", tag))).lower() + + "\n" + ) env: RELEASE_TAG: ${{ github.event.release.tag_name }} @@ -64,17 +83,16 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - # Extract tags from the release: - # type=semver strips the leading 'v' (v1.2.3 → 1.2.3) - # type=raw adds a 'latest' tag on every published release + # The validated version drops the leading 'v'. Pre-releases retain a + # versioned image but never replace the stable 'latest' tag. - name: Extract Docker metadata id: meta uses: docker/metadata-action@v5 with: images: ghcr.io/hkuds/deeptutor tags: | - type=semver,pattern={{version}} - type=raw,value=latest + type=raw,value=${{ needs.validate-release-tag.outputs.image_tag }} + type=raw,value=latest,enable=${{ github.event.release.prerelease == false && needs.validate-release-tag.outputs.is_stable == 'true' }} - name: Build and push uses: docker/build-push-action@v6 diff --git a/.github/workflows/pypi-release.yml b/.github/workflows/pypi-release.yml index f3fdcde62..389855de5 100644 --- a/.github/workflows/pypi-release.yml +++ b/.github/workflows/pypi-release.yml @@ -38,7 +38,13 @@ jobs: import re tag = os.environ["RELEASE_TAG"] - pattern = r"^v\d+\.\d+\.\d+(?:[0-9A-Za-z._+-]*)?$" + pattern = ( + r"^v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)" + r"(?:(?:a|b|rc)(?:0|[1-9]\d*))?" + r"(?:\.post(?:0|[1-9]\d*))?" + r"(?:\.dev(?:0|[1-9]\d*))?" + r"(?:\+[0-9A-Za-z]+(?:[._-][0-9A-Za-z]+)*)?$" + ) if not re.fullmatch(pattern, tag): raise SystemExit( f"Release tag {tag!r} is not a package version tag " diff --git a/tests/test_release_workflow_guards.py b/tests/test_release_workflow_guards.py index 1b1e82ed8..740e3558b 100644 --- a/tests/test_release_workflow_guards.py +++ b/tests/test_release_workflow_guards.py @@ -35,6 +35,21 @@ def _validator_script(publication: str) -> str: return validator["steps"][0]["run"] +def _run_validator(publication: str, tag: str, tmp_path: Path) -> subprocess.CompletedProcess[str]: + output = tmp_path / f"{publication}-output.txt" + return subprocess.run( + [sys.executable, "-c", _validator_script(publication)], + env={ + "RELEASE_TAG": tag, + "GITHUB_OUTPUT": str(output), + "PATH": os.environ["PATH"], + }, + capture_output=True, + text=True, + check=False, + ) + + @pytest.mark.parametrize("publication", RELEASE_WORKFLOWS) def test_non_version_release_tags_skip_publication(publication: str) -> None: document, publish_job_name = _workflow(publication) @@ -55,14 +70,8 @@ def test_non_version_release_tags_skip_publication(publication: str) -> None: ("pypi", "v1.2.3+build.1"), ], ) -def test_version_release_tags_pass_the_guard(publication: str, tag: str) -> None: - result = subprocess.run( - [sys.executable, "-c", _validator_script(publication)], - env={"RELEASE_TAG": tag, "PATH": os.environ["PATH"]}, - capture_output=True, - text=True, - check=False, - ) +def test_version_release_tags_pass_the_guard(publication: str, tag: str, tmp_path: Path) -> None: + result = _run_validator(publication, tag, tmp_path) assert result.returncode == 0, result.stderr @@ -73,19 +82,36 @@ def test_version_release_tags_pass_the_guard(publication: str, tag: str) -> None ("docker", "vmain"), ("docker", "v1.2"), ("docker", "v1.2.x"), + ("docker", "v01.2.3"), + ("docker", "v1.2.3+"), + ("docker", "v1.2.3...."), ("pypi", "vmain"), ("pypi", "v1.2"), ("pypi", "v1.2.x"), + ("pypi", "v01.2.3"), + ("pypi", "v1.2.3+"), + ("pypi", "v1.2.3...."), ], ) -def test_malformed_version_tags_fail_the_guard(publication: str, tag: str) -> None: - result = subprocess.run( - [sys.executable, "-c", _validator_script(publication)], - env={"RELEASE_TAG": tag, "PATH": os.environ["PATH"]}, - capture_output=True, - text=True, - check=False, - ) +def test_malformed_version_tags_fail_the_guard(publication: str, tag: str, tmp_path: Path) -> None: + result = _run_validator(publication, tag, tmp_path) assert result.returncode != 0 assert repr(tag) in result.stderr + + +def test_docker_uses_validated_tag_and_stable_latest_only(tmp_path: Path) -> None: + result = _run_validator("docker", "v1.2.3+build.1", tmp_path) + assert result.returncode == 0, result.stderr + assert (tmp_path / "docker-output.txt").read_text() == ( + "image_tag=1.2.3-build.1\nis_stable=false\n" + ) + + document, _ = _workflow("docker") + metadata = next( + step for step in document["jobs"]["build-and-push"]["steps"] if step.get("id") == "meta" + ) + tags = metadata["with"]["tags"] + assert "needs.validate-release-tag.outputs.image_tag" in tags + assert "github.event.release.prerelease == false" in tags + assert "needs.validate-release-tag.outputs.is_stable == 'true'" in tags