11name : Publish Python 🐍 distributions 📦 to PyPI
22
3+ # Publishing uses PyPI Trusted Publishing (OIDC) - no API token is stored in
4+ # this repository. The publisher configured on PyPI must match:
5+ # owner/repository : Infinitode/LocalSiteMap
6+ # workflow : build_and_publish.yml
7+ # environment : (left blank - therefore no `environment:` key below)
8+ #
9+ # Every upload is signed with PEP 740 attestations, which is what powers the
10+ # "provenance" indicator on the PyPI project page.
11+
312on :
413 push :
514 tags :
6- - ' *'
15+ - ' v[0-9]*'
16+ - ' [0-9]*'
17+ workflow_dispatch :
18+
19+ permissions :
20+ contents : read
721
822jobs :
9- build-n-publish :
10- name : Build and publish Python 🐍 distributions 📦 to PyPI
23+ build :
24+ name : Build distribution 📦
25+ runs-on : ubuntu-latest
26+ outputs :
27+ version : ${{ steps.version.outputs.version }}
28+ steps :
29+ - name : Check out the repository
30+ uses : actions/checkout@v4
31+
32+ - name : Set up Python 3.12
33+ uses : actions/setup-python@v5
34+ with :
35+ python-version : ' 3.12'
36+
37+ - name : Install build tooling
38+ run : >-
39+ python -m pip install --upgrade
40+ build
41+ twine
42+ setuptools
43+ wheel
44+
45+ - name : Run the test suite
46+ run : python -m unittest -v test_localsitemap
47+
48+ - name : Determine the release version
49+ id : version
50+ run : |
51+ set -euo pipefail
52+ if [[ "${GITHUB_REF}" == refs/tags/* ]]; then
53+ VERSION="${GITHUB_REF_NAME#v}"
54+ else
55+ VERSION="$(python -c 'from localsitemap._version import __version__; print(__version__)')"
56+ fi
57+ python - "${VERSION}" <<'PY'
58+ import pathlib
59+ import re
60+ import sys
61+
62+ version = sys.argv[1].strip()
63+ pattern = r"^[0-9]+(\.[0-9]+)*((a|b|rc)[0-9]+)?(\.post[0-9]+)?(\.dev[0-9]+)?$"
64+ if not re.match(pattern, version):
65+ sys.exit("Invalid release version: %r" % version)
66+
67+ target = pathlib.Path("localsitemap/_version.py")
68+ target.write_text(
69+ '"""Single source of truth for the LocalSiteMap version.\n\n'
70+ "The release workflow rewrites this file from the pushed git tag, so the\n"
71+ 'version published to PyPI always matches the tag.\n"""\n\n'
72+ '__version__ = "%s"\n' % version,
73+ encoding="utf-8",
74+ )
75+ print("Building version", version)
76+ PY
77+ echo "version=${VERSION}" >> "${GITHUB_OUTPUT}"
78+
79+ - name : Build sdist and wheel
80+ run : python -m build
81+
82+ - name : Check the distribution metadata
83+ run : python -m twine check --strict dist/*
84+
85+ - name : Upload the distribution artifacts
86+ uses : actions/upload-artifact@v4
87+ with :
88+ name : python-package-distributions
89+ path : dist/
90+ if-no-files-found : error
91+
92+ publish :
93+ name : Publish distribution 📦 to PyPI
94+ needs : build
95+ # Only real tag pushes publish. A manual "Run workflow" therefore builds,
96+ # tests and validates the distributions without touching PyPI, which also
97+ # makes it safe to run before Trusted Publishing is configured.
98+ if : startsWith(github.ref, 'refs/tags/')
99+ runs-on : ubuntu-latest
100+ # NOTE: intentionally no `environment:` key. The PyPI trusted publisher is
101+ # configured with a blank environment name, and adding one here would make
102+ # the OIDC token claim mismatch and the upload fail.
103+ permissions :
104+ id-token : write # required for Trusted Publishing and attestations
105+ steps :
106+ - name : Download the distribution artifacts
107+ uses : actions/download-artifact@v4
108+ with :
109+ name : python-package-distributions
110+ path : dist/
111+
112+ - name : Publish distribution 📦 to PyPI
113+ uses : pypa/gh-action-pypi-publish@release/v1
114+ with :
115+ attestations : true
116+ print-hash : true
117+
118+ verify-provenance :
119+ name : Verify published provenance 🛡️
120+ needs :
121+ - build
122+ - publish
123+ if : startsWith(github.ref, 'refs/tags/')
11124 runs-on : ubuntu-latest
125+ permissions :
126+ contents : read
12127 steps :
13- - uses : actions/checkout@master
14- - name : Set up Python 3.12
15- uses : actions/setup-python@v3
16- with :
17- python-version : ' 3.12'
18- - name : Install pypa/setuptools
19- run : >-
20- python -m
21- pip install setuptools wheel
22- - name : Extract tag name
23- id : tag
24- run : echo ::set-output name=TAG_NAME::$(echo $GITHUB_REF | cut -d / -f 3)
25- - name : Update version in setup.py
26- run : >-
27- sed -i "s/{{VERSION_PLACEHOLDER}}/${{ steps.tag.outputs.TAG_NAME }}/g" setup.py
28- - name : Build a binary wheel
29- run : >-
30- python setup.py sdist bdist_wheel
31- - name : Publish distribution 📦 to PyPI
32- uses : pypa/gh-action-pypi-publish@master
33- with :
34- password : ${{ secrets.PYPI_API_TOKEN }}
128+ - name : Check out the repository
129+ uses : actions/checkout@v4
130+
131+ - name : Set up Python 3.12
132+ uses : actions/setup-python@v5
133+ with :
134+ python-version : ' 3.12'
135+
136+ - name : Confirm every published file carries provenance
137+ env :
138+ VERSION : ${{ needs.build.outputs.version }}
139+ run : >-
140+ python .github/scripts/verify_provenance.py
141+ --project localsitemap
142+ --version "${VERSION}"
0 commit comments