-
Notifications
You must be signed in to change notification settings - Fork 0
115 lines (99 loc) · 3.33 KB
/
publish.yml
File metadata and controls
115 lines (99 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
name: Publish
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
check-version:
runs-on: ubuntu-latest
outputs:
should_publish: ${{ steps.check.outputs.should_publish }}
version: ${{ steps.check.outputs.version }}
is_official: ${{ steps.check.outputs.is_official }}
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Install uv
uses: astral-sh/setup-uv@v7
# Extract version from pyproject.toml, check if tag exists, and determine version type
- name: Check version
id: check
run: |
VERSION=$(uv run python -c "import tomllib; print(tomllib.load(open('pyproject.toml', 'rb'))['project']['version'])")
TAG="v${VERSION}"
echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
# Check if version matches x.y.z (official) or has pre-release suffix
if echo "$VERSION" | grep -qP '^\d+\.\d+\.\d+(\.post\d+)?$'; then
echo "is_official=true" >> "$GITHUB_OUTPUT"
echo "Version $VERSION is an official release"
else
echo "is_official=false" >> "$GITHUB_OUTPUT"
echo "Version $VERSION is a pre-release"
fi
if git tag -l "$TAG" | grep -q "$TAG"; then
echo "Tag $TAG already exists, skipping publish"
echo "should_publish=false" >> "$GITHUB_OUTPUT"
else
echo "Tag $TAG does not exist, will publish"
echo "should_publish=true" >> "$GITHUB_OUTPUT"
fi
# Pre-release versions (e.g. 1.0.0-rc.1, 1.0.0-beta.1) are published from PRs
publish-prerelease:
needs: check-version
if: >-
needs.check-version.outputs.should_publish == 'true'
&& needs.check-version.outputs.is_official == 'false'
&& github.event_name == 'pull_request'
runs-on: ubuntu-latest
environment:
name: pypi
permissions:
id-token: write
contents: write
steps:
- uses: actions/checkout@v6
- name: Install uv
uses: astral-sh/setup-uv@v7
- name: Build
run: uv build
- name: Publish to PyPI
run: uv publish
- name: Create git tag and GitHub Release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
TAG="v${{ needs.check-version.outputs.version }}"
git tag "$TAG"
git push origin "$TAG"
gh release create "$TAG" dist/* --generate-notes --prerelease
# Official versions (x.y.z) are published only from pushes to main
publish:
needs: check-version
if: >-
needs.check-version.outputs.should_publish == 'true'
&& needs.check-version.outputs.is_official == 'true'
&& github.event_name == 'push'
runs-on: ubuntu-latest
environment:
name: pypi
permissions:
id-token: write
contents: write
steps:
- uses: actions/checkout@v6
- name: Install uv
uses: astral-sh/setup-uv@v7
- name: Build
run: uv build
- name: Publish to PyPI
run: uv publish
- name: Create git tag and GitHub Release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
TAG="v${{ needs.check-version.outputs.version }}"
git tag "$TAG"
git push origin "$TAG"
gh release create "$TAG" dist/* --generate-notes