Skip to content

Commit c1632f6

Browse files
CI: Add publishing to PyPI and TestPyPI with trusted publishers
* Use the OpenID Connect (OIDC) standard to publish to PyPI and TestPyPI using PyPI's "Trusted Publisher" implementation to publish without using API tokens stored as GitHub Actions secrets. Use an optional GitHub Actions environment to further restrict publishing to selected branches for additional security. - c.f. https://blog.pypi.org/posts/2023-04-20-introducing-trusted-publishers/ - c.f. https://docs.pypi.org/trusted-publishers/
1 parent 546fa3d commit c1632f6

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

.github/workflows/publish-package.yml

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
name: publish distributions
2+
on:
3+
push:
4+
branches:
5+
- main
6+
tags:
7+
- [0-9]+.[0-9]+
8+
- [0-9]+.[0-9]+.[0-9]+
9+
pull_request:
10+
branches:
11+
- main
12+
- release/v*
13+
release:
14+
types: [published]
15+
workflow_dispatch:
16+
inputs:
17+
publish:
18+
type: choice
19+
description: 'Publish to TestPyPI?'
20+
options:
21+
- false
22+
- true
23+
24+
concurrency:
25+
group: ${{ github.workflow }}-${{ github.ref }}
26+
cancel-in-progress: true
27+
28+
jobs:
29+
build:
30+
name: Build Python distribution
31+
runs-on: ubuntu-latest
32+
33+
steps:
34+
- uses: actions/checkout@v3
35+
with:
36+
fetch-depth: 0
37+
38+
- name: Set up Python
39+
uses: actions/setup-python@v4
40+
with:
41+
python-version: '3.x'
42+
43+
- name: Install python-build and twine
44+
run: |
45+
python -m pip install --upgrade pip setuptools
46+
python -m pip install build twine
47+
python -m pip list
48+
49+
- name: Build a wheel and a sdist
50+
run: |
51+
PYTHONWARNINGS=error,default::DeprecationWarning python -m build .
52+
53+
- name: Verify the distribution
54+
run: twine check --strict dist/*
55+
56+
- name: List contents of sdist
57+
run: python -m tarfile --list dist/array_api_compat-*.tar.gz
58+
59+
- name: List contents of wheel
60+
run: python -m zipfile --list dist/array_api_compat-*.whl
61+
62+
- name: Upload distribution artifact
63+
uses: actions/upload-artifact@v3
64+
with:
65+
name: dist-artifact
66+
path: dist
67+
68+
publish:
69+
name: Publish Python distribution to (Test)PyPI
70+
if: github.event_name != 'pull_request' && github.repository == 'data-apis/array-api-compat'
71+
needs: build
72+
runs-on: ubuntu-latest
73+
# Mandatory for publishing with a trusted publisher
74+
# c.f. https://docs.pypi.org/trusted-publishers/using-a-publisher/
75+
permissions:
76+
id-token: write
77+
# Restrict to the environment set for the trusted publisher
78+
environment:
79+
name: publish-package
80+
81+
steps:
82+
- name: Download distribution artifact
83+
uses: actions/download-artifact@v3
84+
with:
85+
name: dist-artifact
86+
path: dist
87+
88+
- name: List all files
89+
run: ls -lh dist
90+
91+
- name: Publish distribution 📦 to Test PyPI
92+
# Publish to TestPyPI on tag events of if manually triggered
93+
# Compare to 'true' string as booleans get turned into strings in the console
94+
if: >-
95+
(github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v'))
96+
|| (github.event_name == 'workflow_dispatch' && github.event.inputs.publish == 'true')
97+
uses: pypa/gh-action-pypi-publish@v1.8.8
98+
with:
99+
repository-url: https://test.pypi.org/legacy/
100+
print-hash: true
101+
102+
- name: Publish distribution 📦 to PyPI
103+
if: github.event_name == 'release' && github.event.action == 'published'
104+
uses: pypa/gh-action-pypi-publish@v1.8.8
105+
with:
106+
print-hash: true

0 commit comments

Comments
 (0)