-
Notifications
You must be signed in to change notification settings - Fork 6
439 lines (386 loc) · 15.5 KB
/
release-python.yml
File metadata and controls
439 lines (386 loc) · 15.5 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
name: Release Python SDK
on:
issue_comment:
types: [created]
workflow_dispatch:
inputs:
release_type:
description: 'Release type'
required: true
type: choice
options:
- patch
- minor
- major
prerelease:
description: 'Prerelease tag (leave empty for stable release)'
required: false
type: choice
options:
- ''
- beta
- alpha
- rc
ref:
description: 'Branch/ref to release from (default: main)'
required: false
type: string
default: 'main'
permissions:
contents: write
issues: write
pull-requests: write
id-token: write # Required for PyPI Trusted Publishers (OIDC)
jobs:
parse:
if: >
github.event_name == 'issue_comment' &&
(contains(github.event.comment.body, '!release') ||
contains(github.event.comment.body, '!Release') ||
contains(github.event.comment.body, '!RELEASE')) &&
github.event.comment.user.type != 'Bot'
runs-on: ubuntu-latest
outputs:
should_release: ${{ steps.parse.outputs.should_release }}
release_type: ${{ steps.parse.outputs.release_type }}
prerelease: ${{ steps.parse.outputs.prerelease }}
ref: ${{ steps.parse.outputs.ref }}
pr_branch: ${{ steps.parse.outputs.pr_branch }}
pr_title: ${{ steps.parse.outputs.pr_title }}
pr_url: ${{ steps.parse.outputs.pr_url }}
issue_number: ${{ steps.parse.outputs.issue_number }}
requested_by: ${{ steps.parse.outputs.requested_by }}
request_url: ${{ steps.parse.outputs.request_url }}
steps:
- name: Parse release command
id: parse
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const allowedPackages = ['python', 'py'];
const allowedBumps = ['patch', 'minor', 'major'];
const allowedPrereleaseTags = ['beta', 'alpha', 'rc'];
const body = context.payload.comment.body.trim();
const match = body.match(/!release\s+(\S+)\s+(\S+)(?:\s+(\S+))?/i);
if (!match) {
return;
}
const packageName = match[1].toLowerCase();
const releaseType = match[2].toLowerCase();
const prereleaseTag = match[3] ? match[3].toLowerCase() : '';
// Only handle python/py - let other packages be handled by release-npm.yml / release-go.yml
if (!allowedPackages.includes(packageName)) {
return;
}
const allowedAssociation = ['OWNER', 'MEMBER', 'COLLABORATOR'];
const association = context.payload.comment.author_association;
const actor = context.payload.comment.user.login;
const owner = context.repo.owner;
const repo = context.repo.repo;
const issueNumber = context.payload.issue.number;
const invalidBump = !allowedBumps.includes(releaseType);
const invalidPrerelease = prereleaseTag && !allowedPrereleaseTags.includes(prereleaseTag);
if (invalidBump || invalidPrerelease) {
const message = [
`⚠️ @${actor} I couldn't parse the release command.`,
'',
'Expected format: `!release <python|py> <patch|minor|major> [beta|alpha|rc]`',
'',
'Examples:',
'- `!release python patch` - stable release',
'- `!release py minor beta` - beta release'
];
await github.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: message.join('\n')
});
return;
}
if (!allowedAssociation.includes(association)) {
const message = [
`⛔ @${actor} you do not have permission to trigger releases.`,
'',
'Please contact the repository maintainers if this is unexpected.'
];
await github.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: message.join('\n')
});
return;
}
let ref = 'main';
let prBranch = '';
let prTitle = '';
let prUrl = '';
if (context.payload.issue.pull_request) {
const { data: pr } = await github.rest.pulls.get({
owner,
repo,
pull_number: issueNumber
});
if (!pr.head.repo || pr.head.repo.full_name !== `${owner}/${repo}`) {
const message = [
`⛔ @${actor} releases from forked branches are not supported.`,
'',
'Please push the branch to this repository or run the release manually in Actions.'
];
await github.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: message.join('\n')
});
return;
}
ref = pr.head.ref;
prBranch = pr.head.ref;
prTitle = pr.title;
prUrl = pr.html_url;
}
const releaseTypeLabel = prereleaseTag ? `${releaseType} (${prereleaseTag})` : releaseType;
const lines = [
`🚀 @${actor} release command accepted: \`python\` \`${releaseTypeLabel}\`.`,
'',
'The release workflow is queued; results will be posted here.'
];
if (prBranch) {
lines.splice(2, 0, `Target branch: \`${prBranch}\` (open PR). Version commits will be pushed to this branch.`);
}
if (prereleaseTag) {
lines.splice(2, 0, `📦 Prerelease tag: \`${prereleaseTag}\` (will publish to PyPI with pre-release version)`);
}
await github.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: lines.join('\n')
});
core.setOutput('should_release', 'true');
core.setOutput('release_type', releaseType);
core.setOutput('prerelease', prereleaseTag);
core.setOutput('ref', ref);
core.setOutput('pr_branch', prBranch);
core.setOutput('pr_title', prTitle);
core.setOutput('pr_url', prUrl);
core.setOutput('issue_number', issueNumber);
core.setOutput('requested_by', actor);
core.setOutput('request_url', context.payload.comment.html_url);
release:
needs: [parse]
if: |
always() &&
(
(needs.parse.result == 'success' && needs.parse.outputs.should_release == 'true') ||
(github.event_name == 'workflow_dispatch')
)
concurrency:
group: release-python-${{ github.event_name == 'workflow_dispatch' && inputs.ref || needs.parse.outputs.ref }}
cancel-in-progress: false
runs-on: ubuntu-latest
environment: pypi
env:
RELEASE_TYPE: ${{ github.event_name == 'workflow_dispatch' && inputs.release_type || needs.parse.outputs.release_type }}
PRERELEASE: ${{ github.event_name == 'workflow_dispatch' && inputs.prerelease || needs.parse.outputs.prerelease }}
SOURCE_REF: ${{ github.event_name == 'workflow_dispatch' && inputs.ref || needs.parse.outputs.ref }}
ISSUE_NUMBER: ${{ needs.parse.outputs.issue_number }}
REQUESTED_BY: ${{ github.event_name == 'workflow_dispatch' && github.actor || needs.parse.outputs.requested_by }}
REQUEST_URL: ${{ needs.parse.outputs.request_url }}
PR_TITLE: ${{ needs.parse.outputs.pr_title }}
PR_URL: ${{ needs.parse.outputs.pr_url }}
PR_BRANCH: ${{ needs.parse.outputs.pr_branch }}
steps:
- name: Validate context
run: |
set -e
if [ -z "${RELEASE_TYPE:-}" ]; then
echo "RELEASE_TYPE is not set"
exit 1
fi
case "$RELEASE_TYPE" in
patch|minor|major) ;;
*)
echo "Unsupported release type: $RELEASE_TYPE"
exit 1
;;
esac
if [ -z "${SOURCE_REF:-}" ]; then
echo "SOURCE_REF is not set"
exit 1
fi
- name: Checkout source
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ env.SOURCE_REF }}
- name: Configure git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install build tools
run: pip install build
- name: Bump version
id: bump
run: python scripts/bump-python-version.py "$RELEASE_TYPE" "$PRERELEASE"
- name: Export version metadata
run: |
echo "NEW_VERSION=${{ steps.bump.outputs.version }}" >> "$GITHUB_ENV"
echo "TAG_NAME=python-v${{ steps.bump.outputs.version }}" >> "$GITHUB_ENV"
- name: Run lint and format check
working-directory: python
run: |
pip install "ruff>=0.12,<1"
ruff format --check .
ruff check .
- name: Run tests
working-directory: python
run: |
pip install -e ".[dev]"
pytest -q -m "not e2e"
- name: Build package
working-directory: python
run: python -m build
- name: Inspect package contents
id: pack_info
working-directory: python
run: |
pip install twine
twine check dist/*
# Get package info
whl_file=$(ls dist/*.whl 2>/dev/null | head -1)
tar_file=$(ls dist/*.tar.gz 2>/dev/null | head -1)
if [ -n "$whl_file" ]; then
whl_size=$(du -h "$whl_file" | cut -f1)
echo "whl_size=${whl_size}" >> "$GITHUB_OUTPUT"
echo "whl_file=$(basename "$whl_file")" >> "$GITHUB_OUTPUT"
fi
if [ -n "$tar_file" ]; then
tar_size=$(du -h "$tar_file" | cut -f1)
echo "tar_size=${tar_size}" >> "$GITHUB_OUTPUT"
echo "tar_file=$(basename "$tar_file")" >> "$GITHUB_OUTPUT"
fi
- name: Commit version change
run: |
git add python/pyproject.toml
if git diff --cached --quiet; then
echo "No changes to commit"
exit 1
fi
git commit -m "chore(python): release v${NEW_VERSION}"
- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: python/dist/
- name: Create tag
run: git tag "$TAG_NAME"
- name: Push changes
id: push
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
if [ "$SOURCE_REF" = "main" ]; then
RELEASE_BRANCH="release/python-v${NEW_VERSION}"
git checkout -b "$RELEASE_BRANCH"
git push origin "$RELEASE_BRANCH"
git push origin "$TAG_NAME"
PR_URL=$(gh pr create \
--base main \
--head "$RELEASE_BRANCH" \
--title "chore(python): release v${NEW_VERSION}" \
--body "Automated release PR for python v${NEW_VERSION}. Updates pyproject.toml. The package has already been published to PyPI.")
echo "release_pr_url=$PR_URL" >> "$GITHUB_OUTPUT"
echo "used_release_branch=true" >> "$GITHUB_OUTPUT"
echo "release_branch=$RELEASE_BRANCH" >> "$GITHUB_OUTPUT"
else
git push origin HEAD:${SOURCE_REF}
git push origin "$TAG_NAME"
echo "used_release_branch=false" >> "$GITHUB_OUTPUT"
fi
- name: Create GitHub release
id: release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
{
echo "Release python v${NEW_VERSION}"
echo ""
echo "---"
echo ""
echo "**PyPI**: [phala-cloud@${NEW_VERSION}](https://pypi.org/project/phala-cloud/${NEW_VERSION}/)"
echo ""
echo '```bash'
echo "pip install phala-cloud==${NEW_VERSION}"
echo '```'
} > release_body.md
PRERELEASE_FLAG=""
if [ -n "$PRERELEASE" ]; then
PRERELEASE_FLAG="--prerelease"
fi
RELEASE_URL=$(gh release create "$TAG_NAME" \
--title "phala-cloud v${NEW_VERSION}" \
--notes-file release_body.md \
$PRERELEASE_FLAG \
python/dist/*)
echo "html_url=$RELEASE_URL" >> "$GITHUB_OUTPUT"
- name: Comment success
if: ${{ success() && env.ISSUE_NUMBER != '' }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
const usedReleaseBranch = '${{ steps.push.outputs.used_release_branch }}' === 'true';
const releasePrUrl = '${{ steps.push.outputs.release_pr_url }}';
const body = [
`🎉 Release completed: \`python\` v${process.env.NEW_VERSION}`,
'',
`- Branch: \`${process.env.SOURCE_REF}\``,
`- PyPI: [phala-cloud@${process.env.NEW_VERSION}](https://pypi.org/project/phala-cloud/${process.env.NEW_VERSION}/)`,
`- GitHub Release: [link](${{ steps.release.outputs.html_url }})`,
`- Workflow logs: ${runUrl}`,
];
if (usedReleaseBranch && releasePrUrl) {
body.push('');
body.push(`> ⚠️ **Action Required**: Please merge the [release PR](${releasePrUrl}) to update \`main\` branch with version changes.`);
}
body.push(
'',
'### 📦 Package Info',
`- Wheel: **${{ steps.pack_info.outputs.whl_file }}** (${{ steps.pack_info.outputs.whl_size }})`,
`- Source: **${{ steps.pack_info.outputs.tar_file }}** (${{ steps.pack_info.outputs.tar_size }})`,
);
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: Number(process.env.ISSUE_NUMBER),
body: body.join('\n')
});
- name: Comment failure
if: ${{ failure() && env.ISSUE_NUMBER != '' }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
const actor = process.env.REQUESTED_BY ? `@${process.env.REQUESTED_BY}` : 'Requester';
const version = process.env.NEW_VERSION || 'unknown version';
const body = [
`❌ ${actor} release failed: \`python\` ${version}`,
'',
`Branch: \`${process.env.SOURCE_REF}\``,
`Please review the workflow logs: ${runUrl}`
].join('\n');
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: Number(process.env.ISSUE_NUMBER),
body
});