Debian Codegraph @

.github/workflows/publish-to-pypi.yml

raw
1 name: Publish Python 🐍 distribution 📦 to PyPI
2
3 on: 'push'
4
5 env:
6 DEFAULT_PYTHON: "3.12"
7
8 jobs:
9 build:
10 name: Build distribution 📦
11 runs-on: ubuntu-latest
12 permissions:
13 contents: read
14
15 steps:
16 - uses: actions/checkout@v6
17 - name: Set up Python
18 uses: actions/setup-python@v6
19 with:
20 python-version: ${{ env.DEFAULT_PYTHON }}
21 check-latest: true
22 - name: Install pypa/build
23 run: >-
24 python3 -m
25 pip install -r requirements.txt
26 build
27 --user
28 - name: Build a binary wheel and a source tarball
29 run: python3 -m build
30 - name: Store the distribution packages
31 uses: actions/upload-artifact@v7
32 with:
33 name: python-package-distributions
34 path: dist/
35
36 check_version:
37 name: Check the version
38 if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes
39 needs:
40 - build
41 permissions:
42 contents: read
43 runs-on: ubuntu-latest
44 steps:
45 - name: Checkout code
46 uses: actions/checkout@v6
47 with:
48 fetch-depth: 0
49
50 - name: Extract tag version
51 id: extract_tag
52 run: |
53 TAG_VERSION=${GITHUB_REF#refs/tags/}
54 echo "TAG_VERSION=$TAG_VERSION" >> $GITHUB_ENV
55
56 - name: Check tag in package
57 run: |
58 if ! grep -q "__version__ = \"$TAG_VERSION\"" ./cookidoo_api/__init__.py; then
59 echo "Error: Tag version $TAG_VERSION not found in the package."
60 exit 1
61 else
62 echo "Success: Tag version $TAG_VERSION found in package."
63 fi
64
65 - name: Check if version type matches branch type
66 run: |
67 branch=$(git branch -r --contains ${{ github.ref }} --format "%(refname:lstrip=3)" | grep -v '^HEAD$')
68 echo "Branch: $branch"
69 version=${GITHUB_REF#refs/tags/}
70 echo "Version from tag: $version"
71
72 # Check if the version is a prerelease
73 if [[ "$version" =~ [a-zA-Z] ]]; then
74 echo "Prerelease version detected: $version"
75 prerelease=true
76 else
77 echo "Final release version detected: $version"
78 prerelease=false
79 fi
80
81 # Fail if it's a prerelease and on the master branch
82 if [[ "$branch" == "master" && "$prerelease" == "true" ]]; then
83 echo "Error: Prerelease versions are not allowed on the master branch."
84 exit 1
85 fi
86
87 # Fail if it's a final release on a non-master branch
88 if [[ "$branch" != "master" && "$prerelease" == "false" ]]; then
89 echo "Error: Final releases are not allowed on non-master branches."
90 exit 1
91 fi
92
93 publish-to-pypi:
94 name: >-
95 Publish Python 🐍 distribution 📦 to PyPI
96 needs: check_version
97 runs-on: ubuntu-latest
98 environment:
99 name: pypi
100 url: https://pypi.org/p/cookidoo-api # Replace <package-name> with your PyPI project name
101 permissions:
102 id-token: write # IMPORTANT: mandatory for trusted publishing
103 steps:
104 - name: Download all the dists
105 uses: actions/download-artifact@v8
106 with:
107 name: python-package-distributions
108 path: dist/
109 - name: Publish distribution 📦 to PyPI
110 uses: pypa/gh-action-pypi-publish@release/v1
111
112 github-release:
113 name: >-
114 Sign the Python 🐍 distribution 📦 with Sigstore
115 and upload them to GitHub Release
116 needs:
117 - publish-to-pypi
118 runs-on: ubuntu-latest
119
120 permissions:
121 contents: write # IMPORTANT: mandatory for making GitHub Releases
122 id-token: write # IMPORTANT: mandatory for sigstore
123
124 steps:
125 - uses: actions/checkout@v6
126 with:
127 fetch-depth: 1
128 - name: Get version
129 id: version
130 run: |
131 TAG_VERSION=${GITHUB_REF#refs/tags/}
132 echo "version=$TAG_VERSION" >> $GITHUB_OUTPUT
133 - name: Check if version is prerelease
134 id: check_prerelease
135 run: |
136 TAG_VERSION="${{ steps.version.outputs.version }}"
137 if [[ "$TAG_VERSION" =~ [a-zA-Z] ]]; then
138 echo "prerelease=true" >> $GITHUB_OUTPUT
139 else
140 echo "prerelease=false" >> $GITHUB_OUTPUT
141 fi
142 - name: Download all the dists
143 uses: actions/download-artifact@v8
144 with:
145 name: python-package-distributions
146 path: dist/
147 - name: Sign the dists with Sigstore
148 uses: sigstore/[email protected]
149 with:
150 inputs: >-
151 ./dist/*.tar.gz
152 ./dist/*.whl
153 - name: Prerelease
154 uses: softprops/action-gh-release@v3
155 if: ${{ steps.check_prerelease.outputs.prerelease == 'true' }}
156 with:
157 tag_name: ${{ github.ref }}
158 name: ${{ steps.version.outputs.version }}
159 generate_release_notes: true
160 draft: true
161 prerelease: true
162 env:
163 GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
164 - name: Upload artifact signatures to GitHub Release
165 env:
166 GITHUB_TOKEN: ${{ github.token }}
167 # Upload to GitHub Release using the `gh` CLI.
168 # `dist/` contains the built packages, and the
169 # sigstore-produced signatures and certificates.
170 run: >-
171 gh release upload
172 '${{ steps.version.outputs.version }}' dist/**
173 --repo '${{ github.repository }}'