Skip to content

Commit 17a568d

Browse files
committed
Upgrade for pyproject.toml and pypa/gh-action-pypi-publish, refs #6
1 parent cfa0e6e commit 17a568d

4 files changed

Lines changed: 71 additions & 64 deletions

File tree

.github/workflows/push.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- name: Checkout python-lib
13-
uses: actions/checkout@v3
13+
uses: actions/checkout@v4
1414
with:
1515
path: main
1616
- name: Checkout python-lib-template-demo
17-
uses: actions/checkout@v3
17+
uses: actions/checkout@v4
1818
with:
1919
repository: simonw/python-lib-template-demo
2020
path: python-lib-template-demo-current
2121
token: ${{ secrets.PERSONAL_ACCESS_TOKEN_FOR_PUSH }}
2222
- name: Set up Python
23-
uses: actions/setup-python@v4
23+
uses: actions/setup-python@v5
2424
with:
2525
python-version: "3.11"
2626
cache: pip

README.md

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,25 @@ Use this template on your own machine with cookiecutter, or create a brand new r
77
## Installation
88

99
You'll need to have [cookiecutter](https://cookiecutter.readthedocs.io/) installed. I recommend pipx for this:
10-
11-
pipx install cookiecutter
12-
10+
```bash
11+
pipx install cookiecutter
12+
```
1313
Regular `pip` will work OK too.
1414

1515
## Usage
1616

1717
Run `cookiecutter gh:simonw/python-lib` and then answer the prompts. Here's an example run:
18-
19-
$ cookiecutter gh:simonw/python-lib
20-
lib_name []: python lib template demo
21-
description []: Demonstrating https://github.com/simonw/python-lib
22-
hyphenated [python-lib-template-demo]:
23-
underscored [python_lib_template_demo]:
24-
github_username []: simonw
25-
author_name []: Simon Willison
26-
18+
```bash
19+
cookiecutter gh:simonw/python-lib
20+
```
21+
```
22+
lib_name []: python lib template demo
23+
description []: Demonstrating https://github.com/simonw/python-lib
24+
hyphenated [python-lib-template-demo]:
25+
underscored [python_lib_template_demo]:
26+
github_username []: simonw
27+
author_name []: Simon Willison
28+
```
2729
I strongly recommend accepting the suggested value for "hyphenated" and "underscored" by hitting enter on those prompts.
2830

2931
This will create a directory called `python-lib-template-demo` - the name you enter is converted to lowercase and uses hyphens instead of spaces.
@@ -35,54 +37,58 @@ See https://github.com/simonw/python-lib-template-demo for the output of this ex
3537
Having created the new structure from the template, here's how to start working on the library.
3638

3739
If your library is called `my-new-library`, you can start working on it like so:
38-
39-
cd my-new-library
40-
# Create and activate a virtual environment:
41-
python3 -mvenv venv
42-
source venv/bin/activate
43-
# Install dependencies so you can edit the project:
44-
pip install -e '.[test]'
45-
# With zsh you have to run this again for some reason:
46-
source venv/bin/activate
47-
40+
```bash
41+
cd my-new-library
42+
# Create and activate a virtual environment:
43+
python3 -mvenv venv
44+
source venv/bin/activate
45+
# Install dependencies so you can edit the project:
46+
pip install -e '.[test]'
47+
# With zsh you have to run this again for some reason:
48+
source venv/bin/activate
49+
```
4850
You can run the default test for your library like so:
49-
50-
pytest
51-
51+
```bash
52+
pytest
53+
```
5254
This will execute the test in `tests/test_my_new_library.py`.
5355

5456
## Creating a Git repository for your library
5557

5658
You can initialize a Git repository for your library like this:
57-
58-
cd my-new-library
59-
git init
60-
git add .
61-
git commit -m "Initial structure from template"
62-
# Rename the 'master' branch to 'main':
63-
git branch -m master main
64-
59+
```bash
60+
cd my-new-library
61+
git init
62+
git add .
63+
git commit -m "Initial structure from template"
64+
# Rename the 'master' branch to 'main':
65+
git branch -m master main
66+
```
6567
## Publishing your library to GitHub
6668

6769
Use https://github.com/new to create a new GitHub repository sharing the same name as your library, which should be something like `my-new-library`.
6870

6971
Push your `main` branch to GitHub like this:
70-
71-
git remote add origin git@github.com:YOURNAME/my-new-library.git
72-
git push -u origin main
73-
72+
```bash
73+
git remote add origin git@github.com:YOURNAME/my-new-library.git
74+
git push -u origin main
75+
```
7476
The template will have created a GitHub Action which runs your library's test suite against every commit.
7577

