-
Notifications
You must be signed in to change notification settings - Fork 9
202 lines (176 loc) · 6.48 KB
/
python-publish.yaml
File metadata and controls
202 lines (176 loc) · 6.48 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
name: Release and Publish
on:
pull_request:
types: [closed]
branches: [main]
concurrency:
group: release-${{ github.repository }}
cancel-in-progress: false
jobs:
prepare:
if: >
github.event.pull_request.merged == true &&
startsWith(github.event.pull_request.head.ref, 'release/v')
runs-on: ubuntu-latest
outputs:
version: ${{ steps.version.outputs.VERSION }}
prerelease: ${{ steps.version.outputs.PRERELEASE }}
changelog: ${{ steps.changelog.outputs.CONTENT }}
steps:
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
with:
fetch-depth: 0
- name: Extract version from branch name
id: version
env:
HEAD_REF: ${{ github.event.pull_request.head.ref }}
run: |
VERSION=${HEAD_REF#release/v}
echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
if echo "$VERSION" | grep -qE "(rc|alpha|beta)[0-9]+$"; then
echo "PRERELEASE=true" >> $GITHUB_OUTPUT
else
echo "PRERELEASE=false" >> $GITHUB_OUTPUT
fi
- name: Validate version matches pyproject.toml
env:
VERSION: ${{ steps.version.outputs.VERSION }}
run: |
PYPROJECT_VERSION=$(grep '^version = ' sdk/pyproject.toml | sed 's/version = "\(.*\)"/\1/')
if [ "$VERSION" != "$PYPROJECT_VERSION" ]; then
echo "::error::Version mismatch: branch=$VERSION, pyproject.toml=$PYPROJECT_VERSION"
exit 1
fi
- name: Extract changelog
id: changelog
env:
VERSION: ${{ steps.version.outputs.VERSION }}
PRERELEASE: ${{ steps.version.outputs.PRERELEASE }}
run: |
if [ "$PRERELEASE" = "true" ]; then
echo "CONTENT=Pre-release version $VERSION" >> $GITHUB_OUTPUT
else
CONTENT=$(sed -n "/## \[$VERSION\]/,/## \[/p" sdk/CHANGELOG.md | tail -n +2 | head -n -1)
DELIMITER=$(openssl rand -hex 8)
echo "CONTENT<<${DELIMITER}" >> $GITHUB_OUTPUT
echo "$CONTENT" >> $GITHUB_OUTPUT
echo "${DELIMITER}" >> $GITHUB_OUTPUT
fi
build-and-test:
needs: prepare
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
- name: Setup Python
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5
with:
python-version: '3.11'
- name: Install build tools
run: pip install build
- name: Build distributions
run: python -m build sdk/
- name: Upload distributions
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
with:
name: release-dists
path: sdk/dist/
retention-days: 7
create-tag:
needs: [prepare, build-and-test]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
- name: Create and push tag
env:
VERSION: ${{ needs.prepare.outputs.version }}
run: |
TAG="v$VERSION"
if git rev-parse "$TAG" >/dev/null 2>&1; then
echo "Tag $TAG already exists, skipping."
exit 0
fi
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag -a "$TAG" -m "Release $TAG"
git push origin "$TAG"
pypi-publish:
needs: [prepare, build-and-test, create-tag]
runs-on: ubuntu-latest
permissions:
id-token: write
environment:
name: pypi
url: https://pypi.org/p/eggai
steps:
- name: Check if already published
id: check
env:
VERSION: ${{ needs.prepare.outputs.version }}
run: |
STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://pypi.org/pypi/eggai/$VERSION/json)
echo "exists=$([ "$STATUS" = "200" ] && echo true || echo false)" >> $GITHUB_OUTPUT
- name: Download distributions
if: steps.check.outputs.exists == 'false'
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
with:
name: release-dists
path: dist/
- name: Publish to PyPI
if: steps.check.outputs.exists == 'false'
uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # release/v1
github-release:
needs: [prepare, build-and-test, create-tag]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
- name: Download distributions
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
with:
name: release-dists
path: dist/
- name: Create GitHub Release
env:
GH_TOKEN: ${{ github.token }}
VERSION: ${{ needs.prepare.outputs.version }}
PRERELEASE: ${{ needs.prepare.outputs.prerelease }}
CHANGELOG_BODY: ${{ needs.prepare.outputs.changelog }}
run: |
if gh release view "v$VERSION" > /dev/null 2>&1; then
echo "Release v$VERSION already exists, uploading artifacts."
gh release upload "v$VERSION" dist/* --clobber
else
PRERELEASE_FLAG=""
if [ "$PRERELEASE" = "true" ]; then
PRERELEASE_FLAG="--prerelease"
fi
printf '%s\n' "## What's Changed" "" "$CHANGELOG_BODY" "" "## Installation" "" '```bash' "pip install eggai==$VERSION" '```' > /tmp/notes.md
gh release create "v$VERSION" dist/* \
--title "v$VERSION" \
--notes-file /tmp/notes.md \
$PRERELEASE_FLAG
fi
notify-failure:
needs: [prepare, build-and-test, create-tag, pypi-publish, github-release]
if: failure()
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- name: Create failure issue
env:
GH_TOKEN: ${{ github.token }}
VERSION: ${{ needs.prepare.outputs.version }}
run: |
gh issue create \
--repo "$GITHUB_REPOSITORY" \
--title "Release v$VERSION failed" \
--body "Workflow run: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" \
--label "bug"