Skip to content

Commit f1f92bc

Browse files
committed
feat: literal includes for the action
1 parent d7e8774 commit f1f92bc

File tree

1 file changed

+18
-88
lines changed

1 file changed

+18
-88
lines changed

tutorials/trusted-publishing.md

Lines changed: 18 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -132,28 +132,12 @@ date. Once Dependabot is enabled, it will update these hashes for you in the fut
132132

133133
Thus, the workflow that you should use should be similar to:
134134

135-
```{literalinclude} ../examples/pure-hatch/.github/release.yml
136-
:language: python
137-
:lines: 10-20
135+
```{literalinclude} ../examples/pure-hatch/.github/workflows/release.yml
136+
:language: yaml
137+
:lines: 1-23
138138
139139
```
140140

141-
142-
```yaml}
143-
jobs:
144-
build_package:
145-
name: Build the package
146-
runs-on: ubuntu-latest
147-
steps:
148-
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0
149-
with:
150-
persist-credentials: false
151-
- name: Set up Hatch
152-
uses: pypa/hatch@257e27e51a6a5616ed08a39a408a21c35c9931bc
153-
- name: Build artifacts
154-
run: hatch build
155-
```
156-
157141
Now, you can commit the `.github/workflows/release.yaml` file to the repository and push to GitHub.
158142

159143
At this point, if you create a new release for your project on GitHub, the
@@ -166,13 +150,10 @@ You need to add one more step to the job definition to be able to access the
166150
wheel. You will upload it to the artifacts temporary area[^github-artifacts]. Add
167151
the following to the `release.yaml` file:
168152

169-
```yaml
170-
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
171-
with:
172-
path: dist/
173-
name: dist.zip
174-
if-no-files-found: error
175-
retention-days: 1
153+
```{literalinclude} ../examples/pure-hatch/.github/workflows/release.yml
154+
:language: yaml
155+
:lines: 24-29
156+
176157
```
177158

178159
:::{admonition} Upload artifacts parameters
@@ -230,20 +211,11 @@ package could get compromised before the release.
230211
In the `release.yaml` file, add the following new job, after the job defined in
231212
the previous section:
232213

233-
```yaml
234-
publish_release_to_pypi:
235-
name: Publish release to PyPI
236-
needs: [build_package] # only run if `build_package` succeeded
237-
runs-on: ubuntu-latest
238-
environment:
239-
name: pypi
240-
url: <URL TO YOUR PROJECT HERE>
241-
steps:
242-
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
243-
with:
244-
name: dist.zip
245-
path: dist/
246-
- uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4
214+
215+
```{literalinclude} ../examples/pure-hatch/.github/workflows/release.yml
216+
:language: yaml
217+
:lines: 31-46
218+
247219
```
248220

249221
:::{admonition} Make sure to change the URL
@@ -321,65 +293,23 @@ the following information:
321293
Once you fill in this form and click "Add" the publisher is configured and can
322294
be used to publish new releases of your package.
323295

324-
:::{admonition} Fully hardened GitHub Actions release workflow
296+
## Fully hardened GitHub Actions release workflow
325297

326-
For better security it is recommended to also control the permissions of the
298+
For better security, it is also recommended to control the permissions of the
327299
GitHub token used within each job of the workflow. The permissions should be
328300
scoped at job level and be as minimal as possible. A workflow that configures
329301
trusted publishing and also does this is the following:
330302

331-
```yaml
332-
name: Release
333-
334-
on:
335-
release:
336-
types:
337-
- published
303+
```{literalinclude} ../examples/pure-hatch/.github/workflows/release.yml
304+
:language: yaml
338305
339-
permissions: {} # no permissions to the token at global level
340-
341-
jobs:
342-
build_package:
343-
name: Build the package
344-
runs-on: ubuntu-latest
345-
permissions:
346-
contents: read # this job only needs read access
347-
steps:
348-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
349-
with:
350-
persist-credentials: false
351-
- name: Set up Hatch
352-
uses: pypa/hatch@257e27e51a6a5616ed08a39a408a21c35c9931bc
353-
- name: Build artifacts
354-
run: hatch build
355-
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
356-
with:
357-
path: dist/
358-
name: dist.zip
359-
if-no-files-found: error
360-
retention-days: 1
361-
362-
publish_release_to_pypi:
363-
name: Publish release to PyPI
364-
needs: [build_package]
365-
runs-on: ubuntu-latest
366-
environment:
367-
name: pypi
368-
url: <URL TO YOUR PROJECT HERE>
369-
permissions:
370-
contents: read # this job needs read access
371-
id-token: write # but also needs to be able to write the publishing token
372-
steps:
373-
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
374-
with:
375-
name: dist.zip
376-
path: dist/
377-
- uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4
378306
```
379307

380308
You can copy the above into your `release.yaml` file. You only need to update
381309
the `url:` field and configure trusted publishing on PyPI.
382310

311+
:::{note}
312+
The workflow above should be up to date with the current versions of GitHub actions. However, it's good to turn on Dependabot to update the action versions in the future.
383313
:::
384314

385315
## You have enabled trusted publishing for your project

0 commit comments

Comments
 (0)