Skip to content

Commit bce4702

Browse files
authored
Merge pull request #3 from nathan-weinberg/pypi
Copy pypi release automation from `instructlab/sdg` repo
2 parents f4d36e8 + 0c965d0 commit bce4702

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

.github/workflows/pypi.yaml

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
name: Build, test, and upload PyPI package
4+
5+
on:
6+
push:
7+
branches:
8+
- "main"
9+
- "release-**"
10+
tags:
11+
- "v*"
12+
pull_request:
13+
branches:
14+
- "main"
15+
- "release-**"
16+
release:
17+
types:
18+
- published
19+
20+
env:
21+
LC_ALL: en_US.UTF-8
22+
23+
defaults:
24+
run:
25+
shell: bash
26+
27+
permissions:
28+
contents: read
29+
30+
jobs:
31+
# Create and verify release artifacts
32+
# - build source dist (tar ball) and wheel
33+
# - validate artifacts with various tools
34+
# - upload artifacts to GHA
35+
build-package:
36+
name: Build and check packages
37+
runs-on: ubuntu-latest
38+
steps:
39+
- name: "Harden Runner"
40+
uses: step-security/harden-runner@17d0e2bd7d51742c71671bd19fa12bdc9d40a3d6 # v2.8.1
41+
with:
42+
egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs
43+
44+
- name: "Checkout"
45+
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
46+
with:
47+
# for setuptools-scm
48+
fetch-depth: 0
49+
50+
- name: "Build and Inspect"
51+
uses: hynek/build-and-inspect-python-package@b4fc3f6ba2b3da04f09659be99e2a29fb6146a61 # v2.6.0
52+
53+
# push to Test PyPI on
54+
# - a new GitHub release is published
55+
# - a PR is merged into main branch
56+
publish-test-pypi:
57+
name: Publish packages to test.pypi.org
58+
# environment: publish-test-pypi
59+
if: ${{ (github.repository_owner == 'instructlab') && ((github.event.action == 'published') || ((github.event_name == 'push') && (github.ref == 'refs/heads/main'))) }}
60+
permissions:
61+
contents: read
62+
# see https://docs.pypi.org/trusted-publishers/
63+
id-token: write
64+
runs-on: ubuntu-latest
65+
needs: build-package
66+
67+
steps:
68+
- name: "Harden Runner"
69+
uses: step-security/harden-runner@17d0e2bd7d51742c71671bd19fa12bdc9d40a3d6 # v2.8.1
70+
with:
71+
egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs
72+
73+
- name: "Download build artifacts"
74+
uses: actions/download-artifact@65a9edc5881444af0b9093a5e628f2fe47ea3b2e # v4.1.7
75+
with:
76+
name: Packages
77+
path: dist
78+
79+
- name: "Upload to Test PyPI"
80+
uses: pypa/gh-action-pypi-publish@81e9d935c883d0b210363ab89cf05f3894778450 # v1.8.14
81+
with:
82+
repository-url: https://test.pypi.org/legacy/
83+
84+
# push to Production PyPI on
85+
# - a new GitHub release is published
86+
publish-pypi:
87+
name: Publish release to pypi.org
88+
# environment: publish-pypi
89+
if: ${{ (github.repository_owner == 'instructlab') && (github.event.action == 'published') }}
90+
permissions:
91+
# see https://docs.pypi.org/trusted-publishers/
92+
id-token: write
93+
# allow gh release upload
94+
contents: write
95+
96+
runs-on: ubuntu-latest
97+
needs: build-package
98+
99+
steps:
100+
- name: "Harden Runner"
101+
uses: step-security/harden-runner@f086349bfa2bd1361f7909c78558e816508cdc10 # v2.8.0
102+
with:
103+
egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs
104+
105+
- name: "Download build artifacts"
106+
uses: actions/download-artifact@65a9edc5881444af0b9093a5e628f2fe47ea3b2e # v4.1.7
107+
with:
108+
name: Packages
109+
path: dist
110+
111+
- name: "Sigstore sign package"
112+
uses: sigstore/gh-action-sigstore-python@61f6a500bbfdd9a2a339cf033e5421951fbc1cd2 # v2.1.1
113+
with:
114+
inputs: |
115+
./dist/*.tar.gz
116+
./dist/*.whl
117+
118+
- name: "Upload artifacts and signatures to GitHub release"
119+
run: |
120+
gh release upload '${{ github.ref_name }}' dist/* --repo '${{ github.repository }}'
121+
env:
122+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
123+
124+
# PyPI does not accept .sigstore artifacts and
125+
# gh-action-pypi-publish has no option to ignore them.
126+
- name: "Remove sigstore signatures before uploading to PyPI"
127+
run: |
128+
rm ./dist/*.sigstore
129+
130+
- name: "Upload to PyPI"
131+
uses: pypa/gh-action-pypi-publish@81e9d935c883d0b210363ab89cf05f3894778450 # v1.8.14

0 commit comments

Comments
 (0)