7678
## Publishing your library as a package to PyPI
7779

78-
The template also includes an Action for publishing packages to [PyPI](https://pypi.org/).
80+
The template also includes a `publish.yml` GitHub Actions workflow for publishing packages to [PyPI](https://pypi.org/), using [pypa/gh-action-pypi-publish](https://github.com/pypa/gh-action-pypi-publish).
7981

80-
To use this action, you need to create a PyPI account and an API token against that account.
82+
To use this action, you need to create a PyPI account and [configure a Trusted Publisher](https://til.simonwillison.net/pypi/pypi-releases-from-github) for this package.
8183

82-
Once you have created your account, navigate to https://pypi.org/manage/account/token/ and create an API token. For initial publication of the package you will need to set the scope of the token to "Entire account (all projects)".
84+
Once you have created your account, navigate to https://pypi.org/manage/account/publishing/ and create a "pending publisher" for the package. Use the following values:
8385

84-
Add that token to your repository as a GitHub secret called `PYPI_TOKEN`. You can find this in the "Settings -> Secrets -> New Secret" area of the repository. The token should begin with the string `pypi-`.
86+
- **PyPI Project Name:** The name of your package
87+
- **Owner:** Your GitHub username or organization - the "foo" in `github.com/foo/bar`
88+
- **Repsitory name:** The name of your repository - the "bar" in `github.com/foo/bar`
89+
- **Workflow name:** `publish.yml`
90+
- **Environment name:** `release`
8591

86-
Now, any time you create a new "Release" on GitHub the Action will build your package and push it to PyPI. The tag for the new release needs to match the `VERSION` string at the top of your `setup.py` file.
92+
Now, any time you create a new "Release" on GitHub the Action will build your package and push it to PyPI.
8793

88-
After the first release has gone out you can create a new PyPI API token that is scoped just to that project and use that to replace the `PYPI_TOKEN` secret in your GitHub repository settings.
94+
The tag for your release needs to match the `VERSION` string at the top of your `pyproject.toml` file. You should bump this version any time you release a new version of your package.

{{cookiecutter.hyphenated}}/.github/workflows/publish.yml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ jobs:
1212
runs-on: ubuntu-latest
1313
strategy:
1414
matrix:
15-
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
15+
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
1616
steps:
17-
- uses: actions/checkout@v3
17+
- uses: actions/checkout@v4
1818
- name: Set up Python ${{ matrix.python-version }}
19-
uses: actions/setup-python@v4
19+
uses: actions/setup-python@v5
2020
with:
2121
python-version: ${{ matrix.python-version }}
2222
cache: pip
23-
cache-dependency-path: setup.py
23+
cache-dependency-path: pyproject.toml
2424
- name: Install dependencies
2525
run: |
2626
pip install '.[test]'
@@ -30,22 +30,23 @@ jobs:
3030
deploy:
3131
runs-on: ubuntu-latest
3232
needs: [test]
33+
environment: release
34+
permissions:
35+
id-token: write
3336
steps:
34-
- uses: actions/checkout@v3
37+
- uses: actions/checkout@v4
3538
- name: Set up Python
36-
uses: actions/setup-python@v4
39+
uses: actions/setup-python@v5
3740
with:
38-
python-version: "3.11"
41+
python-version: "3.12"
3942
cache: pip
40-
cache-dependency-path: setup.py
43+
cache-dependency-path: pyproject.toml
4144
- name: Install dependencies
4245
run: |
43-
pip install setuptools wheel twine build
44-
- name: Publish
45-
env:
46-
TWINE_USERNAME: __token__
47-
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
46+
pip install setuptools wheel build
47+
- name: Build
4848
run: |
4949
python -m build
50-
twine upload dist/*
50+
- name: Publish
51+
uses: pypa/gh-action-pypi-publish@release/v1
5152
{% endraw %}

{{cookiecutter.hyphenated}}/.github/workflows/test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ jobs:
1010
runs-on: ubuntu-latest
1111
strategy:
1212
matrix:
13-
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
13+
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
1414
steps:
15-
- uses: actions/checkout@v3
15+
- uses: actions/checkout@v4
1616
- name: Set up Python ${{ matrix.python-version }}
17-
uses: actions/setup-python@v4
17+
uses: actions/setup-python@v5
1818
with:
1919
python-version: ${{ matrix.python-version }}
2020
cache: pip
21-
cache-dependency-path: setup.py
21+
cache-dependency-path: pyproject.toml
2222
- name: Install dependencies
2323
run: |
2424
pip install '.[test]'

0 commit comments

Comments
 (0)