Skip to content

Commit 1f4a242

Browse files
committed
Apply further suggestions and improvements
Add `sphinx-toolbox` dependency.
1 parent 4580825 commit 1f4a242

File tree

4 files changed

+79
-42
lines changed

4 files changed

+79
-42
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ sphinx-inline-tabs==2021.4.11b9
44
python-docs-theme==2022.1
55
sphinx-copybutton==0.5.0
66
pypa-docs-theme @ git+https://github.com/pypa/pypa-docs-theme.git
7+
sphinx-toolbox==3.5.0

source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
'sphinx.ext.todo',
3737
'sphinx_inline_tabs',
3838
'sphinx_copybutton',
39+
'sphinx_toolbox.collapse',
3940
]
4041

4142
# config for copy button

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

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
name: Publish Python 🐍 distributions 📦 to PyPI and TestPyPI
1+
name: Publish Python 🐍 distribution 📦 to PyPI and TestPyPI
22

33
on: push
44

55
jobs:
66
build:
7-
name: Build distribution packages
7+
name: Build distribution 📦
88
runs-on: ubuntu-latest
99

1010
steps:
@@ -30,34 +30,52 @@ jobs:
3030
publish-to-pypi:
3131
name: >-
3232
Publish Python 🐍 distribution 📦 to PyPI
33-
and sign them with Sigstore
33+
if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes
3434
needs:
3535
- build
3636
runs-on: ubuntu-latest
3737
environment:
3838
name: pypi
39+
# Fill in your project (e.g. repository) name
40+
# for <package-name>
3941
url: https://pypi.org/p/<package-name>
4042
permissions:
4143
id-token: write
4244

