Skip to content

Commit 84a98ee

Browse files
authored
🐛 FIX: Build releases with python -m build, on every PR (#164)
## What and why The release build is broken: the next `v*` tag would fail before anything reaches PyPI. The `publish` job runs `pip install flit~=3.4` and then `flit build`. That now resolves flit 3.12.0 with flit_core 4.2.0 (flit 3.12 requires `flit_core>=3.12.0` with no upper bound), and `flit build` crashes: ``` TypeError: SdistBuilder.build() got an unexpected keyword argument 'gen_setup_py' ``` This PR changes `.github/workflows/tests.yml`, keeping its file name, which the PyPI trusted publisher names: - **New `build` job, run on every push and PR.** It runs `python -m build`, which builds with the project's own `[build-system]` pin (`flit_core >=3.4,<4`), so the release build can no longer drift from it. Then `twine check --strict dist/*`. On a tag push it also checks that `dist/` holds exactly one wheel and one sdist, and that the tag matches their version. It uploads `dist/` as an artifact, and `allgood` now needs it, so a broken build shows up on PRs instead of at release time. - **`publish` publishes that artifact.** It downloads it and uploads it with `pypa/gh-action-pypi-publish` (trusted publishing; attestations are on by default). It needs `pre-commit`, `tests` and `build`, runs only for `v*` tag pushes, uses the `pypi` environment, and its only permission is `id-token: write`. (Its job-level `if` used to match any tag, but the trigger already limited tag runs to `v*`, so that does not change.) - **Read-only default permissions.** The workflow gets a top-level `permissions: contents: read`. ## How it was tested - **The failure on master, reproduced** (Python 3.10, as in the old job): `pip install "flit~=3.4"` installed flit 3.12.0 + flit_core 4.2.0, and `flit build` exited 1 with the `TypeError` above. - **The new build, run locally on Python 3.13** with the same commands as the job: - `python -m build` exited 0 and built `mdit_py_plugins-0.7.0.tar.gz` and `mdit_py_plugins-0.7.0-py3-none-any.whl`. - `twine check --strict dist/*` passed for both. - **The tag check**, run from the workflow text against those files: - `v0.7.0`: exit 0; - `v0.7.1` and `v0.7`: exit 1, with `tag v0.7.1 != built version(s) ['0.7.0']`; - a `dist/` with only the wheel, only the sdist, or nothing: exit 1, with `expected one wheel and one sdist, got 1 and 0` (and so on). - **`actionlint`** reports nothing new. It still reports one finding on master, which this PR does not touch: SC2102 on `pip install -e .[testing]` in `tests`. - **`pre-commit run --all-files`** passes. - **`pytest`**: 511 passed (Python 3.13). No package code changed. ### Distribution contents compared with PyPI 0.6.1 - **Wheel:** the same files, plus the new `mdit_py_plugins/section_ref/` (#144). `METADATA` is identical apart from `Version`, and `WHEEL` names the same generator, currently flit 3.12.0 (the release build gets whichever `flit_core` the `>=3.4,<4` range resolves to when the tag is pushed). - **sdist:** 71 files become 62. - Added: the two `section_ref` modules. - No longer shipped: `.coveragerc`, `.github/dependabot.yml`, `.github/workflows/copilot-setup-steps.yml`, `.github/workflows/tests.yml`, `.gitignore`, `.pre-commit-config.yaml`, `.readthedocs.yml`, `AGENTS.md`, `CHANGELOG.md`, `codecov.yml` and `tox.ini`. The flit CLI added these from version control; the PEP 517 backend does not. - Still shipped: the package, `LICENSE`, `README.md`, `pyproject.toml` and `PKG-INFO`. - `tests/` and `docs/` were already excluded by `[tool.flit.sdist]` and still are. - The conda-forge feedstock builds from the sdist, but the only file it reads is `LICENSE` (`license_file`). ## Notes for the reviewer - **CI has not run on these commits yet.** `push` triggers only on `master` and tags, so the `build` job first runs when this PR is opened. - **The `publish` job itself first runs on the `v0.7.0` tag.** That tag push is also the first upload through trusted publishing (0.6.1 was uploaded with the token). - **The new jobs use the current action majors** (`checkout@v7`, `setup-python@v7`, `upload-artifact@v7`, `download-artifact@v8`). The existing jobs still use `checkout@v4` / `setup-python@v5`; they are bumped separately. - **Not in this PR:** - creating a GitHub Release on a tag; - a required reviewer on the `pypi` environment; - the Codecov upload; - pinning `build` / `twine` and SHA-pinning the actions (the repo pins neither today); - shipping `tests/` and `tox.ini` in the sdist, which needs a `pyproject.toml` change.
1 parent 519953d commit 84a98ee

1 file changed

Lines changed: 51 additions & 8 deletions

File tree

‎.github/workflows/tests.yml‎

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ on:
1010
- 'v*'
1111
pull_request:
1212

13+
permissions:
14+
contents: read
15+
1316
jobs:
1417

1518
pre-commit:
@@ -55,30 +58,70 @@ jobs:
5558
files: ./coverage.xml
5659
fail_ci_if_error: true
5760

61+
build:
62+
63+
name: Build distributions
64+
runs-on: ubuntu-latest
65+
66+
steps:
67+
- uses: actions/checkout@v7
68+
with:
69+
persist-credentials: false
70+
- uses: actions/setup-python@v7
71+
with:
72+
python-version: "3.13"
73+
# `python -m build` builds with the project's own [build-system] pin,
74+
# so the release build cannot drift from it
75+
# (`pip install flit~=3.4` resolves flit 3.12 + flit_core 4.2, and `flit build` crashes)
76+
- run: python -m pip install build twine
77+
- run: python -m build
78+
- run: python -m twine check --strict dist/*
79+
- name: Check the tag matches the built version
80+
if: startsWith(github.ref, 'refs/tags/v')
81+
run: |
82+
python - <<'EOF'
83+
import os, pathlib, sys
84+
tag = os.environ["GITHUB_REF_NAME"].removeprefix("v")
85+
wheels = list(pathlib.Path("dist").glob("*.whl"))
86+
sdists = list(pathlib.Path("dist").glob("*.tar.gz"))
87+
if len(wheels) != 1 or len(sdists) != 1:
88+
sys.exit(f"expected one wheel and one sdist, got {len(wheels)} and {len(sdists)}")
89+
versions = {p.name.removesuffix(".tar.gz").split("-")[1] for p in wheels + sdists}
90+
if versions != {tag}:
91+
sys.exit(f"tag v{tag} != built version(s) {sorted(versions)}")
92+
print(f"tag v{tag} matches the built version")
93+
EOF
94+
- uses: actions/upload-artifact@v7
95+
with:
96+
name: dist
97+
path: dist/
98+
retention-days: 7
99+
58100
allgood:
59101
runs-on: ubuntu-latest
60102
needs:
61103
- pre-commit
62104
- tests
105+
- build
63106
steps:
64107
- run: echo "Great success!"
65108

66109
publish:
67110

68111
name: Publish to PyPI
69-
needs: [pre-commit, tests]
70-
if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags')
112+
needs: [pre-commit, tests, build]
113+
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
71114
runs-on: ubuntu-latest
72115
permissions:
73-
id-token: write
116+
id-token: write # the only permission this job has (trusted publishing)
74117
environment:
75118
name: pypi
76119
url: https://pypi.org/p/mdit-py-plugins
77120
steps:
78-
- uses: actions/checkout@v4
79-
- uses: actions/setup-python@v5
121+
# publish exactly the distributions the `build` job built and checked
122+
- uses: actions/download-artifact@v8
80123
with:
81-
python-version: "3.10"
82-
- run: pip install flit~=3.4
83-
- run: flit build
124+
name: dist
125+
path: dist/
126+
# attestations are generated and uploaded by default
84127
- uses: pypa/gh-action-pypi-publish@release/v1

0 commit comments

Comments
 (0)