Skip to content

Commit 7429c12

Browse files
committed
chore: fix errors from docs-test
1 parent c9abedd commit 7429c12

File tree

4 files changed

+58
-34
lines changed

4 files changed

+58
-34
lines changed
164 KB
Loading
43.4 KB
Loading

tutorials/pyproject-toml.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ The classifier key should look something like the example below. A few notes:
468468
- Your classifier values might be different depending upon your intended audience, development status of your package and the Python versions that you support
469469
- You can add as many classifiers as you wish as long as you use the [designated PyPI classifier values](https://PyPI.org/classifiers/).
470470

471-
{emphasize-lines="26-34"}
471+
{emphasize-lines="26-33"}
472472
```toml
473473
[build-system]
474474
requires = ["hatchling"]
@@ -514,10 +514,10 @@ Finally, add the project.urls table to your pyproject.toml file.
514514
`project.urls` contains links that are relevant for your project. You might want to include:
515515

516516
- **Homepage:** A link to your published documentation for your project. If you are working through this tutorial, then you may not have this link yet. That's ok, you can skip it for the time being.
517-
- **Bug reports:** a link to your issues / discussions or wherever you want users to report bugs.
517+
- **Bug reports:** a link to your issues/discussions or wherever you want users to report bugs.
518518
- **Source:** the GitHub / GitLab link for your project.
519519

520-
{emphasize-lines="36-39"}
520+
{emphasize-lines="35-38"}
521521
```toml
522522
[build-system]
523523
requires = ["hatchling"]

tutorials/trusted-publishing.md

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ In the previous Python packaging lessons, you learned:
1616
In this lesson, you will learn how to:
1717

1818
- Automate building and publishing the package on GitHub Actions
19-
- Configure trusted publishing for the project
20-
- Secure your workflow using action step GitHub hashes and versions
19+
- Configure PyPI Trusted Publishing for the project
20+
- Secure your workflow using GitHub action hashes and versions in your workflow file
2121

2222
This tutorial assumes that your project is hosted on GitHub and that you want
2323
to publish a package from your project to PyPI.
@@ -28,9 +28,15 @@ to publish a package from your project to PyPI.
2828
GitHub Actions[^gha] is an infrastructure provided by GitHub to automate
2929
software workflows, straight from the GitHub repository of the project. You can
3030
configure automated testing for every pull request, automate publishing of
31-
documentation, automate creation of webpages for the project, and even automate
32-
the release process. For this lesson, we will only focus on using actions to release
33-
and publish your Python package securely.
31+
documentation, automate creation of web pages for the project, and even automate
32+
the release process. For this lesson, we will focus on using actions to release
33+
and publish your Python package securely to PyPI.
34+
35+
:::{admonition} Why Trusted Publishing Matters
36+
37+
If you are wondering why trusted publishing is so important, [check out this blog post:](https://www.pyopensci.org/blog/python-packaging-security-publish-pypi.html) that dives deeper into what can happen when you don't lock down your publishing workflows.
38+
:::
39+
3440

3541
### Step 0: Create a release workflow
3642

@@ -44,7 +50,7 @@ GitHub's convention that all GitHub Actions are configured via YAML files in the
4450

4551
You can name the workflow file whatever you wish. We suggest using something
4652
simple and expressive like `release.yaml` so you, your future self, and contributors
47-
that work on your project know exactly what the workflow does.
53+
who work on your project know exactly what the workflow does.
4854
:::
4955

5056
### Step 1: Name the workflow
@@ -55,7 +61,7 @@ At the top of the `release.yaml` file, type the following:
5561
name: Release
5662
```
5763
58-
This provides a name to the workflow. It allows you to quickly find all runs of
64+
This provides a name to the workflow that you can use to quickly find all runs of
5965
this GitHub Action on the "Actions" tab in the GitHub repository.
6066
6167
:::{figure-md} github-actions-release-workflows-summary
@@ -66,9 +72,10 @@ This image shows an example of a configured workflow for the release. On the top
6672
6773
### Step 2: Add triggers to the workflow
6874
69-
Every GitHub Actions workflow runs only when [certain conditions](https://docs.github.com/en/actions/reference/events-that-trigger-workflows) are met. A
75+
Every GitHub Actions workflow runs when [certain conditions](https://docs.github.com/en/actions/reference/events-that-trigger-workflows) are met. In this case,
76+
we assume that a
7077
release workflow should only run when the repository owner creates a new [release](https://docs.github.com/en/repositories/releasing-projects-on-github/managing-releases-in-a-repository)
71-
for the package. Add the following to the `release.yaml` file:
78+
for the package. Add the following to the `release.yaml` file to ensure it runs when you create and publish a release:
7279

7380
```yaml
7481
on:
@@ -83,14 +90,14 @@ A GitHub Actions *workflow* file can contain multiple *jobs* that run independen
8390
When triggered, the GitHub Action runs all the jobs in a workflow (excluding any steps that have conditional requirements).
8491

8592
:::{note}
86-
Jobs and steps can also have [conditional logic](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idif) that allows them only to run if specific criteria exist. For instance, you may want only to have a job step to publish to PyPI if a release was made for the package.
93+
Jobs and steps can also have [conditional logic](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idif) that allows them only to run if specific criteria exist. For instance, you may want only to have a job step to publish to PyPI if a release was made for the package. But you might want to test building the package every time you merge a new pull request.
8794
:::
8895

89-
For a release job, we need to clone or check out the repository. We use the actions/checkout action to check out the code. You then use `hatch` to build
90-
the package.
96+
For a release job, you need to clone or check out the repository. You can use the `actions/checkout` action to check out the code. You then install and use `hatch` to build
97+
your package.
9198

9299
You also need to make sure to set up Hatch on the machine GitHub is
93-
using to run the workflow. The `pypa/hatch` action installs Hatch.
100+
using to run the workflow.
94101

95102
A minimal job definition would look like this:
96103

@@ -100,9 +107,16 @@ jobs:
100107
name: Build the package
101108
runs-on: ubuntu-latest
102109
steps:
103-
- uses: actions/checkout@v5 # This is the version of the checkout job that checks out your code.
104-
- uses: pypa/hatch
105-
- run: hatch build
110+
- uses: actions/checkout@v5
111+
- name: Setup Python
112+
uses: actions/setup-python@v6
113+
with:
114+
python-version: "3.12" # Select the version that you want to build your package on
115+
- name: Upgrade pip, install Hatch, and check Hatch version
116+
run: |
117+
pip install --upgrade pip
118+
pip install --upgrade hatch
119+
hatch --version # Verify that Hatch is installed
106120
```
107121

108122
Notice that above, you provide a version for each action step. `action/checkout@v5` tells GitHub to use version 5 of the checkout action. The checkout action checks out the code from your repository. In this case, the code will be used to build your package.
@@ -126,8 +140,8 @@ attack[^changed-files-supply-chain-attack]).
126140

127141
:::{tip}
128142
Enabling Dependabot[^dependabot] in
129-
the repository will ensure that you always get a PR to keep the actions up to
130-
date. Once Dependabot is enabled, it will update these hashes for you in the future!
143+
the repository will ensure that your actions stay up to date. The dependabot tool will open
144+
pull requests that update your action versions at whatever frequency you want.
131145
:::
132146

133147
Thus, the workflow that you should use should be similar to:
@@ -160,7 +174,7 @@ the following to the `release.yaml` file:
160174
:class: tip
161175

162176
Above, you have configured the artifact to be deleted after 1 day. The artifacts storage
163-
on GitHub actions is temporary; users should not be getting the package from here.
177+
on GitHub actions is temporary; users should not download your package from the GitHub artifacts.
164178

165179
You have also configured the release job to error if the `dist/` directory does
166180
not exist. This means that `hatch build` (from the previous step) failed to
@@ -221,32 +235,42 @@ the previous section:
221235

222236
:::{admonition} Make sure to change the URL
223237

224-
Remember to change the `url:` to the URL for your package on PyPI!
238+
Remember to change the `url:` value to the URL for your package on PyPI!
225239
:::
226240

227241
This job has two steps:
228242

229-
- as discussed above, it uses `download-artifact` to download the artifacts
243+
* It uses `download-artifact` to download the artifacts
230244
built in the previous job
231-
- it uses `gh-action-pypi-publish` to publish the package to PyPI.
245+
* It uses `gh-action-pypi-publish` to publish the package to PyPI.
232246

233-
You are almost there!! Now, you just need to enable trusted publishing for the project
247+
You are almost there!! Now, you just need to enable trusted publishing for your project
234248
on PyPI. And then, your work is done!
235249

236250
### Step 2: Enable trusted publishing on PyPI
237251

252+
:::{figure-md} trusted-publishing-image
253+
254+
<img src="../images/trusted-publisher-pypi-github.png" alt="Diagram showing PyPI's trusted publisher workflow: Step 1 builds distribution files via GitHub, Step 2 uses a trusted environment (PyPI), Step 3 securely uploads to PyPI. Shows chain of trust with lock icon connecting GitHub Action to Python Package Index." width="800px">
255+
256+
This lesson is the first in a series of lessons to help you get started with Python packaging.
257+
:::
258+
238259
Before trusted publishing was created, in order to upload to PyPI from GitHub
239260
actions you would have needed to add the username and password as arguments to
240261
the `gh-action-pypi-publish` step. While documentation recommends using the
241262
GitHub's `secrets` environment for the password/token, in several cases, users
242-
were pasting it directly in the workflow file. Furthermore, accidental leakage
243-
of the token could allow attackers to publish new packages in your name, until
244-
you discover the compromise and revoke the leaked credential.
263+
were pasting the password directly into the workflow file. Furthermore, accidental leakage
264+
of the password or token could allow attackers to publish new packages using your account, until
265+
you discover the compromise and revoke the leaked credentials.
266+
267+
To prevent these incidents and improve supply chain security, developers created [Trusted Publishing](https://docs.pypi.org/trusted-publishers/). Trusted publishing allows you to register a
268+
publishing workflow on
269+
PyPI and then map that workflow to an automation workflow (e.g., GitHub Actions) that is allowed
270+
to publish the package.
245271

246-
To prevent these incidents and improve supply chain security
247-
developers created [Trusted Publishing](https://docs.pypi.org/trusted-publishers/). This allows registering publishers on
248-
PyPI and mapping them to the automation workflow that is allowed to publish the
249-
package.
272+
You do not need to enter a token or password value in a trusted publisher workflow. It's a
273+
secure connection between your
250274

251275
:::{admonition} Trusted Publishing outside of GitHub Actions
252276
:class: tip
@@ -316,7 +340,7 @@ The workflow above should be up to date with the current versions of GitHub acti
316340

317341
## You have enabled trusted publishing for your project
318342

319-
Congratulations. You have now configured your project to do secure releases when a new version is being tagged on GitHub. The workflow we have configured builds the package from the exact version of code that we are tagging. This provides a guarantee for your users that the package we have released does exactly what the code states it does -- there is no potential for supply chain related vulnerabilities arising from our package! If you have a package that is ready for real-world use on the real PyPI, then you can follow the same steps to publish it securely.
343+
Congratulations!! You have now configured your project to do secure releases when a new version is being tagged on GitHub. The workflow we have configured builds the package from the exact version of code that we are tagging. This provides a guarantee for your users that the package that you have released does precisely what the code states it does. There is little to no potential for supply chain related vulnerabilities arising from your package! If you have a package that is ready for real-world use on the real PyPI, then you can follow the same steps to publish it securely.
320344

321345
## Footnotes
322346

0 commit comments

Comments
 (0)