-
Notifications
You must be signed in to change notification settings - Fork 73
101 lines (90 loc) · 3.35 KB
/
python-release.yml
File metadata and controls
101 lines (90 loc) · 3.35 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
name: Python SDK Release
on:
workflow_call:
inputs:
working-directory:
description: Path where build commands should run
required: true
type: string
tag:
description: Git tag for this release (e.g., v0.1.0)
required: true
type: string
version:
description: Package version without the leading v (e.g., 0.1.0)
required: true
type: string
python-version:
description: Python version used to build the package
required: false
type: string
default: "3.11"
secrets:
PYPI_API_TOKEN:
required: true
jobs:
publish:
name: Publish Python SDK
runs-on: ubuntu-latest
environment: release
if: inputs.version != ''
defaults:
run:
shell: bash
working-directory: ${{ inputs.working-directory }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Show release context
run: |
echo "Tag: ${{ inputs.tag }}"
echo "Version: ${{ inputs.version }}"
echo "Working directory: ${{ inputs.working-directory }}"
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python-version }}
- name: Upgrade pip and install build dependencies
run: |
python -m pip install --upgrade pip
python -m pip install build twine
- name: Verify package version matches tag
id: verify-python-version
run: |
TAG_VERSION="${{ inputs.version }}"
PACKAGE_VERSION=$(python -c "import tomllib; data = tomllib.load(open('pyproject.toml', 'rb')); print(data['project']['version'])")
echo "Detected package version: $PACKAGE_VERSION"
if [ "$PACKAGE_VERSION" != "$TAG_VERSION" ]; then
echo "❌ Version mismatch! Tag: $TAG_VERSION, Package: $PACKAGE_VERSION"
exit 1
fi
echo "✅ Python version matches tag"
- name: Build package
run: python -m build
- name: Check if package version already exists on PyPI
id: check-python
run: |
VERSION="${{ inputs.version }}"
PACKAGE_NAME=$(python -c "import tomllib; data = tomllib.load(open('pyproject.toml', 'rb')); print(data['project']['name'])")
echo "Package name: $PACKAGE_NAME"
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "https://pypi.org/pypi/$PACKAGE_NAME/$VERSION/json/")
if [ "$HTTP_CODE" = "200" ]; then
echo "⚠️ $PACKAGE_NAME v$VERSION already exists on PyPI"
echo "should_publish=false" >> "$GITHUB_OUTPUT"
else
echo "✅ $PACKAGE_NAME v$VERSION not found on PyPI"
echo "should_publish=true" >> "$GITHUB_OUTPUT"
fi
echo "package_name=$PACKAGE_NAME" >> "$GITHUB_OUTPUT"
- name: Publish package to PyPI
if: steps.check-python.outputs.should_publish == 'true'
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
print-hash: true
attestations: false
- name: Skip publish (already exists)
if: steps.check-python.outputs.should_publish != 'true'
run: echo "Skipping PyPI publish because version already exists."