4345
steps:
4446
- name: Download all the dists
45-
uses: actions/download-artifact@v3
46-
with:
47-
name: python-package-distributions
48-
path: dist/
49-
- name: Publish distribution 📦 to PyPI
50-
uses: pypa/gh-action-pypi-publish@release/v1
51-
- name: Sign the dists with Sigstore
52-
uses: sigstore/[email protected]
53-
with:
54-
inputs: >-
55-
./dist/*.tar.gz
56-
./dist/*.whl
47+
uses: actions/download-artifact@v3
48+
with:
49+
name: python-package-distributions
50+
path: dist/
51+
- name: Publish distribution 📦 to PyPI
52+
uses: pypa/gh-action-pypi-publish@release/v1
53+
54+
github-release:
55+
name: >-
56+
Sign the Python 🐍 distribution 📦 with Sigstore
57+
and upload them to GitHub Release
58+
needs:
59+
- publish-to-pypi
60+
steps:
61+
- name: Sign the dists with Sigstore
62+
uses: sigstore/[email protected]
63+
with:
64+
inputs: >-
65+
./dist/*.tar.gz
66+
./dist/*.whl
67+
- name: Upload artifact signatures to GitHub Release
68+
# Confusingly, this action also supports updating releases, not
69+
# just creating them. This is what we want here, since we've manually
70+
# created the release above.
71+
uses: softprops/action-gh-release@v1
72+
with:
73+
# dist/ contains the built packages, which smoketest-artifacts/
74+
# contains the signatures and certificates.
75+
files: dist/**
5776

5877
publish-to-testpypi:
59-
name: Build and publish Python 🐍 distributions 📦 to TestPyPI
60-
if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes
78+
name: Publish Python 🐍 distribution 📦 to TestPyPI
6179
needs:
6280
- build
6381
runs-on: ubuntu-latest

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

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ popular choice is having a workflow that's triggered by a
99
This guide shows you how to publish a Python distribution
1010
whenever a tagged commit is pushed.
1111
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
1313
for temporarily storing and downloading the source packages.
1414

1515
.. attention::
@@ -23,15 +23,15 @@ Configuring trusted publishing
2323
==============================
2424

2525
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
2727
the generated tokens are created for each of your projects
2828
individually and expire automatically. Otherwise, you'll need to generate an
2929
`API token`_ for both PyPI and TestPyPI. In case of publishing to third-party
3030
indexes like :doc:`devpi <devpi:index>`, you may need to provide a
3131
username/password combination.
3232

3333
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.
3535
The following steps will lead you through creating the "pending" publishers
3636
for your new :term:`PyPI project <Project>`.
3737
However it is also possible to add `trusted publishing`_ to any
@@ -52,17 +52,17 @@ Let's begin! 🚀
5252
2. Fill in the name you wish to publish your new
5353
:term:`PyPI project <Project>` under
5454
(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
5757
the ``.github/`` folder, see :ref:`workflow-definition`.
5858
Finally add the name of the GitHub Actions environment
5959
(``pypi``) we're going set up under your repository.
6060
Register the trusted publisher.
6161
3. Now, go to https://test.pypi.org/manage/account/publishing/ and repeat
6262
the second step, but now enter ``testpypi`` as the name of the
6363
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
6666
for the first time.
6767

6868
.. note::
@@ -95,23 +95,19 @@ should make GitHub run this workflow:
9595
:language: yaml
9696
:end-before: jobs:
9797

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-
10298
Checking out the project and building distributions
10399
===================================================
104100

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
107103
build the distribution packages.
108104

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
110106
your project and storing them for later use:
111107

112108
.. literalinclude:: github-actions-ci-cd-sample/publish-to-test-pypi.yml
113109
:language: yaml
114-
:start-after: jobs:
110+
:start-at: jobs:
115111
:end-before: Install pypa/build
116112

117113
This will download your repository into the CI runner and then
@@ -123,7 +119,7 @@ So add this to the steps list:
123119

124120
.. literalinclude:: github-actions-ci-cd-sample/publish-to-test-pypi.yml
125121
:language: yaml
126-
:start-after: version: "3.x"
122+
:start-at: Install pypa/build
127123
:end-before: publish-to-pypi
128124

129125
Defining a workflow job environment
@@ -135,14 +131,18 @@ In this guide, we'll use the latest stable Ubuntu LTS version
135131
provided by GitHub Actions. This also defines a GitHub Environment
136132
for the job to run in its context and a URL to be displayed in GitHub's
137133
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.
140136

141137
.. literalinclude:: github-actions-ci-cd-sample/publish-to-test-pypi.yml
142138
:language: yaml
143139
:start-after: path: dist/
144140
:end-before: steps:
145141

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+
146146
Publishing the distribution to PyPI
147147
===================================
148148

@@ -151,14 +151,24 @@ Finally, add the following steps at the end:
151151
.. literalinclude:: github-actions-ci-cd-sample/publish-to-test-pypi.yml
152152
:language: yaml
153153
:start-after: id-token: write
154-
:end-before: publish-to-testpypi:
154+
:end-before: github-release:
155155

156156
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
159159
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
162172

163173
Separate workflow for publishing to TestPyPI
164174
============================================
@@ -169,8 +179,15 @@ section:
169179

170180
.. literalinclude:: github-actions-ci-cd-sample/publish-to-test-pypi.yml
171181
: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
173188

189+
.. literalinclude:: github-actions-ci-cd-sample/publish-to-test-pypi.yml
190+
:language: yaml
174191

175192
That's all, folks!
176193
==================
@@ -193,7 +210,7 @@ sure that your release pipeline remains healthy!
193210
https://github.com/actions/download-artifact
194211
.. _`upload-artifact`:
195212
https://github.com/actions/upload-artifact
196-
.. _`sigstore/gh-action-sigstore-python`:
213+
.. _`sigstore/gh-action-sigstore-python GitHub Action`:
197214
https://github.com/marketplace/actions/gh-action-sigstore-python
198215
.. _Secrets:
199216
https://docs.github.com/en/actions/reference/encrypted-secrets

0 commit comments

Comments
 (0)