Skip to content

Commit f1c331b

Browse files
CI: Add publishing to PyPI and TestPyPI with trusted publishers
1 parent ea6a9d6 commit f1c331b

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

.github/workflows/publish-package.yml

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

0 commit comments

Comments
 (0)