Skip to content

Commit f6d52cb

Browse files
authored
Create release-pypi.yml
1 parent 944f443 commit f6d52cb

1 file changed

Lines changed: 129 additions & 0 deletions

File tree

.github/workflows/release-pypi.yml

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# This workflow automates the build, testing, and publication of a Python distribution to PyPI and TestPyPI.
2+
3+
name: Publish Python 🐍 distribution 📦 to PyPI and TestPyPI
4+
5+
# Trigger the workflow on push events to the 'main' branch and tags starting with 'v'.
6+
on:
7+
push:
8+
tags:
9+
- v*
10+
branches:
11+
- main
12+
# Uncomment the below section to enable testing package builds on pull requests to 'main'.
13+
# pull_request:
14+
# branches:
15+
# - main
16+
17+
jobs:
18+
# Job to build the Python distribution.
19+
build:
20+
name: Build distribution 📦
21+
runs-on: ubuntu-latest
22+
outputs:
23+
package_name: ${{ steps.build_artifacts.outputs.package_name }}
24+
package_version: ${{ steps.build_artifacts.outputs.package_version }}
25+
steps:
26+
- uses: actions/checkout@v4
27+
with:
28+
persist-credentials: false
29+
fetch-tags: true
30+
fetch-depth: 0
31+
- name: Set up Python
32+
uses: actions/setup-python@v5
33+
with:
34+
python-version: "3.x"
35+
- name: Install pypa/build
36+
run: python3 -m pip install build hatchling hatch-vcs --user
37+
- name: Build a binary wheel and a source tarball
38+
id: build_artifacts
39+
run: |
40+
python3 -m build
41+
package_name=$(hatchling metadata name)
42+
package_version=$(hatchling version)
43+
echo "package_name=$package_name" >> $GITHUB_OUTPUT
44+
echo "package_version=$package_version" >> $GITHUB_OUTPUT
45+
- name: Store the distribution packages
46+
uses: actions/upload-artifact@v4
47+
with:
48+
name: python-package-distributions
49+
path: dist/
50+
51+
# Job to publish the distribution to PyPI on tag pushes.
52+
publish-to-pypi:
53+
name: Publish Python 🐍 distribution 📦 to PyPI
54+
if: startsWith(github.ref, 'refs/tags/')
55+
needs: [build]
56+
runs-on: ubuntu-latest
57+
environment:
58+
name: pypi
59+
url: https://pypi.org/p/${{ needs.build.outputs.package_name }}
60+
permissions:
61+
id-token: write # Required for trusted publishing
62+
steps:
63+
- name: Download all the dists
64+
uses: actions/download-artifact@v4
65+
with:
66+
name: python-package-distributions
67+
path: dist/
68+
- name: Publish distribution 📦 to PyPI
69+
uses: pypa/gh-action-pypi-publish@release/v1
70+
71+
# Job to sign the distribution with Sigstore and upload it to GitHub Release.
72+
github-release:
73+
name: Sign the Python 🐍 distribution 📦 with Sigstore and upload them to GitHub Release
74+
needs: [publish-to-pypi]
75+
runs-on: ubuntu-latest
76+
permissions:
77+
contents: write # Required for making GitHub Releases
78+
id-token: write # Required for Sigstore
79+
steps:
80+
- name: Download all the dists
81+
uses: actions/download-artifact@v4
82+
with:
83+
name: python-package-distributions
84+
path: dist/
85+
- name: Sign the dists with Sigstore
86+
uses: sigstore/gh-action-sigstore-python@v3.0.0
87+
with:
88+
inputs: |
89+
./dist/*.tar.gz
90+
./dist/*.whl
91+
- name: Delete the stub of Release
92+
env:
93+
GITHUB_TOKEN: ${{ github.token }}
94+
run: |
95+
gh release delete "$GITHUB_REF_NAME" --repo "$GITHUB_REPOSITORY" --cleanup-tag --yes
96+
- name: Create GitHub Release
97+
env:
98+
GITHUB_TOKEN: ${{ github.token }}
99+
run: |
100+
gh release create "$GITHUB_REF_NAME" --title "Release $GITHUB_REF_NAME" --generate-notes --latest --repo "$GITHUB_REPOSITORY"
101+
- name: Upload artifact signatures to GitHub Release
102+
env:
103+
GITHUB_TOKEN: ${{ github.token }}
104+
run: |
105+
gh release upload "$GITHUB_REF_NAME" dist/** --repo "$GITHUB_REPOSITORY"
106+
107+
# Job to publish the distribution to TestPyPI on pushes to the 'main' branch.
108+
publish-to-testpypi:
109+
name: Publish Python 🐍 distribution 📦 to TestPyPI
110+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
111+
needs: [build]
112+
runs-on: ubuntu-latest
113+
environment:
114+
name: testpypi
115+
url: https://test.pypi.org/p/${{ needs.build.outputs.package_name }}
116+
permissions:
117+
id-token: write # Required for trusted publishing
118+
steps:
119+
- name: Download all the dists
120+
uses: actions/download-artifact@v4
121+
with:
122+
name: python-package-distributions
123+
path: dist/
124+
- name: Publish distribution 📦 to TestPyPI
125+
uses: pypa/gh-action-pypi-publish@release/v1
126+
with:
127+
repository-url: https://test.pypi.org/legacy/
128+
skip-existing: true
129+
verbose: true

0 commit comments

Comments
 (0)