Skip to content

Commit 214c341

Browse files
committed
bugfix(chore): publish to pypi
1 parent 8b1760e commit 214c341

1 file changed

Lines changed: 22 additions & 20 deletions

File tree

.github/workflows/publish-to-pypi.yml

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ on:
1111
required: false
1212
default: false
1313
type: boolean
14+
version:
15+
description: 'Version to publish (e.g., 0.1.0) - required for manual testing'
16+
required: true
17+
type: string
1418

1519
jobs:
1620
build-and-publish-pypi:
@@ -33,49 +37,48 @@ jobs:
3337
- name: Setup virtual environment
3438
run: uv venv .venv --python=3.10
3539

36-
- name: Extract version from tag
40+
- name: Extract version
3741
run: |
38-
echo "VERSION=$(echo $GITHUB_REF | sed -n 's/refs\/tags\/v//p')" >> $GITHUB_ENV
39-
echo "Releasing version: $VERSION"
42+
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
43+
VERSION="${{ inputs.version }}"
44+
else
45+
VERSION=$(echo $GITHUB_REF | sed -n 's/refs\/tags\/v//p')
46+
fi
47+
echo "VERSION=$VERSION" >> $GITHUB_ENV
48+
49+
if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+.*$ ]]; then
50+
echo "Invalid version format: $VERSION"
51+
exit 1
52+
fi
4053
4154
- name: Update version in pyproject.toml
42-
run: |
43-
sed -i "s/version = \".*\"/version = \"$VERSION\"/" pyproject.toml
44-
echo "Updated version to $VERSION"
45-
grep "version =" pyproject.toml
55+
run: sed -i "s/version = \".*\"/version = \"$VERSION\"/" pyproject.toml
4656

4757
- name: Build package
4858
run: uv build
4959

50-
# 🎯 测试发布到 TestPyPI(手动触发或测试时)
5160
- name: Publish to TestPyPI
52-
if: github.event_name == 'workflow_dispatch' && inputs.test_only == true
61+
if: github.event_name == 'workflow_dispatch' && inputs.test_only == 'true'
5362
env:
5463
UV_PUBLISH_TOKEN: ${{ secrets.TEST_PYPI_API_KEY }}
55-
run: |
56-
echo "🧪 Publishing to TestPyPI for testing..."
57-
uv publish --publish-url https://test.pypi.org/legacy/ --verbose
64+
run: uv publish --publish-url https://test.pypi.org/legacy/
5865

59-
# 🚀 正式发布到 PyPI(标签触发)
6066
- name: Publish to PyPI
6167
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
6268
env:
6369
UV_PUBLISH_TOKEN: ${{ secrets.PYPI_API_KEY }}
64-
run: |
65-
echo "🚀 Publishing to PyPI..."
66-
uv publish --publish-url https://upload.pypi.org/legacy/ --verbose
70+
run: uv publish --publish-url https://upload.pypi.org/legacy/
6771

68-
# 📦 备用方案:使用 twine
6972
- name: Publish with twine (fallback)
70-
if: failure() # 如果上面的步骤失败
73+
if: failure()
7174
env:
7275
TWINE_USERNAME: __token__
7376
TWINE_PASSWORD: ${{ github.event_name == 'push' && secrets.PYPI_API_KEY || secrets.TEST_PYPI_API_KEY }}
7477
TWINE_REPOSITORY_URL: ${{ github.event_name == 'push' && 'https://upload.pypi.org/legacy/' || 'https://test.pypi.org/legacy/' }}
7578
run: |
7679
source .venv/bin/activate
7780
uv pip install twine
78-
twine upload dist/* --verbose
81+
twine upload dist/*
7982
8083
- name: Upload built artifacts
8184
uses: actions/upload-artifact@v4
@@ -87,7 +90,6 @@ jobs:
8790
name: Create GitHub Release
8891
runs-on: ubuntu-latest
8992
needs: build-and-publish-pypi
90-
# 只有正式发布时才创建 Release
9193
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
9294
permissions:
9395
contents: write

0 commit comments

Comments
 (0)