Skip to content

Commit 5b073a1

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 5b073a1

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

.github/workflows/publish-package.yml

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

0 commit comments

Comments
 (0)