---
myst:
  html_meta:
    "description": "Publish packages released with repoplone through trusted publishing on PyPI and npm, or give repoplone the tokens to publish them itself, and set up a GitHub token."
    "property=og:description": "Publish packages released with repoplone through trusted publishing on PyPI and npm, or give repoplone the tokens to publish them itself, and set up a GitHub token."
    "property=og:title": "Authenticate to PyPI, npm, and GitHub"
    "keywords": "Plone, repoplone, release, trusted publishing, PyPI, npm, GitHub, token, UV_PUBLISH_TOKEN, GITHUB_TOKEN"
---

(how-to-authenticate)=

# Authenticate to PyPI, npm, and GitHub

This guide shows you how to set up the credentials a release needs.
It covers trusted publishing for PyPI and npm, tokens for repoplone, and the GitHub token.

| Credential | Needed when |
|---|---|
| PyPI | a Python package is published |
| npm | a Node package is published |
| GitHub | the release creates a GitHub release |

(how-to-authenticate-trusted)=

## Publish with trusted publishing

PyPI and npm both recommend trusted publishing.
A GitHub Actions workflow proves its identity to the registry, and gets a short-lived credential for one upload.
No long-lived token is stored on a computer or in the repository.

With trusted publishing, `repoplone release` prepares the release, and a workflow publishes it.

1. Set `publish = false` on each package in `repository.toml`.

   ```toml
   [[package]]
   type = "python"
   name = "my.package"
   path = "."
   changelog = "CHANGELOG.md"
   publish = false
   ```

   `repoplone release` still sets the version, builds the changelog, tags the release, and creates the GitHub release.
   It no longer uploads the package.
   It no longer needs PyPI or npm credentials either.

1. Register the workflow as a trusted publisher.
   On PyPI, add it in the publishing settings of the project.
   On npm, add it in the settings of the package.
   Each registry asks for the repository, the workflow file name, and optionally a GitHub environment.

1. Add a workflow that publishes when a GitHub release is published.
   The job that uploads needs the `id-token: write` permission.

   ```yaml
   name: Release

   on:
     release:
       types:
         - published

   permissions:
     contents: read

   jobs:
     publish:
       runs-on: ubuntu-latest
       environment: release
       permissions:
         id-token: write
       steps:
         - uses: actions/checkout@v7
           with:
             ref: ${{ github.event.release.tag_name }}
         - uses: astral-sh/setup-uv@v7
         - run: uv build
         - uses: pypa/gh-action-pypi-publish@release/v1
   ```

   For a Node package, run `npm publish` in the package folder from a job with the same permission.
   npm trusted publishing requires npm 11.5.1 or later, and Node 22.14.0 or later.

repoplone publishes itself this way.
Its [release workflow](https://github.com/plone/repoplone/blob/main/.github/workflows/release.yml) also accepts a tag by hand, to retry a failed upload.

```{note}
GitHub starts no workflow for a release created with the `GITHUB_TOKEN` of another workflow.
Run `repoplone release` from your computer with a personal token, or trigger the publishing workflow on the tag instead.
```

(how-to-authenticate-pypi)=

## Let repoplone publish to PyPI

To have repoplone upload a Python package itself, keep `publish = true` and give it a token.
Create an API token in your PyPI account settings, scoped to the project when it already exists.
Export it before you release.

```shell
export UV_PUBLISH_TOKEN="pypi-your-token"
```

`uv publish` uploads with this token.
repoplone also accepts a token stored for `pypi.org` with `uv auth login`.

Without either, the release stops with `You are not authenticated to PyPi using UV.`

(how-to-authenticate-npm)=

## Let repoplone publish to npm

To have repoplone release a Node package with release-it, keep `publish = true` and log in to the npm registry.

```shell
npm login
```

repoplone runs `npm whoami` to check the login.
release-it then publishes with the same credentials.

(how-to-authenticate-github)=

## GitHub

Create a token that can create releases in the repository.
A fine-grained token needs the {guilabel}`Contents` permission set to {guilabel}`Read and write`.
Export it before you release.

```shell
export GITHUB_TOKEN="github_pat_your-token"
```

Without `GITHUB_TOKEN`, repoplone offers to use the token of the GitHub CLI, `gh`, when you run it in a terminal.
Without any token, it warns before the release and skips the GitHub release.

```{seealso}
- {doc}`/reference/environment-variables`, for every variable and command repoplone uses.
- {doc}`release`, to run the release.
- [Trusted publishers on PyPI](https://docs.pypi.org/trusted-publishers/), and [trusted publishing on npm](https://docs.npmjs.com/trusted-publishers).
```
