Skip to content

Commit 51d9421

Browse files
committed
CI: add automatic publication to pypi.org and test.pypi.org
We want to reduce the potential for human error in our publication process and also streamline the process for everyone with the permission to create tags in the repository. The CI job runs for new commits pushed to the master branch and newly pushed tags, as long as the PUBLISH_PYPI GitHub Action variable is set to "true". This is to prevent CI runs on forked repository from failing because they are not allowed to publish on pypi.org and test.pypi.org. A fork that wants to use the publish logic just has to set the PUBLISH_PYPI variable for their repository. The job does not check out the git repository (hence why it does not use the existing publication logic in the Makefile) and instead downloads the artifacts generated by the build job. All builds are uploaded to test.pypi.org (so they can be tested via pip install) and tagged releases are uploaded to pypi.org as well. Also remove the upload helpers from the Makefile to make it clear that they are replaced by the automated process. Signed-off-by: Leonard Göhrs <[email protected]>
1 parent d9660fd commit 51d9421

File tree

3 files changed

+66
-45
lines changed

3 files changed

+66
-45
lines changed

.github/workflows/check-and-build.yaml

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: Check and Publish
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
codespell:
7+
name: Codespell
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v4
11+
- run: make qa-codespell
12+
13+
pytest:
14+
name: Python Test
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
- run: make qa-pytest
19+
20+
ruff:
21+
name: Python Format and Lint
22+
runs-on: ubuntu-latest
23+
steps:
24+
- uses: actions/checkout@v4
25+
- run: make qa-ruff
26+
27+
build:
28+
name: Python Build
29+
runs-on: ubuntu-latest
30+
steps:
31+
- uses: actions/checkout@v4
32+
with:
33+
# include tags and full history for setuptools_scm
34+
fetch-depth: 0
35+
- run: make build
36+
- uses: actions/upload-artifact@v4
37+
with:
38+
name: dist
39+
path: dist
40+
41+
publish:
42+
name: Publish
43+
if: ${{ github.event_name == 'push' && vars.PUBLISH_PYPI == 'true' && (startsWith(github.ref, 'refs/tags') || github.ref == 'refs/heads/master') }}
44+
runs-on: ubuntu-latest
45+
needs:
46+
- codespell
47+
- pytest
48+
- ruff
49+
- build
50+
permissions:
51+
id-token: write
52+
steps:
53+
- name: Download artifacts from build stage
54+
uses: actions/download-artifact@v4
55+
with:
56+
name: dist
57+
path: dist/
58+
- name: Publish distribution package to TestPyPI
59+
uses: pypa/gh-action-pypi-publish@release/v1
60+
with:
61+
repository-url: https://test.pypi.org/legacy/
62+
- name: Publish distribution package to PyPI
63+
if: ${{ startsWith(github.ref, 'refs/tags') }}
64+
uses: pypa/gh-action-pypi-publish@release/v1

Makefile

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ $(PYTHON_PACKAGING_VENV)/.created:
1212
$(PYTHON) -m venv $(PYTHON_PACKAGING_VENV) && \
1313
. $(PYTHON_PACKAGING_VENV)/bin/activate && \
1414
$(PYTHON) -m pip install --upgrade pip && \
15-
$(PYTHON) -m pip install build twine
15+
$(PYTHON) -m pip install build
1616
date > $(PYTHON_PACKAGING_VENV)/.created
1717

18-
.PHONY: packaging-env build _release
18+
.PHONY: packaging-env build
1919

2020
packaging-env: $(PYTHON_PACKAGING_VENV)/.created
2121

@@ -24,10 +24,6 @@ build: packaging-env
2424
rm -rf dist *.egg-info && \
2525
$(PYTHON) -m build
2626

27-
_release: build
28-
. $(PYTHON_PACKAGING_VENV)/bin/activate && \
29-
$(PYTHON) -m twine upload dist/*
30-
3127
# helper ######################################################################
3228
.PHONY: clean envs
3329

0 commit comments

Comments
 (0)