Skip to content

Commit bac6c94

Browse files
authored
Add PyPI trusted publisher as an option (#93)
2 parents 377554a + 5b245cb commit bac6c94

File tree

8 files changed

+44
-24
lines changed

8 files changed

+44
-24
lines changed

.github/workflows/_pypi.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
on:
22
workflow_call:
3-
secrets:
4-
PYPI_TOKEN:
5-
required: true
63

74
jobs:
85
upload:
@@ -18,5 +15,3 @@ jobs:
1815

1916
- name: Publish to PyPI using trusted publishing
2017
uses: pypa/gh-action-pypi-publish@release/v1
21-
with:
22-
password: ${{ secrets.PYPI_TOKEN }}

catalog-info.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ spec:
8686
enum:
8787
- pyright
8888
- mypy
89+
pypi:
90+
title: PyPI?
91+
description: Would you like the wheel and source distribution to be automatically uploaded to PyPI when a release is made?
92+
type: boolean
93+
default: false
8994

9095
# This template is meant to be used on top of an existing template.
9196
# By adding the following and fetching from an absolute URL you can

copier.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ type_checker:
8686
- pyright
8787
- mypy
8888

89+
pypi:
90+
type: bool
91+
help: Would you like the wheel and source distribution to be automatically uploaded to PyPI when a release is made?
92+
8993
# Internal variables
9094
repo_url:
9195
type: str
@@ -106,3 +110,10 @@ _subdirectory: "template"
106110

107111
_tasks:
108112
- "git init --initial-branch=main"
113+
114+
_migrations:
115+
- version: 2.0.0
116+
before:
117+
- echo This update will require you to login to pypi.org and make changes before you can make a new release to PyPI. If you do not have time to do this now, press CTRL+C to abort this update.
118+
after:
119+
- echo Visit https://diamondlightsource.github.io/python-copier-template/main/how-to/pypi.html to find out how to set up PyPI trusted publishing

docs/how-to/pypi.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
# Creating a PyPI Token
1+
# Setting up PyPI publishing
22

3-
To publish your package on PyPI requires a PyPI account and for GitHub Actions to have a PyPI token authorizing access to that account.
3+
To publish your package on PyPI requires a PyPI account and for PyPI to be setup for [Trusted Publisher](https://docs.pypi.org/trusted-publishers/).
44

5-
The simplest approach is to set up a PyPI token that is scoped to your PyPI account and add it to the secrets for your GitHub Organization (or user). This means that all new projects created in the Organization will automatically gain permission to publish to PyPI.
5+
## Gather the information
66

7-
Alternatively you can create a project scoped token for each project. This is more work but more secure as a bad actor that obtains the key can only affect a single project.
7+
You will need the following information:
88

9-
If you do not already have a PyPI account use this link: [create_account].
9+
- Owner: The GitHub org that the repo is contained in, e.g. `DiamondLightSource`
10+
- Repository name: The GitHub repository name, e.g. `python-copier-template-example`
11+
- PyPI Project Name: The distribution name on PyPI, e.g. `dls-python-copier-template-example`
12+
- Workflow name: The workflow that does publishing, `_pypi.yml` for `python-copier-template` projects
13+
- Environment name: The GitHub environment that publishing is done with, `release` for `python-copier-template` projects
1014

11-
To learn how to create a token and store it in Github see: [adding_a_token]. You can ignore the other sections of the page regarding Github Actions because these are already provided by skeleton. Note that skeleton uses `PYPI_TOKEN` as the secret name instead of `PYPI_API_TOKEN` described in the link.
15+
## If publishing to the DiamondLightSource PyPI organisation
1216

13-
[adding_a_token]: https://packaging.python.org/en/latest/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/#saving-credentials-on-github
14-
[create_account]: https://pypi.org/account/register/
17+
If you are publishing to the DiamondLightSource PyPI organisation then use the above information and follow the [Developer Portal Guide on PyPI publishing](https://dev-portal.diamond.ac.uk/guide/python/how-tos/pypi/).
18+
19+
## If publishing the PyPI project to another organisation
20+
21+
If you are publishing to a different PyPI organisation then use the above information in one of the following guides:
22+
23+
- [Creating a PyPI project with a trusted publisher](https://docs.pypi.org/trusted-publishers/creating-a-project-through-oidc/)
24+
- [Adding a trusted publisher to an existing PyPI project](https://docs.pypi.org/trusted-publishers/adding-a-publisher/)

example-answers.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ github_org: DiamondLightSource
1010
package_name: python_copier_template_example
1111
repo_name: python-copier-template-example
1212
type_checker: pyright
13+
pypi: true

template/README.md.jinja

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[![CI]({{repo_url}}/actions/workflows/ci.yml/badge.svg)]({{repo_url}}/actions/workflows/ci.yml)
22
[![Coverage](https://codecov.io/gh/{{github_org}}/{{repo_name}}/branch/main/graph/badge.svg)](https://codecov.io/gh/{{github_org}}/{{repo_name}})
3-
[![PyPI](https://img.shields.io/pypi/v/{{distribution_name}}.svg)](https://pypi.org/project/{{distribution_name}})
3+
{% if pypi %}[![PyPI](https://img.shields.io/pypi/v/{{distribution_name}}.svg)](https://pypi.org/project/{{distribution_name}}){% endif %}
44
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
55

66
# {{ package_name }}
@@ -10,12 +10,12 @@
1010
This is where you should write a short paragraph that describes what your module does,
1111
how it does it, and why people should use it.
1212

13-
Source | <{{repo_url}}>
14-
:---: | :---:
15-
PyPI | `pip install {{distribution_name}}`
16-
{% if docker %}Docker | `docker run ghcr.io/{{github_org | lower}}/{{repo_name}}:latest`
17-
{% endif %}{% if sphinx %}Documentation | <{{docs_url}}>{% endif %}
18-
Releases | <{{repo_url}}/releases>
13+
{% if True %}Source | <{{repo_url}}>
14+
{% endif %}{% if True %}:---: | :---:
15+
{% endif %}{% if pypi %}PyPI | `pip install {{distribution_name}}`
16+
{% endif %}{% if docker %}Docker | `docker run ghcr.io/{{github_org | lower}}/{{repo_name}}:latest`
17+
{% endif %}{% if sphinx %}Documentation | <{{docs_url}}>
18+
{% endif %}Releases | <{{repo_url}}/releases>
1919

2020
This is where you should put some images or code snippets that illustrate
2121
some relevant examples. If it is a library then you might put some

template/{% if git_platform=="github.com" %}.github{% endif %}/workflows/ci.yml.jinja

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,14 @@ jobs:
5050
needs: check
5151
if: needs.check.outputs.branch-pr == ''
5252
uses: ./.github/workflows/_dist.yml
53-
{% raw %}
53+
{% if pypi %}
5454
pypi:
5555
if: github.ref_type == 'tag'
5656
needs: dist
5757
uses: ./.github/workflows/_pypi.yml
5858
permissions:
5959
id-token: write
60-
secrets:
61-
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
62-
{% endraw %}
60+
{% endif %}
6361
release:
6462
if: github.ref_type == 'tag'
6563
needs: [dist{% if sphinx %}, docs{% endif %}]

template/{% if git_platform=="github.com" %}.github{% endif %}/workflows/_pypi.yml renamed to template/{% if git_platform=="github.com" %}.github{% endif %}/workflows/{% if pypi %}_pypi.yml{% endif %}

File renamed without changes.

0 commit comments

Comments
 (0)