From 1037ed3db1ef8f95bc27cd8193dfcc6a690daf7a Mon Sep 17 00:00:00 2001 From: Jarrett Lusso Date: Thu, 5 Nov 2026 14:59:23 -0500 Subject: [PATCH] Automate PyPI releases from version tags Releases were published by hand, so a step like bumping VERSION or creating the GitHub release could be missed. Pushing a tag made by `make release` now publishes to PyPI through trusted publishing. --- .github/workflows/release.yml | 51 +++++++++++++++++++++++++++++++++++ Makefile | 18 +++++++++++++ README.md | 7 +++++ 3 files changed, 76 insertions(+) create mode 100644 .github/workflows/release.yml create mode 100644 Makefile diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..ca44e00 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,51 @@ +name: Release + +on: + push: + tags: + - "[0-9]+.[0-9]+.[0-9]+" + +permissions: + contents: write + id-token: write + +jobs: + release: + name: Release + runs-on: ubuntu-latest + environment: release + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Check tag matches VERSION + run: | + version="$(cat VERSION)" + if [ "$GITHUB_REF_NAME" != "$version" ]; then + echo "::error::Tag $GITHUB_REF_NAME does not match VERSION $version. Run 'make release VERSION=x.y.z'." + exit 1 + fi + + - name: Set up Python + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + with: + python-version: "3.13" + + - name: Run tests + run: | + pip install -r requirements.txt + pytest + + - name: Build + run: | + pip install build + python -m build + + - name: Publish to PyPI + uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3 + with: + skip-existing: true + + - name: Create GitHub release + run: gh release create "$GITHUB_REF_NAME" --verify-tag --generate-notes + env: + GH_TOKEN: ${{ github.token }} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..910f81a --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +.PHONY: release + +release: +ifndef VERSION + $(error VERSION is required, e.g. make release VERSION=1.2.3) +endif + @echo "$(VERSION)" | grep -Eq '^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$$' || { echo "error: VERSION must be x.y.z"; exit 1; } + @test -z "$$(git status --porcelain)" || { echo "error: working tree not clean"; exit 1; } + @test "$$(git branch --show-current)" = master || { echo "error: release from master"; exit 1; } + @git fetch -q --tags origin master + @! git rev-parse -q --verify "refs/tags/$(VERSION)" >/dev/null || { echo "error: tag $(VERSION) already exists"; exit 1; } + @cur="$$(cat VERSION)"; [ "$$cur" != "$(VERSION)" ] && [ "$$(printf '%s\n' "$$cur" "$(VERSION)" | sort -V | tail -1)" = "$(VERSION)" ] || { echo "error: VERSION must be greater than $$cur"; exit 1; } + @test "$$(git rev-parse HEAD)" = "$$(git rev-parse origin/master)" || { echo "error: master is not in sync with origin"; exit 1; } + pytest + @echo "$(VERSION)" > VERSION + @git commit -q -am "Release $(VERSION)" + @git tag -a $(VERSION) -m $(VERSION) + @echo "Tagged $(VERSION). Push the commit and tag to release: git push --follow-tags" diff --git a/README.md b/README.md index 1a9a037..bfda25c 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,13 @@ Tests can be run with the following command: pytest ``` +## Cutting a Release + +Run `make release VERSION=x.y.z` on an up to date `master` with a clean tree, +then `git push --follow-tags`. It runs the tests, bumps `VERSION`, commits, +and tags `x.y.z`. Pushing the tag triggers the release workflow, which +publishes to PyPI and creates the GitHub release. + ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/emailable/emailable-python.