|
2 | 2 |
|
3 | 3 | [](https://www.gnu.org/licenses/gpl-3.0) [](https://github.com/psf/black) [](https://github.com/joergschultzelutter/pypi-publish-workflow/actions/workflows/codeql.yml) |
4 | 4 |
|
5 | | -This is a __Github Actions__ workflow for automatic publications to PyPi. Version data from a python file is extracted and then used by the PyPi setup process which will publish the package to PyPi Test and Prod. |
| 5 | +This is a __Github Actions__ workflow for automatic publications to PyPi. Version data from a python file is extracted and then used by the PyPi setup process which will publish the package to PyPi Test and Prod, following PyPi's [Trusted Publishing](https://docs.pypi.org/trusted-publishers/) model. |
6 | 6 |
|
7 | 7 | The workflow will only be triggered for the publication of new repo releases / prereleases for the 'master' repo branch. |
8 | 8 |
|
9 | | -## Installation instructions |
10 | | - |
11 | | -### Setup Github Secrets |
12 | | - |
13 | | -- Create token secrets for both [PyPi Test](https://test.pypi.org/) and [PyPi Prod](https://www.pypi.org/) (``Account Settings`` > ``API Tokens`` > ``Add API token``). |
14 | | -- In your Github project, goto ``Settings`` > ``Secrets and Variables`` > ``Actions`` |
15 | | -- Create two `Secrets` keys (`New repository secret`) named ``TEST_PYPI_API_TOKEN`` and ``PROD_PYPI_API_TOKEN`` and assign the previously created token secrets to these keys |
16 | | - |
17 | | -### Overview on config files |
| 9 | +## Overview on config files |
18 | 10 |
|
19 | 11 | This repo contains three files that you may need to amend and copy to your Github repository: |
20 | 12 |
|
21 | 13 | - ``setup.py``: this is a regular Python ``setup.py`` file; amend the file content with your package information and then save the file in your repo's root directory |
22 | 14 | - ``publish-to-pypi.yml``: Edit this file, amend the configuration settings (see next chapter) and then save the file in your repo's Github Actions directory (``.github/workflows``). You may also need to activate the new workflow once you have installed it - see [documentation on Github](https://docs.github.com/en/actions). |
23 | 15 |
|
24 | | -### `publish-to-pypi.yml` configuration |
| 16 | +## Configuration file instructions |
| 17 | + |
| 18 | +### `publish-to-pypi.yml` |
25 | 19 |
|
26 | 20 | Open the file. You will notice a section which looks like this: |
27 | 21 |
|
@@ -98,20 +92,42 @@ Necessary steps for a manual usage: |
98 | 92 | - open `setup.py` and assign a version number to the `GITHUB_PROGRAM_VERSION` variable |
99 | 93 | - `pip install git+https://github.com/my-repository-name@my-branch#egg=my-package-name` |
100 | 94 |
|
| 95 | +## Installation instructions |
| 96 | + |
| 97 | +The workflow uses PyPi's [Trusted Publisher](https://docs.pypi.org/trusted-publishers/) model. For a new project on PyPi, follow [these instructions](https://docs.pypi.org/trusted-publishers/creating-a-project-through-oidc/) for setting up a trusted publisher. For an existing project which you may want to migrate from a secret-based workflow to the new trusted workflows, use [these instructions](https://docs.pypi.org/trusted-publishers/adding-a-publisher/). |
| 98 | + |
| 99 | +### Step 1: set up a Github environment |
| 100 | + |
| 101 | +- In your GitHub project, go to **Settings > Environments** and create a new environment called `pypi`. |
| 102 | +- Configure `Required reviewers` or other settings, if necessary. You do NOT need to configure any secrets here. |
| 103 | + |
| 104 | +### Step 2: Deploy the workflow and the setup file |
| 105 | + |
| 106 | +- You need to configure the files prior to deployment, see previous chapter **Configuration file instructions** |
| 107 | +- `setup.py` goes into your repository's root directory |
| 108 | +- `publish-to-pypi.yml` goes into your repository's `.github/workflows` directory (or add as a new GitHub action) |
| 109 | + |
| 110 | +### Step 3: Trusted Publisher Setup |
| 111 | + |
| 112 | +- Log on to your PyPi Test & Prod accounts |
| 113 | +- Follow the instructions on how to set up a [Trusted Publisher](https://docs.pypi.org/trusted-publishers/) on both Test and Prod environments: |
| 114 | + - Set the `Workflow Name` to `publish-to-pypi.yml` |
| 115 | + - Set the `Environment` to `pypi` |
| 116 | + |
101 | 117 | ## Running the Github Action |
102 | 118 |
|
103 | 119 | This Github action will do the following __whenever a new release/pre-release is published for the 'master' branch__: |
104 | 120 |
|
105 | 121 | - Read the Python file and extract the version information, based on the given Regex. Abort job if no match was found. |
106 | 122 | - Check if the Github ``ref_type`` has the value ``tag``. This is only the case when you drafted a new release. Otherwise, this value is likely set to ``master``. Abort job in case of a mismatch. |
107 | 123 | - Check if the Github ``ref_name`` is equal to the extracted version from you Python file. Abort job in case of a mismatch. This will prevent issues where there is a mismatch between your Github release version (tag) and the one in the Python file. |
108 | | -- Build the PyPi package. Deploy it to PyPi Test and (if successful AND not a pre-release) PyPi Prod. |
| 124 | +- Build the PyPi package. Deploy it to PyPi Test and (if successful AND not a pre-release) PyPi Prod. Note: This is done as a separate workflow step, see [this issue](https://github.com/pypa/gh-action-pypi-publish/issues/319) for technical details. |
109 | 125 |
|
110 | 126 | This job will be triggered for releases AND prereleases in 'created' state (read: you tag a (pre)release in Github). Releases will be pushed to both PyPi Test and Prod whereas prereleases will only be pushed to PyPi Test. |
111 | 127 |
|
112 | 128 | ## Test your work flow |
113 | 129 |
|
114 | | -In case you want to become acquainted with this work flow: The safest way to test the work flow is to create both Github secret entries ``TEST_PYPI_API_TOKEN`` and ``PROD_PYPI_API_TOKEN`` but assign an invalid token to them. When you run the workflow for a new 'master' branch prerelease, the job will try to push it to PyPi Test and will fail because of the invalid token. |
| 130 | +- Publish your package as a prerelease. This should deploy your code only to PyPi Test. |
115 | 131 |
|
116 | 132 | ## Workflow |
117 | 133 | A basic workflow diagram of this Github Action can be found [here](docs/workflow.jpg) |
|
0 commit comments