File: publish.md

package info (click to toggle)
pdm 2.23.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 5,516 kB
  • sloc: python: 24,994; javascript: 34; makefile: 12
file content (81 lines) | stat: -rw-r--r-- 2,262 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Build and Publish

If you are developing a library, after adding dependencies to your project, and finishing the coding, it's time to build and publish your package. It is as simple as one command:

```bash
pdm publish
```

This will automatically build a wheel and a source distribution(sdist), and upload them to the PyPI index.

PyPI requires API tokens to publish packages, you can use `__token__` as the username and API token as the password.

To specify another repository other than PyPI, use the `--repository` option, the parameter can be either the upload URL or the name of the repository stored in the config file.

```bash
pdm publish --repository testpypi
pdm publish --repository https://test.pypi.org/legacy/
```

## Publish with trusted publishers

You can configure trusted publishers for PyPI so that you don't need to expose the PyPI tokens in the release workflow. To do this, follow
[the guide](https://docs.pypi.org/trusted-publishers/adding-a-publisher/) to add a publisher write a action as below:

### GitHub Actions

```yaml
on:
  release:
    types: [published]

jobs:
  pypi-publish:
    name: upload release to PyPI
    runs-on: ubuntu-latest
    permissions:
      # This permission is needed for private repositories.
      contents: read
      # IMPORTANT: this permission is mandatory for trusted publishing
      id-token: write
    steps:
      - uses: actions/checkout@v4

      - uses: pdm-project/setup-pdm@v4

      - name: Publish package distributions to PyPI
        run: pdm publish
```

### GitLab CI

```yaml
image: python:3.12-bookworm
before_script:
  - pip install pdm

publish-package:
  stage: release
  environment: production
  id_tokens:
    PYPI_ID_TOKEN: # for testpypi: TESTPYPI_ID_TOKEN
      aud: "pypi" # testpypi
  script:
    - pdm publish
```

## Build and publish separately

You can also build the package and upload it in two steps, to allow you to inspect the built artifacts before uploading.

```bash
pdm build
```

There are many options to control the build process, depending on the backend used. Refer to the [build configuration](../reference/build.md) section for more details.

The artifacts will be created at `dist/` and able to upload to PyPI.

```bash
pdm publish --no-build
```