Skip to content

Commit 4c57867

Browse files
committed
Apply feedback from code review
1 parent 17993ef commit 4c57867

File tree

2 files changed

+80
-27
lines changed

2 files changed

+80
-27
lines changed

source/guides/github-actions-ci-cd-sample/publish-to-test-pypi.yml

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,45 @@ name: Publish Python 🐍 distributions 📦 to PyPI and TestPyPI
33
on: push
44

55
jobs:
6-
build-n-publish:
7-
name: Build and publish Python 🐍 distributions 📦 to PyPI and TestPyPI
6+
build-n-publish-pypi:
7+
name: Build and publish Python 🐍 distributions 📦 to PyPI
88
runs-on: ubuntu-latest
9+
environment:
10+
name: pypi
11+
url: https://pypi.org/p/<package-name>
12+
permissions:
13+
id-token: write
14+
15+
steps:
16+
- uses: actions/checkout@v3
17+
- name: Set up Python
18+
uses: actions/setup-python@v4
19+
with:
20+
python-version: "3.x"
21+
- name: Install pypa/build
22+
run: >-
23+
python3 -m
24+
pip install
25+
build
26+
--user
27+
- name: Build a binary wheel and a source tarball
28+
run: >-
29+
python3 -m
30+
build
31+
--sdist
32+
--wheel
33+
--outdir dist/
34+
.
35+
# Actually publish to PyPI
36+
- name: Publish distribution 📦 to PyPI
37+
if: startsWith(github.ref, 'refs/tags')
38+
uses: pypa/gh-action-pypi-publish@release/v1
39+
build-n-publish-testpypi:
40+
name: Build and publish Python 🐍 distributions 📦 to TestPyPI
41+
runs-on: ubuntu-latest
42+
environment:
43+
name: testpypi
44+
url: https://test.pypi.org/p/<package-name>
945
permissions:
1046
id-token: write
1147

@@ -29,11 +65,7 @@ jobs:
2965
--wheel
3066
--outdir dist/
3167
.
32-
# Actually publish to PyPI/TestPyPI
3368
- name: Publish distribution 📦 to Test PyPI
3469
uses: pypa/gh-action-pypi-publish@release/v1
3570
with:
3671
repository-url: https://test.pypi.org/legacy/
37-
- name: Publish distribution 📦 to PyPI
38-
if: startsWith(github.ref, 'refs/tags')
39-
uses: pypa/gh-action-pypi-publish@release/v1

source/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows.rst

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,23 @@ This guide relies on PyPI's `trusted publishing`_ implementation to connect
2424
to `GitHub Actions CI/CD`_. This is recommended for security reasons, since
2525
the generated tokens are created for each of your projects
2626
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.
2930

3031
Since this guide will demonstrate uploading to both
3132
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.
3336

3437
Let's begin! 🚀
3538

36-
1. Go to https://pypi.org/manage/account/publishing/
39+
1. Go to https://pypi.org/manage/account/publishing/.
3740
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`.
4044
Finally add the name of the GitHub Actions environment
4145
running under your repository.
4246
Register the trusted publisher.
@@ -74,29 +78,35 @@ should make GitHub run this workflow:
7478
Defining a workflow job environment
7579
===================================
7680

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.
7986
In this guide, we'll use the latest stable Ubuntu LTS version
8087
provided by GitHub Actions:
8188

8289
.. literalinclude:: github-actions-ci-cd-sample/publish-to-test-pypi.yml
8390
:language: yaml
8491
:start-after: on:
85-
:end-before: steps:
92+
:end-before: environment:
8693

8794

8895
Checking out the project and building distributions
8996
===================================================
9097

91-
Then, add the following under the ``build-n-publish`` section:
98+
Then, add the following under the ``build-n-publish-pypi`` section:
9299

93100
.. literalinclude:: github-actions-ci-cd-sample/publish-to-test-pypi.yml
94101
:language: yaml
95102
:start-after: runs-on:
96103
:end-before: Install pypa/build
97104

98105
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.
100110

101111
And now we can build dists from source. In this example, we'll
102112
use ``build`` package.
@@ -114,25 +124,36 @@ So add this to the steps list:
114124
.. literalinclude:: github-actions-ci-cd-sample/publish-to-test-pypi.yml
115125
:language: yaml
116126
:start-after: version: "3.x"
117-
:end-before: Actually publish to PyPI/TestPyPI
127+
:end-before: Actually publish to PyPI
118128

119129

120-
Publishing the distribution to PyPI and TestPyPI
121-
================================================
130+
Publishing the distribution to PyPI
131+
===================================
122132

123133
Finally, add the following steps at the end:
124134

125135
.. literalinclude:: github-actions-ci-cd-sample/publish-to-test-pypi.yml
126136
: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
134145
these updated regularly.
135146

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
136157

137158
That's all, folks!
138159
==================

0 commit comments

Comments
 (0)