@@ -24,19 +24,23 @@ This guide relies on PyPI's `trusted publishing`_ implementation to connect
24
24
to `GitHub Actions CI/CD `_. This is recommended for security reasons, since
25
25
the generated tokens are created for each of your projects
26
26
individually and expire automatically. Otherwise you'll need to generate an
27
- `API token `_ or provide a username/password combination for both PyPI and
28
- TestPyPI.
27
+ `API token `_ for both PyPI and TestPyPI. In case of publishing to third-party
28
+ indexes like :doc: `devpi <devpi:index >`, you will need to provide a
29
+ username/password combination.
29
30
30
31
Since this guide will demonstrate uploading to both
31
32
PyPI and TestPyPI, we'll need two trusted publishers configured.
32
- The following steps will lead you through creating the "pending" publishers.
33
+ The following steps will lead you through creating the "pending" publishers
34
+ for your new project. However it is also possible to add `trusted publishing `_
35
+ to any pre-existing project, if you are its owner.
33
36
34
37
Let's begin! 🚀
35
38
36
- 1. Go to https://pypi.org/manage/account/publishing/
39
+ 1. Go to https://pypi.org/manage/account/publishing/.
37
40
2. Fill in the name you wish to publish your new project under,
38
- your repository data and the name of the release workflow file
39
- under the ``.github/ `` folder, see :ref: `workflow-definition `.
41
+ your GitHub username and repository name and
42
+ the name of the release workflow file under
43
+ the ``.github/ `` folder, see :ref: `workflow-definition `.
40
44
Finally add the name of the GitHub Actions environment
41
45
running under your repository.
42
46
Register the trusted publisher.
@@ -74,29 +78,35 @@ should make GitHub run this workflow:
74
78
Defining a workflow job environment
75
79
===================================
76
80
77
- Now, let's add initial setup for our job. It's a process that
78
- will execute commands that we'll define later.
81
+ We will have to define two jobs to publish to PyPI
82
+ and TestPyPI respectively.
83
+
84
+ Now, let's add initial setup for our job that will publish to PyPI.
85
+ It's a process that will execute commands that we'll define later.
79
86
In this guide, we'll use the latest stable Ubuntu LTS version
80
87
provided by GitHub Actions:
81
88
82
89
.. literalinclude :: github-actions-ci-cd-sample/publish-to-test-pypi.yml
83
90
:language: yaml
84
91
:start-after: on:
85
- :end-before: steps :
92
+ :end-before: environment :
86
93
87
94
88
95
Checking out the project and building distributions
89
96
===================================================
90
97
91
- Then, add the following under the ``build-n-publish `` section:
98
+ Then, add the following under the ``build-n-publish-pypi `` section:
92
99
93
100
.. literalinclude :: github-actions-ci-cd-sample/publish-to-test-pypi.yml
94
101
:language: yaml
95
102
:start-after: runs-on:
96
103
:end-before: Install pypa/build
97
104
98
105
This will download your repository into the CI runner and then
99
- install and activate the newest available Python 3 release.
106
+ install and activate the newest available Python 3 release. It
107
+ also defines the package index to publish to, PyPI, and grants
108
+ a permission to the action that is mandatory for trusted
109
+ publishing.
100
110
101
111
And now we can build dists from source. In this example, we'll
102
112
use ``build `` package.
@@ -114,25 +124,36 @@ So add this to the steps list:
114
124
.. literalinclude :: github-actions-ci-cd-sample/publish-to-test-pypi.yml
115
125
:language: yaml
116
126
:start-after: version: "3.x"
117
- :end-before: Actually publish to PyPI/TestPyPI
127
+ :end-before: Actually publish to PyPI
118
128
119
129
120
- Publishing the distribution to PyPI and TestPyPI
121
- ================================================
130
+ Publishing the distribution to PyPI
131
+ ===================================
122
132
123
133
Finally, add the following steps at the end:
124
134
125
135
.. literalinclude :: github-actions-ci-cd-sample/publish-to-test-pypi.yml
126
136
:language: yaml
127
- :start-after: Actually publish to PyPI/TestPyPI
128
-
129
- These two steps use the `pypa/gh-action-pypi-publish `_ GitHub
130
- Action: the first one uploads contents of the ``dist/ `` folder
131
- into TestPyPI unconditionally and the second does that to
132
- PyPI, but only if the current commit is tagged. It is recommended
133
- you use the latest release tag; a tool like GitHub's dependabot can keep
137
+ :start-after: Actually publish to PyPI
138
+ :end-before: build-n-publish-testpypi
139
+
140
+ This step uses the `pypa/gh-action-pypi-publish `_ GitHub
141
+ Action: It uploads the contents of the ``dist/ `` folder
142
+ into PyPI unconditionally, but only if the current commit
143
+ is tagged. It is recommended you use the latest release
144
+ tag; a tool like GitHub's dependabot can keep
134
145
these updated regularly.
135
146
147
+ Separate workflow for publishing to TestPyPI
148
+ ============================================
149
+
150
+ Now, repeat these steps and create another job for
151
+ publishing to the TestPyPI package index under the ``jobs ``
152
+ section:
153
+
154
+ .. literalinclude :: github-actions-ci-cd-sample/publish-to-test-pypi.yml
155
+ :language: yaml
156
+ :start-after: uses: pypa/gh-action-pypi-publish@release/v1
136
157
137
158
That's all, folks!
138
159
==================
0 commit comments