Skip to content

Commit 2747c1c

Browse files
fix(ci): validate release source before building distributions (#5220)
1 parent e80737c commit 2747c1c

2 files changed

Lines changed: 195 additions & 8 deletions

File tree

‎.github/workflows/publish.yml‎

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ concurrency:
1212
cancel-in-progress: false
1313

1414
jobs:
15-
build:
15+
checks:
1616
permissions:
1717
contents: read
1818
runs-on: ubuntu-latest
19-
outputs:
20-
artifact-id: ${{ steps.upload.outputs.artifact-id }}
2119

2220
steps:
2321
# Tag rules and environment protection remain the release authorization boundary.
@@ -52,11 +50,60 @@ jobs:
5250
version: "0.11.14"
5351
enable-cache: false
5452
python-version: "3.14"
55-
- name: Install dependencies
53+
- name: Check release source
5654
working-directory: release-source
55+
shell: bash
5756
env:
5857
OPENAI_API_KEY: fake-for-tests
59-
run: make sync
58+
UV_LOCKED: "1"
59+
run: |
60+
for python_version in 3.10 3.11 3.12 3.13 3.14; do
61+
UV_PYTHON="$python_version" make sync tests
62+
done
63+
make typecheck
64+
65+
build:
66+
# Test processes and filesystem mutations stay on the checks runner.
67+
needs: checks
68+
permissions:
69+
contents: read
70+
runs-on: ubuntu-latest
71+
outputs:
72+
artifact-id: ${{ steps.upload.outputs.artifact-id }}
73+
74+
steps:
75+
# Tag rules and environment protection remain the release authorization boundary.
76+
- name: Checkout release validator from main
77+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
78+
with:
79+
ref: refs/heads/main
80+
path: control
81+
persist-credentials: false
82+
sparse-checkout: .github/scripts
83+
- name: Checkout release commit
84+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
85+
with:
86+
ref: ${{ github.sha }}
87+
path: release-source
88+
fetch-depth: 0
89+
persist-credentials: false
90+
- name: Setup Python for release validation
91+
uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97
92+
with:
93+
python-version: "3.14"
94+
- name: Validate release provenance
95+
env:
96+
RELEASE_TAG: ${{ github.event.release.tag_name }}
97+
RELEASE_SHA: ${{ github.sha }}
98+
run: >-
99+
python -I control/.github/scripts/verify_release.py
100+
--repo release-source --tag "$RELEASE_TAG" --expected-sha "$RELEASE_SHA"
101+
- name: Setup uv
102+
uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9 # setup-uv v9.0.0; uv 0.11.14
103+
with:
104+
version: "0.11.14"
105+
enable-cache: false
106+
python-version: "3.14"
60107
- name: Build package
61108
working-directory: release-source
62109
run: uv build

‎tests/test_repository_workflow_interfaces.py‎

Lines changed: 143 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
from __future__ import annotations
22

3+
import os
34
import re
45
import runpy
6+
import shutil
7+
import subprocess
8+
import sys
59
from pathlib import Path
610

11+
import pytest
12+
import yaml
13+
714
ROOT = Path(__file__).resolve().parents[1]
815
MAKEFILE = ROOT / "Makefile"
916
TESTS_WORKFLOW = ROOT / ".github" / "workflows" / "tests.yml"
@@ -169,8 +176,9 @@ def test_prospective_contract_preparation_removes_api_key_before_uv() -> None:
169176
assert recipe.index("unset OPENAI_API_KEY") < recipe.index("uv run")
170177

171178

172-
def test_release_build_validates_before_executing_candidate_code() -> None:
173-
build = _workflow_job("build", PUBLISH_WORKFLOW)
179+
@pytest.mark.parametrize("job_name", ["checks", "build"])
180+
def test_release_build_validates_before_executing_candidate_code(job_name: str) -> None:
181+
build = _workflow_job(job_name, PUBLISH_WORKFLOW)
174182

175183
assert "contents: read" in build
176184
assert "id-token:" not in build
@@ -180,11 +188,143 @@ def test_release_build_validates_before_executing_candidate_code() -> None:
180188
assert build.count("persist-credentials: false") == 2
181189
assert "fetch-depth: 0" in build
182190
validation = build.index("python -I control/.github/scripts/verify_release.py")
183-
assert validation < build.index("run: make sync") < build.index("run: uv build")
191+
candidate_command = (
192+
'UV_PYTHON="$python_version" make sync tests' if job_name == "checks" else "run: uv build"
193+
)
194+
assert validation < build.index(candidate_command)
184195
assert ' --tag "$RELEASE_TAG" --expected-sha "$RELEASE_SHA"' in build
185196
assert "enable-cache: false" in build
186197

187198

199+
@pytest.mark.parametrize("failed_check", [None, "3.12:sync tests", ":typecheck"])
200+
def test_release_checks_fail_closed(tmp_path: Path, failed_check: str | None) -> None:
201+
bash = shutil.which("bash")
202+
if bash is None:
203+
pytest.skip("The publish workflow requires Bash.")
204+
workflow = yaml.safe_load(PUBLISH_WORKFLOW.read_text(encoding="utf-8"))
205+
jobs = workflow["jobs"]
206+
checks = jobs["checks"]
207+
build = jobs["build"]
208+
steps = checks["steps"]
209+
check_index = next(i for i, step in enumerate(steps) if step["name"] == "Check release source")
210+
check = steps[check_index]
211+
package = next(step for step in build["steps"] if step.get("run") == "uv build")
212+
assert check["working-directory"] == package["working-directory"] == "release-source"
213+
assert build["needs"] == "checks"
214+
assert check["shell"] == "bash"
215+
assert check["env"] == {"OPENAI_API_KEY": "fake-for-tests", "UV_LOCKED": "1"}
216+
for job in (checks, build, jobs["publish"]):
217+
assert "if" not in job and "continue-on-error" not in job
218+
for step in job["steps"]:
219+
assert "if" not in step and "continue-on-error" not in step
220+
221+
bin_dir = tmp_path / "bin"
222+
bin_dir.mkdir()
223+
make = bin_dir / "make"
224+
make.write_text(
225+
"#!/bin/sh\n"
226+
'check="${UV_PYTHON:-}:$*"\n'
227+
'printf "%s\\n" "$check" >> "$CHECK_LOG"\n'
228+
'[ "$check" != "$FAILED_CHECK" ]\n',
229+
encoding="utf-8",
230+
)
231+
make.chmod(0o755)
232+
log = tmp_path / "checks.log"
233+
result = subprocess.run(
234+
[bash, "--noprofile", "--norc", "-e", "-o", "pipefail", "-c", check["run"]],
235+
cwd=tmp_path,
236+
env={
237+
"PATH": f"{bin_dir}{os.pathsep}{os.defpath}",
238+
"CHECK_LOG": str(log),
239+
"FAILED_CHECK": failed_check or "",
240+
**check["env"],
241+
},
242+
capture_output=True,
243+
text=True,
244+
timeout=5,
245+
)
246+
expected = [f"3.{minor}:sync tests" for minor in range(10, 15)] + [":typecheck"]
247+
if failed_check is None:
248+
assert result.returncode == 0, result.stderr
249+
assert log.read_text(encoding="utf-8").splitlines() == expected
250+
else:
251+
assert result.returncode != 0
252+
assert (
253+
log.read_text(encoding="utf-8").splitlines()
254+
== expected[: expected.index(failed_check) + 1]
255+
)
256+
257+
258+
def test_release_checks_reject_stale_lockfile(tmp_path: Path) -> None:
259+
uv = shutil.which("uv")
260+
if uv is None:
261+
pytest.skip("The publish workflow requires uv.")
262+
workflow = yaml.safe_load(PUBLISH_WORKFLOW.read_text(encoding="utf-8"))
263+
check = next(
264+
step
265+
for step in workflow["jobs"]["checks"]["steps"]
266+
if step["name"] == "Check release source"
267+
)
268+
dependency = tmp_path / "dependency"
269+
dependency.mkdir()
270+
(dependency / "pyproject.toml").write_text(
271+
'[project]\nname = "fixture-dependency"\nversion = "0.1.0"\n', encoding="utf-8"
272+
)
273+
project = (
274+
'[project]\nname = "release-fixture"\nversion = "0.1.0"\n'
275+
'requires-python = ">=3.10"\ndependencies = []\n'
276+
'[tool.uv.sources]\nfixture-dependency = { path = "dependency" }\n'
277+
)
278+
pyproject = tmp_path / "pyproject.toml"
279+
pyproject.write_text(project, encoding="utf-8")
280+
env = {
281+
"PATH": os.defpath,
282+
"UV_PYTHON": sys.executable,
283+
"UV_CACHE_DIR": str(tmp_path / "cache"),
284+
}
285+
if "SYSTEMROOT" in os.environ:
286+
env["SYSTEMROOT"] = os.environ["SYSTEMROOT"]
287+
command = [uv, "--offline", "--no-config"]
288+
subprocess.run(command + ["lock"], cwd=tmp_path, env=env, check=True, timeout=15)
289+
lockfile = tmp_path / "uv.lock"
290+
original_lock = lockfile.read_bytes()
291+
# Exercise synchronization without building or installing either fixture package.
292+
sync = command + ["sync", "--no-install-project", "--no-install-package", "fixture-dependency"]
293+
env.update(check["env"])
294+
subprocess.run(sync, cwd=tmp_path, env=env, check=True, timeout=15)
295+
pyproject.write_text(
296+
project.replace("dependencies = []", 'dependencies = ["fixture-dependency"]'),
297+
encoding="utf-8",
298+
)
299+
result = subprocess.run(sync, cwd=tmp_path, env=env, capture_output=True, text=True, timeout=15)
300+
assert result.returncode != 0
301+
assert "lockfile" in result.stderr and "needs to be updated" in result.stderr
302+
assert lockfile.read_bytes() == original_lock
303+
304+
305+
def test_release_build_is_isolated_from_test_execution() -> None:
306+
workflow = yaml.safe_load(PUBLISH_WORKFLOW.read_text(encoding="utf-8"))
307+
checks = workflow["jobs"]["checks"]
308+
build = workflow["jobs"]["build"]
309+
# Separate GitHub-hosted jobs provide fresh runners, not just new directories.
310+
assert checks["runs-on"] == build["runs-on"] == "ubuntu-latest"
311+
assert checks["permissions"] == build["permissions"] == {"contents": "read"}
312+
assert "outputs" not in checks
313+
assert build["needs"] == "checks"
314+
for job in (checks, build):
315+
assert "env" not in job and "container" not in job
316+
for step in job["steps"]:
317+
action = step.get("uses", "")
318+
assert not action.startswith(("actions/cache@", "actions/download-artifact@"))
319+
if action.startswith("astral-sh/setup-uv@"):
320+
assert step["with"]["enable-cache"] is False
321+
if job is checks:
322+
assert not action.startswith("actions/upload-artifact@")
323+
build_commands = [step["run"] for step in build["steps"] if "run" in step]
324+
assert len(build_commands) == 2 # Provenance validation, then packaging; no test execution.
325+
assert build_commands[-1] == "uv build"
326+
327+
188328
def test_pypi_job_only_publishes_the_build_artifact() -> None:
189329
workflow = PUBLISH_WORKFLOW.read_text(encoding="utf-8")
190330
publish = _workflow_job("publish", PUBLISH_WORKFLOW)

0 commit comments

Comments
 (0)