Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PyPI's "Trusted Publisher" integration in publish.yaml #10

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ on:
inputs:
python-version:
description: |-
Python version for the workflow, passed to actions/setup-python.
Python version for the workflow, passed to actions/setup-python.
type: string
required: false
default: "3.x"
secrets:
PYPI_TOKEN:
required: true
TESTPYPI_TOKEN:
required: true
environment:
description: |-
Environment name to use in the workflow.
type: string
required: false
default: "publish"

jobs:
publish:
environment: ${{ inputs.environment }}

runs-on: ubuntu-latest

steps:
Expand Down Expand Up @@ -47,14 +50,9 @@ jobs:
uses: pypa/gh-action-pypi-publish@release/v1
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
with:
user: __token__
password: ${{ secrets.TESTPYPI_TOKEN }}
repository-url: https://test.pypi.org/legacy/

# Performed on a release event
- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
if: github.event_name == 'release'
with:
user: __token__
password: ${{ secrets.PYPI_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Editors
.vscode
28 changes: 27 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Lint and check Python code with black, flake8, isort, and mypy.
Build Python package distributions; check with ``twine``, and publish to (Test)PyPI.

- `Source </~https://github.com/iiasa/actions/blob/main/.github/workflows/publish.yaml>`__
- Usage example:
- Usage example (``main`` branch):

.. code-block:: yaml

Expand All @@ -81,3 +81,29 @@ Build Python package distributions; check with ``twine``, and publish to (Test)P
secrets:
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
TESTPYPI_TOKEN: ${{ secrets.TESTPYPI_TOKEN }}

This usage is **deprecated**: workflows using publish.yaml should switch to the ``pypi-trusted`` approach, below.
This version will be disabled and cease to work once all known user workflows have migrated.

- The ``pypi-trusted`` branch contains an updated version of this workflow that uses PyPI's new (2023) `Trusted Publisher <https://docs.pypi.org/trusted-publishers/>`__ integration.
This obviates the use of secrets, above.
To use this branch, specify on the ``uses:`` line.

.. code-block:: yaml

name: Build package / publish

on:
pull_request:
branches: [ main ] # Package is built and checked
push:
branches: [ main ] # Package is built and checked
tags: [ "v*" ] # Package is pushed to TestPyPI
release:
types: [ published ] # Package is also pushed to PyPI

jobs:
publish:
uses: iiasa/actions/.github/workflows/publish.yaml@pypi-trusted
permissions:
id-token: write