2022-07-25 22:42:56 +08:00
|
|
|
[![SWUbanner]][SWUdocs]
|
|
|
|
|
2022-12-07 05:59:26 +08:00
|
|
|
[![🧪 GitHub Actions CI/CD workflow tests badge]][GHA workflow runs list]
|
|
|
|
[![pre-commit.ci status badge]][pre-commit.ci results page]
|
|
|
|
|
2019-03-28 03:58:36 +08:00
|
|
|
# PyPI publish GitHub Action
|
2021-02-20 03:00:25 +08:00
|
|
|
|
2020-06-03 23:53:04 +08:00
|
|
|
This action allows you to upload your [Python distribution packages]
|
2019-11-27 05:07:42 +08:00
|
|
|
in the `dist/` directory to PyPI.
|
2019-09-27 19:37:19 +08:00
|
|
|
This text suggests a minimalistic usage overview. For more detailed
|
|
|
|
walkthrough check out the [PyPA guide].
|
2019-03-28 03:58:36 +08:00
|
|
|
|
2023-04-04 00:19:33 +08:00
|
|
|
If you have any feedback regarding specific action versions, please leave
|
|
|
|
comments in the corresponding [per-release announcement discussions].
|
|
|
|
|
2019-03-28 03:58:36 +08:00
|
|
|
|
2022-07-25 23:25:08 +08:00
|
|
|
## 🌇 `master` branch sunset ❗
|
|
|
|
|
|
|
|
The `master` branch version has been sunset. Please, change the GitHub
|
|
|
|
Action version you use from `master` to `release/v1` or use an exact
|
2023-07-13 22:44:47 +08:00
|
|
|
tag, or opt-in to [use a full Git commit SHA] and Dependabot.
|
2022-07-25 23:25:08 +08:00
|
|
|
|
|
|
|
|
2019-03-28 03:58:36 +08:00
|
|
|
## Usage
|
2019-08-21 04:48:52 +08:00
|
|
|
|
2023-04-03 22:36:59 +08:00
|
|
|
### Trusted publishing
|
2023-03-07 04:03:34 +08:00
|
|
|
|
2024-02-24 18:46:07 +08:00
|
|
|
> [!NOTE]
|
|
|
|
> Trusted publishing cannot be used from within a reusable workflow at this
|
2023-11-29 11:44:26 +08:00
|
|
|
> time. It is recommended to instead create a non-reusable workflow that contains a
|
|
|
|
> job calling your reusable workflow, and then do the trusted publishing step from
|
|
|
|
> a separate job within that non-reusable workflow. Alternatively, you can still
|
|
|
|
> use a username/token inside the reusable workflow.
|
2023-10-23 16:00:32 +08:00
|
|
|
|
2023-08-10 05:58:56 +08:00
|
|
|
> [!NOTE]
|
|
|
|
> Trusted publishing is sometimes referred to by its
|
2023-04-22 21:27:01 +08:00
|
|
|
> underlying technology -- OpenID Connect, or OIDC for short.
|
|
|
|
> If you see references to "OIDC publishing" in the context of PyPI,
|
|
|
|
> this is what they're referring to.
|
|
|
|
|
2023-04-24 13:26:17 +08:00
|
|
|
This example jumps right into the current best practice. If you want to
|
2023-04-24 23:28:57 +08:00
|
|
|
use API tokens directly or a less secure username and password, check out
|
|
|
|
[how to specify username and password].
|
2023-04-24 13:26:17 +08:00
|
|
|
|
2023-04-03 20:26:53 +08:00
|
|
|
This action supports PyPI's [trusted publishing]
|
2023-03-07 04:03:34 +08:00
|
|
|
implementation, which allows authentication to PyPI without a manually
|
|
|
|
configured API token or username/password combination. To perform
|
2023-04-03 20:26:53 +08:00
|
|
|
[trusted publishing] with this action, your project's
|
2023-08-11 23:23:40 +08:00
|
|
|
publisher must already be [configured on PyPI].
|
2023-03-07 04:03:34 +08:00
|
|
|
|
2023-04-03 20:26:53 +08:00
|
|
|
To enter the trusted publishing flow, configure this action's job with the
|
|
|
|
`id-token: write` permission and **without** an explicit username or password:
|
2023-03-07 04:03:34 +08:00
|
|
|
|
|
|
|
```yaml
|
2023-04-24 13:26:17 +08:00
|
|
|
# .github/workflows/ci-cd.yml
|
2023-03-07 04:03:34 +08:00
|
|
|
jobs:
|
|
|
|
pypi-publish:
|
|
|
|
name: Upload release to PyPI
|
|
|
|
runs-on: ubuntu-latest
|
2023-04-24 13:04:43 +08:00
|
|
|
environment:
|
|
|
|
name: pypi
|
|
|
|
url: https://pypi.org/p/<your-pypi-project-name>
|
2023-03-07 04:03:34 +08:00
|
|
|
permissions:
|
2023-04-03 20:26:53 +08:00
|
|
|
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
|
2023-03-07 04:03:34 +08:00
|
|
|
steps:
|
|
|
|
# retrieve your distributions here
|
|
|
|
|
|
|
|
- name: Publish package distributions to PyPI
|
|
|
|
uses: pypa/gh-action-pypi-publish@release/v1
|
|
|
|
```
|
|
|
|
|
2023-08-10 05:58:56 +08:00
|
|
|
> [!NOTE]
|
2023-08-10 06:01:51 +08:00
|
|
|
> Pro tip: instead of using branch pointers, like `unstable/v1`, pin versions of
|
|
|
|
> Actions that you use to tagged versions or sha1 commit identifiers.
|
2023-08-10 05:58:56 +08:00
|
|
|
> This will make your workflows more secure and better reproducible, saving you
|
|
|
|
> from sudden and unpleasant surprises.
|
2023-04-24 13:26:17 +08:00
|
|
|
|
2023-04-03 20:26:53 +08:00
|
|
|
Other indices that support trusted publishing can also be used, like TestPyPI:
|
2023-03-07 04:03:34 +08:00
|
|
|
|
|
|
|
```yaml
|
|
|
|
- name: Publish package distributions to TestPyPI
|
|
|
|
uses: pypa/gh-action-pypi-publish@release/v1
|
|
|
|
with:
|
|
|
|
repository-url: https://test.pypi.org/legacy/
|
|
|
|
```
|
2023-04-24 13:04:43 +08:00
|
|
|
_(don't forget to update the environment name to `testpypi` or similar!)_
|
2023-03-07 04:03:34 +08:00
|
|
|
|
2023-08-10 05:58:56 +08:00
|
|
|
> [!NOTE]
|
2023-08-10 06:01:51 +08:00
|
|
|
> Pro tip: only set the `id-token: write` permission in the job that does
|
2023-03-07 04:03:34 +08:00
|
|
|
> publishing, not globally. Also, try to separate building from publishing
|
|
|
|
> — this makes sure that any scripts maliciously injected into the build
|
|
|
|
> or test environment won't be able to elevate privileges while flying under
|
|
|
|
> the radar.
|
|
|
|
|
2023-04-24 13:26:17 +08:00
|
|
|
A common use case is to upload packages only on a tagged commit, to do so add a
|
|
|
|
filter to the job:
|
|
|
|
|
|
|
|
```yml
|
|
|
|
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
|
|
|
|
```
|
|
|
|
|
2024-09-01 08:50:29 +08:00
|
|
|
### Generating and uploading attestations
|
|
|
|
|
|
|
|
> [!IMPORTANT]
|
|
|
|
> Support for generating and uploading [digital attestations] is currently
|
|
|
|
> experimental and limited only to Trusted Publishing flows using PyPI or TestPyPI.
|
|
|
|
> Support for this feature is not yet stable; the settings and behavior described
|
|
|
|
> below may change without prior notice.
|
|
|
|
|
|
|
|
> [!NOTE]
|
|
|
|
> Generating and uploading digital attestations currently requires
|
|
|
|
> authentication with a [trusted publisher].
|
|
|
|
|
2024-10-30 09:20:55 +08:00
|
|
|
Generating signed [digital attestations] for all the distribution files
|
|
|
|
and uploading them all together is now on by default for all projects
|
|
|
|
using Trusted Publishing. To disable it, set `attestations` as follows:
|
2024-09-01 08:50:29 +08:00
|
|
|
|
|
|
|
```yml
|
|
|
|
with:
|
2024-10-30 09:20:55 +08:00
|
|
|
attestations: false
|
2024-09-01 08:50:29 +08:00
|
|
|
```
|
|
|
|
|
2024-10-30 09:20:55 +08:00
|
|
|
The attestation objects are created using [Sigstore] for each
|
|
|
|
distribution package, signing them with the identity provided
|
2024-09-01 08:50:29 +08:00
|
|
|
by the GitHub's OIDC token associated with the current workflow. This means
|
|
|
|
both the trusted publishing authentication and the attestations are tied to the
|
|
|
|
same identity.
|
2023-03-07 04:03:34 +08:00
|
|
|
|
2019-09-12 20:06:55 +08:00
|
|
|
## Non-goals
|
|
|
|
|
|
|
|
This GitHub Action [has nothing to do with _building package
|
|
|
|
distributions_]. Users are responsible for preparing dists for upload
|
|
|
|
by putting them into the `dist/` folder prior to running this Action.
|
|
|
|
|
2023-08-10 05:58:56 +08:00
|
|
|
> [!IMPORTANT]
|
|
|
|
> Since this GitHub Action is docker-based, it can only
|
2021-02-20 03:22:31 +08:00
|
|
|
> be used from within GNU/Linux based jobs in GitHub Actions CI/CD
|
|
|
|
> workflows. This is by design and is unlikely to change due to a number
|
|
|
|
> of considerations we rely on.
|
|
|
|
>
|
|
|
|
> This should not stop one from publishing platform-specific
|
|
|
|
> distribution packages, though. It is strongly advised to separate jobs
|
|
|
|
> for building the OS-specific wheels from the publish job. This allows
|
|
|
|
> one to (1) test exactly the same artifacts that are about to be
|
|
|
|
> uploaded to PyPI, (2) prevent parallel unsynchronized jobs from
|
|
|
|
> publishing only part of the dists asynchronously (in case when part of
|
|
|
|
> the jobs fail and others succeed ending up with an incomplete release
|
|
|
|
> on PyPI) and (3) make an atomic upload to PyPI (when part of the dists
|
|
|
|
> appear on PyPI, installers like pip will use that version for the
|
|
|
|
> dependency resolution but this may cause some environments to use
|
2021-02-20 03:28:01 +08:00
|
|
|
> sdists while the wheel for their runtime is not yet available).
|
2021-02-20 03:22:31 +08:00
|
|
|
>
|
|
|
|
> To implement this sort of orchestration, please use
|
|
|
|
> `actions/upload-artifact` and `actions/download-artifact` actions for
|
|
|
|
> sharing the built dists across stages and jobs. Then, use the `needs`
|
|
|
|
> setting to order the build, test and publish stages.
|
|
|
|
|
2019-09-12 20:06:55 +08:00
|
|
|
|
2019-09-15 15:16:54 +08:00
|
|
|
## Advanced release management
|
|
|
|
|
|
|
|
For best results, figure out what kind of workflow fits your
|
|
|
|
project's specific needs.
|
2019-09-16 19:01:16 +08:00
|
|
|
|
2023-04-24 13:26:17 +08:00
|
|
|
For example, you could implement a parallel job that
|
2019-09-19 16:04:14 +08:00
|
|
|
pushes every commit to TestPyPI or your own index server,
|
2019-09-15 15:16:54 +08:00
|
|
|
like `devpi`. For this, you'd need to (1) specify a custom
|
2023-03-11 08:18:41 +08:00
|
|
|
`repository-url` value and (2) generate a unique version
|
2019-09-15 15:16:54 +08:00
|
|
|
number for each upload so that they'd not create a conflict.
|
2019-09-16 19:01:16 +08:00
|
|
|
The latter is possible if you use `setuptools_scm` package but
|
2019-09-15 15:16:54 +08:00
|
|
|
you could also invent your own solution based on the distance
|
|
|
|
to the latest tagged commit.
|
|
|
|
|
2023-04-24 13:26:17 +08:00
|
|
|
You'll need to create another token for a separate host and then [save it as a
|
|
|
|
GitHub repo secret][Creating & using secrets] under an environment used in
|
|
|
|
your job. Though, passing a password would disable the secretless [trusted
|
|
|
|
publishing] so it's better to configure it instead, when publishing to TestPyPI
|
|
|
|
and not something custom.
|
2019-09-15 21:24:35 +08:00
|
|
|
|
2019-09-15 15:16:54 +08:00
|
|
|
The action invocation in this case would look like:
|
|
|
|
```yml
|
2019-09-19 16:04:14 +08:00
|
|
|
- name: Publish package to TestPyPI
|
2021-02-20 03:05:09 +08:00
|
|
|
uses: pypa/gh-action-pypi-publish@release/v1
|
2019-09-15 15:16:54 +08:00
|
|
|
with:
|
2020-12-13 00:07:19 +08:00
|
|
|
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
|
2023-03-11 08:18:41 +08:00
|
|
|
repository-url: https://test.pypi.org/legacy/
|
2019-09-15 15:16:54 +08:00
|
|
|
```
|
|
|
|
|
2019-12-06 20:44:40 +08:00
|
|
|
### Customizing target package dists directory
|
2019-12-06 07:25:02 +08:00
|
|
|
|
|
|
|
You can change the default target directory of `dist/`
|
|
|
|
to any directory of your liking. The action invocation
|
|
|
|
would now look like:
|
|
|
|
|
|
|
|
```yml
|
2019-12-06 20:42:24 +08:00
|
|
|
- name: Publish package to PyPI
|
2021-02-20 03:05:09 +08:00
|
|
|
uses: pypa/gh-action-pypi-publish@release/v1
|
2019-12-06 07:25:02 +08:00
|
|
|
with:
|
2023-03-11 08:18:41 +08:00
|
|
|
packages-dir: custom-dir/
|
2019-12-06 07:25:02 +08:00
|
|
|
```
|
2019-09-15 15:16:54 +08:00
|
|
|
|
2020-06-03 23:04:52 +08:00
|
|
|
### Disabling metadata verification
|
2020-06-02 23:08:43 +08:00
|
|
|
|
2020-06-03 23:04:52 +08:00
|
|
|
It is recommended that you run `twine check` just after producing your files,
|
|
|
|
but this also runs `twine check` before upload. You can also disable the twine
|
|
|
|
check with:
|
2020-06-02 23:08:43 +08:00
|
|
|
|
|
|
|
```yml
|
|
|
|
with:
|
2023-03-11 08:18:41 +08:00
|
|
|
verify-metadata: false
|
2020-06-02 23:08:43 +08:00
|
|
|
```
|
|
|
|
|
2020-06-20 03:30:53 +08:00
|
|
|
### Tolerating release package file duplicates
|
|
|
|
|
|
|
|
Sometimes, when you publish releases from multiple places, your workflow
|
|
|
|
may hit race conditions. For example, when publishing from multiple CIs
|
2020-07-09 15:45:41 +08:00
|
|
|
or even having workflows with the same steps triggered within GitHub
|
2020-06-20 03:30:53 +08:00
|
|
|
Actions CI/CD for different events concerning the same high-level act.
|
|
|
|
|
2023-03-11 08:18:41 +08:00
|
|
|
To facilitate this use-case, you may use `skip-existing` (disabled by
|
2020-06-20 03:30:53 +08:00
|
|
|
default) setting as follows:
|
|
|
|
|
|
|
|
```yml
|
|
|
|
with:
|
2023-03-11 08:18:41 +08:00
|
|
|
skip-existing: true
|
2020-06-20 03:30:53 +08:00
|
|
|
```
|
|
|
|
|
2023-08-10 05:58:56 +08:00
|
|
|
> [!NOTE]
|
2023-08-10 06:01:51 +08:00
|
|
|
> Pro tip: try to avoid enabling this setting where possible. If you
|
2023-08-10 05:58:56 +08:00
|
|
|
> have steps for publishing to both PyPI and TestPyPI, consider only using
|
|
|
|
> it for the latter, having the former fail loudly on duplicates.
|
2020-06-20 03:30:53 +08:00
|
|
|
|
2020-09-15 12:31:21 +08:00
|
|
|
### For Debugging
|
|
|
|
|
|
|
|
Sometimes, `twine upload` can fail and to debug use the `verbose` setting as follows:
|
|
|
|
|
|
|
|
```yml
|
|
|
|
with:
|
|
|
|
verbose: true
|
|
|
|
```
|
|
|
|
|
2022-01-08 12:41:13 +08:00
|
|
|
### Showing hash values of files to be uploaded
|
|
|
|
|
|
|
|
You may want to verify whether the files on PyPI were automatically uploaded by CI script.
|
|
|
|
It will show SHA256, MD5, BLAKE2-256 values of files to be uploaded.
|
|
|
|
|
|
|
|
```yml
|
|
|
|
with:
|
2023-03-11 08:18:41 +08:00
|
|
|
print-hash: true
|
2022-01-08 12:41:13 +08:00
|
|
|
```
|
|
|
|
|
2022-07-26 05:13:35 +08:00
|
|
|
### Specifying a different username
|
|
|
|
|
|
|
|
The default username value is `__token__`. If you publish to a custom
|
|
|
|
registry that does not provide API tokens, like `devpi`, you may need to
|
|
|
|
specify a custom username and password pair. This is how it's done.
|
|
|
|
|
|
|
|
```yml
|
|
|
|
with:
|
|
|
|
user: guido
|
|
|
|
password: ${{ secrets.DEVPI_PASSWORD }}
|
|
|
|
```
|
|
|
|
|
2023-04-24 13:26:17 +08:00
|
|
|
The secret used in `${{ secrets.DEVPI_PASSWORD }}` needs to be created on the
|
|
|
|
environment page under the settings of your project on GitHub.
|
|
|
|
See [Creating & using secrets].
|
|
|
|
|
|
|
|
In the past, when publishing to PyPI, the most secure way of the access scoping
|
|
|
|
for automatic publishing was to use the [API tokens][PyPI API token] feature of
|
|
|
|
PyPI. One would make it project-scoped and save as an environment-bound secret
|
|
|
|
in their GitHub repository settings, naming it `${{ secrets.PYPI_API_TOKEN }}`,
|
2023-04-24 23:28:57 +08:00
|
|
|
for example. See [Creating & using secrets]. While still secure,
|
|
|
|
[trusted publishing] is now encouraged over API tokens as a best practice
|
|
|
|
on supported platforms (like GitHub).
|
2023-04-24 13:26:17 +08:00
|
|
|
|
2019-03-28 03:58:36 +08:00
|
|
|
## License
|
2019-08-21 04:48:52 +08:00
|
|
|
|
2019-03-28 03:58:36 +08:00
|
|
|
The Dockerfile and associated scripts and documentation in this project
|
2019-03-30 06:48:49 +08:00
|
|
|
are released under the [BSD 3-clause license](LICENSE.md).
|
2019-08-21 04:48:52 +08:00
|
|
|
|
|
|
|
|
2022-12-07 05:59:26 +08:00
|
|
|
[🧪 GitHub Actions CI/CD workflow tests badge]:
|
2024-11-06 05:29:18 +08:00
|
|
|
https://github.com/pypa/gh-action-pypi-publish/actions/workflows/build-and-push-docker-image.yml/badge.svg?branch=unstable%2Fv1&event=push
|
2022-12-07 05:59:26 +08:00
|
|
|
[GHA workflow runs list]:
|
|
|
|
https://github.com/pypa/gh-action-pypi-publish/actions/workflows/self-smoke-test-action.yml?query=branch%3Aunstable%2Fv1
|
|
|
|
|
|
|
|
[pre-commit.ci results page]:
|
2022-12-07 06:24:07 +08:00
|
|
|
https://results.pre-commit.ci/latest/github/pypa/gh-action-pypi-publish/unstable/v1
|
2022-12-07 05:59:26 +08:00
|
|
|
[pre-commit.ci status badge]:
|
2022-12-07 06:24:07 +08:00
|
|
|
https://results.pre-commit.ci/badge/github/pypa/gh-action-pypi-publish/unstable/v1.svg
|
2022-12-07 05:59:26 +08:00
|
|
|
|
2023-07-13 22:44:47 +08:00
|
|
|
[use a full Git commit SHA]:
|
|
|
|
https://julienrenaux.fr/2019/12/20/github-actions-security-risk/
|
|
|
|
|
2023-04-04 00:19:33 +08:00
|
|
|
[per-release announcement discussions]:
|
|
|
|
https://github.com/pypa/gh-action-pypi-publish/discussions/categories/announcements
|
|
|
|
|
2019-09-25 05:04:57 +08:00
|
|
|
[Creating & using secrets]:
|
2019-11-24 02:35:12 +08:00
|
|
|
https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets
|
2019-09-12 20:06:55 +08:00
|
|
|
[has nothing to do with _building package distributions_]:
|
|
|
|
https://github.com/pypa/gh-action-pypi-publish/issues/11#issuecomment-530480449
|
2019-09-27 19:37:19 +08:00
|
|
|
[PyPA guide]:
|
|
|
|
https://packaging.python.org/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/
|
2020-06-03 23:53:04 +08:00
|
|
|
[PyPI API token]: https://pypi.org/help/#apitoken
|
|
|
|
[Python distribution packages]:
|
2022-07-26 04:55:14 +08:00
|
|
|
https://packaging.python.org/glossary/#term-Distribution-Package
|
2022-07-25 22:42:56 +08:00
|
|
|
[SWUbanner]:
|
|
|
|
https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner-direct-single.svg
|
|
|
|
[SWUdocs]:
|
|
|
|
https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md
|
2023-03-07 04:03:34 +08:00
|
|
|
|
|
|
|
[warehouse#12965]: https://github.com/pypi/warehouse/issues/12965
|
2023-04-03 20:26:53 +08:00
|
|
|
[trusted publishing]: https://docs.pypi.org/trusted-publishers/
|
2023-08-11 23:23:40 +08:00
|
|
|
[configured on PyPI]: https://docs.pypi.org/trusted-publishers/adding-a-publisher/
|
2023-04-24 13:26:17 +08:00
|
|
|
|
|
|
|
[how to specify username and password]: #specifying-a-different-username
|
2024-09-01 08:50:29 +08:00
|
|
|
|
|
|
|
[digital attestations]: https://peps.python.org/pep-0740/
|
|
|
|
[Sigstore]: https://www.sigstore.dev/
|
|
|
|
[trusted publisher]: #trusted-publishing
|