@@ -9,7 +9,7 @@ popular choice is having a workflow that's triggered by a
9
9
This guide shows you how to publish a Python distribution
10
10
whenever a tagged commit is pushed.
11
11
It will use the `pypa/gh-action-pypi-publish GitHub Action `_ for
12
- publishing. It also uses GitHub's `upload-artifact `_ and `download-artifact `_ actions
12
+ publishing. It also uses GitHub's `upload-artifact `_ and `download-artifact `_ actions
13
13
for temporarily storing and downloading the source packages.
14
14
15
15
.. attention ::
@@ -23,15 +23,15 @@ Configuring trusted publishing
23
23
==============================
24
24
25
25
This guide relies on PyPI's `trusted publishing `_ implementation to connect
26
- to `GitHub Actions CI/CD `_. This is recommended for security reasons, since
26
+ to `GitHub Actions CI/CD `_. This is recommended for security reasons, since
27
27
the generated tokens are created for each of your projects
28
28
individually and expire automatically. Otherwise, you'll need to generate an
29
29
`API token `_ for both PyPI and TestPyPI. In case of publishing to third-party
30
30
indexes like :doc: `devpi <devpi:index >`, you may need to provide a
31
31
username/password combination.
32
32
33
33
Since this guide will demonstrate uploading to both
34
- PyPI and TestPyPI, we'll need two trusted publishers configured.
34
+ PyPI and TestPyPI, we'll need two trusted publishers configured.
35
35
The following steps will lead you through creating the "pending" publishers
36
36
for your new :term: `PyPI project <Project> `.
37
37
However it is also possible to add `trusted publishing `_ to any
@@ -52,17 +52,17 @@ Let's begin! 🚀
52
52
2. Fill in the name you wish to publish your new
53
53
:term: `PyPI project <Project> ` under
54
54
(the ``name `` value in your ``setup.cfg `` or ``pyproject.toml ``),
55
- your GitHub username and repository name and
56
- the name of the release workflow file under
55
+ the GitHub repository owner's name (org or user)
56
+ and repository name and the name of the release workflow file under
57
57
the ``.github/ `` folder, see :ref: `workflow-definition `.
58
58
Finally add the name of the GitHub Actions environment
59
59
(``pypi ``) we're going set up under your repository.
60
60
Register the trusted publisher.
61
61
3. Now, go to https://test.pypi.org/manage/account/publishing/ and repeat
62
62
the second step, but now enter ``testpypi `` as the name of the
63
63
GitHub Actions environment.
64
- 4. Your "pending" publishers are now ready for their first use and will
65
- create your projects automatically once you use them
64
+ 4. Your "pending" publishers are now ready for their first use and will
65
+ create your projects automatically once you use them
66
66
for the first time.
67
67
68
68
.. note ::
@@ -95,23 +95,19 @@ should make GitHub run this workflow:
95
95
:language: yaml
96
96
:end-before: jobs:
97
97
98
- This will also ensure that the release workflow is only triggered
99
- if the current commit is tagged. It is recommended you use the
100
- latest release tag.
101
-
102
98
Checking out the project and building distributions
103
99
===================================================
104
100
105
- We will have to define two jobs to publish to PyPI
106
- and TestPyPI respectively, and an additional job to
101
+ We will have to define two jobs to publish to PyPI
102
+ and TestPyPI respectively, and an additional job to
107
103
build the distribution packages.
108
104
109
- First, we'll define the job for building the dist packages of
105
+ First, we'll define the job for building the dist packages of
110
106
your project and storing them for later use:
111
107
112
108
.. literalinclude :: github-actions-ci-cd-sample/publish-to-test-pypi.yml
113
109
:language: yaml
114
- :start-after : jobs:
110
+ :start-at : jobs:
115
111
:end-before: Install pypa/build
116
112
117
113
This will download your repository into the CI runner and then
@@ -123,7 +119,7 @@ So add this to the steps list:
123
119
124
120
.. literalinclude :: github-actions-ci-cd-sample/publish-to-test-pypi.yml
125
121
:language: yaml
126
- :start-after: version: "3.x"
122
+ :start-at: Install pypa/build
127
123
:end-before: publish-to-pypi
128
124
129
125
Defining a workflow job environment
@@ -135,14 +131,18 @@ In this guide, we'll use the latest stable Ubuntu LTS version
135
131
provided by GitHub Actions. This also defines a GitHub Environment
136
132
for the job to run in its context and a URL to be displayed in GitHub's
137
133
UI nicely. Additionally, it allows aqcuiring an OpenID Connect token
138
- which is mandatory that the ``pypi-publish `` actions needs to
139
- implement secretless trusted publishing to PyPI.
134
+ that the ``pypi-publish `` actions needs to implement secretless
135
+ trusted publishing to PyPI.
140
136
141
137
.. literalinclude :: github-actions-ci-cd-sample/publish-to-test-pypi.yml
142
138
:language: yaml
143
139
:start-after: path: dist/
144
140
:end-before: steps:
145
141
142
+ This will also ensure that the PyPI publishing workflow is only triggered
143
+ if the current commit is tagged. It is recommended you use the
144
+ latest release tag.
145
+
146
146
Publishing the distribution to PyPI
147
147
===================================
148
148
@@ -151,14 +151,24 @@ Finally, add the following steps at the end:
151
151
.. literalinclude :: github-actions-ci-cd-sample/publish-to-test-pypi.yml
152
152
:language: yaml
153
153
:start-after: id-token: write
154
- :end-before: publish-to-testpypi :
154
+ :end-before: github-release :
155
155
156
156
This step uses the `pypa/gh-action-pypi-publish `_ GitHub
157
- Action: after the stored distribution package has been
158
- downloaded by the `download-artifact `_ action, it uploads
157
+ Action: after the stored distribution package has been
158
+ downloaded by the `download-artifact `_ action, it uploads
159
159
the contents of the ``dist/ `` folder into PyPI unconditionally.
160
- This job also signs the artifacts with the `sigstore/gh-action-sigstore-python `_
161
- GitHub Action publishing them to PyPI.
160
+
161
+ Signing the distribution packages
162
+ =================================
163
+
164
+ This additional job signs the distribution packages with the
165
+ `sigstore/gh-action-sigstore-python GitHub Action `_ and then uploads
166
+ them to GitHub Release.
167
+
168
+ .. literalinclude :: github-actions-ci-cd-sample/publish-to-test-pypi.yml
169
+ :language: yaml
170
+ :start-at: github-release:
171
+ :end-before: publish-to-testpypi
162
172
163
173
Separate workflow for publishing to TestPyPI
164
174
============================================
@@ -169,8 +179,15 @@ section:
169
179
170
180
.. literalinclude :: github-actions-ci-cd-sample/publish-to-test-pypi.yml
171
181
:language: yaml
172
- :start-after: ./dist/*.whl
182
+ :start-at: publish-to-testpypi
183
+
184
+ The whole CD workflow
185
+ =====================
186
+
187
+ .. collapse :: Load file
173
188
189
+ .. literalinclude :: github-actions-ci-cd-sample/publish-to-test-pypi.yml
190
+ :language: yaml
174
191
175
192
That's all, folks!
176
193
==================
@@ -193,7 +210,7 @@ sure that your release pipeline remains healthy!
193
210
https://github.com/actions/download-artifact
194
211
.. _`upload-artifact` :
195
212
https://github.com/actions/upload-artifact
196
- .. _`sigstore/gh-action-sigstore-python` :
213
+ .. _`sigstore/gh-action-sigstore-python GitHub Action ` :
197
214
https://github.com/marketplace/actions/gh-action-sigstore-python
198
215
.. _Secrets :
199
216
https://docs.github.com/en/actions/reference/encrypted-secrets
0 commit comments