Skip to content

Commit d7b087c

Browse files
committed
Add initial tutorial for trusted publishing.
This should solve #253. Signed-off-by: Mihai Maruseac <[email protected]>
1 parent 76b9482 commit d7b087c

File tree

3 files changed

+390
-5
lines changed

3 files changed

+390
-5
lines changed

tutorials/intro.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ What is a Python package? <self>
4545
Create a Python package <create-python-package>
4646
Publish to PyPI <publish-pypi>
4747
Publish to conda-forge <publish-conda-forge>
48+
Publish using GitHub Actions and Trusted Publishing <trusted-publishing>
4849
:::
4950

5051
:::{toctree}

tutorials/publish-pypi.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Publish your Python package to PyPI
77

88
:::{todo}
9-
- Make sure they add /dist to their .gitignore file. We have not discussed GitHub workflows anywhere yet. Where does that fit?
9+
- Make sure they add /dist to their .gitignore file. Where does that fit?
1010

1111
:::
1212

@@ -62,9 +62,7 @@ to TestPyPI. You need to:
6262
1. **Create an account on TestPyPI (or PyPI)**: You will need to create a TestPyPI account and associated token which provides permissions for you to upload your package. When you later publish your package to PyPI, you will need a separate PyPI account and token.
6363
1. **Publish to TestPyPI using `hatch publish`**
6464

65-
In a future lesson, you will learn how to create an automated
66-
GitHub Actions workflow that publishes an updated
67-
version of your package to PyPI every time you create a GitHub release.
65+
In a [future lesson](trusted-publishing), you will learn how to create an automated GitHub Actions workflow that publishes an updated version of your package to PyPI every time you create a GitHub release.
6866

6967
:::{admonition} Learn more about building Python packages in our guide
7068
:class: tip
@@ -368,8 +366,19 @@ related to PyPI publication.
368366
1. You can create a package-specific token which you will use to publish your package (manually) to PyPI. This is a great option if you don't wish to automate your PyPI publication workflow.
369367
2. You can also create an automated publication workflow on GitHub using GitHub Actions. This is a great way to make the publication process easier and it also supports a growing maintainer team. In this case we suggest you don't worry about the token and instead setup a specific GitHub Actions that publishes your package when you make a release. You can then create a "trusted publisher" workflow on PyPI.
370368

371-
You will learn how to create the automated trusted publisher workflow in a followup lesson.
369+
:::{admonition} Trusted Publishing
370+
:class: tip
371+
372+
While publishing from GitHub Action is possible using tokens, we recommend the _Trusted Publishing_ approach as it also confers significant security and usability benefits.
373+
374+
On the usability front, when Trusted Publishing is enabled, users no longer need to manually create API tokens on PyPI and store them in the GitHub release workflow.
375+
376+
On the security front, Trusted Publishing reduces a risk related to the API token being long lived: with API tokens, as soon as an attacker gets access to it, they can publish many packages and versions in your name (dependending on the scope of the token), until you discover the token compromise and rotate the credential. Trusted Publishing avoids this problem by minting very short lived tokens which expire automatically.
377+
378+
For these benefits, it is recommended that users use _only_ the GitHub Actions release workflow to publish packages.
379+
:::
372380

381+
You will learn how to create the automated trusted publisher workflow in a followup lesson.
373382

374383
### OPTIONAL: If you want to use a manual token-based publication workflow
375384

@@ -386,6 +395,15 @@ To do this:
386395

387396
And you're all done!
388397
398+
:::{admonition} Trusted Publishing instead of token-based publication
399+
:class: tip
400+
401+
Trusted Publishing will generate short lived tokens, scoped to the project, on
402+
demand, only when a specific release workflows gets triggered. This solves all
403+
the security and usability issues associated with storing credentials in
404+
files/GitHub secrets.
405+
:::
406+
389407
## You have published your package to TestPyPI!
390408
391409
Congratulations. You have now successfully published your package to TestPyPI. If you have a package that is ready for real-world use on the real PyPI, then you can follow the same steps (with the differences noted above) to publish it on PyPI.

0 commit comments

Comments
 (0)