Repository navigation
357 lines (296 loc) · 13.2 KB
/
Copy pathsdk-publish.yml
File metadata and controls
357 lines (296 loc) · 13.2 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
---
name: Publish to PyPI
# Trigger on pushes to main or via manual dispatch. Publishing is guarded by
# the PyPI version check below, so pushes that don't bump __version__ no-op.
on:
push:
branches:
- main
workflow_dispatch:
inputs:
branch:
description: 'Branch to publish from'
required: false
default: 'main'
concurrency:
# Serialize publishes so a push + dispatch (or two rapid pushes) can't both
# pass the PyPI existence check and race on `twine upload`.
group: publish-pypi-${{ github.ref }}
cancel-in-progress: false
permissions:
contents: write # For creating GitHub releases
id-token: write # For trusted publishing to PyPI
env:
PYTHON_VERSION: "3.11"
jobs:
publish:
name: 📦 Build and Publish to PyPI
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
ref: ${{ github.event.inputs.branch || github.ref }}
fetch-depth: 0 # Full history for proper release notes
- name: Set up Python
uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install build dependencies
run: |
python -m pip install --upgrade pip
pip install build twine hatchling jq
# === VERSION VALIDATION ===
- name: Extract version from __init__.py
id: get_version
run: |
# Extract version from source using sed (avoids import issues)
version=$(sed -n 's/^__version__[[:space:]]*=[[:space:]]*"\([^"]*\)".*/\1/p' src/honeyhive/__init__.py)
echo "version=$version" >> $GITHUB_OUTPUT
echo "📦 Detected version: $version"
# Validate version format
if ! echo "$version" | grep -E '^[0-9]+\.[0-9]+\.[0-9]+(-rc[0-9]+|rc[0-9]+|alpha[0-9]+|beta[0-9]+)?$'; then
echo "❌ Invalid version format: $version"
echo "Expected format: X.Y.Z or X.Y.Z-rc# or X.Y.Zrc#"
exit 1
fi
echo "✅ Version format is valid"
- name: Check if version already published to PyPI
id: check_pypi
run: |
VERSION="${{ steps.get_version.outputs.version }}"
echo "🔍 Checking if version $VERSION exists on PyPI..."
# Query PyPI API for package versions
response=$(curl -s https://pypi.org/pypi/honeyhive/json || echo '{}')
# Check if version exists in releases
if echo "$response" | python -c \
"import sys, json; \
data = json.load(sys.stdin); \
sys.exit(0 if '$VERSION' in data.get('releases', {}) else 1)"; then
echo "exists=true" >> $GITHUB_OUTPUT
echo "✅ Version $VERSION already published to PyPI"
echo "SKIP_REASON=Version $VERSION already exists on PyPI" >> $GITHUB_OUTPUT
else
echo "exists=false" >> $GITHUB_OUTPUT
echo "🆕 Version $VERSION is new - will publish to PyPI"
fi
- name: Skip publishing (version already exists)
if: steps.check_pypi.outputs.exists == 'true'
run: |
echo "## ✅ Version Already Published" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Version:** \`${{ steps.get_version.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "This version is already available on PyPI." >> $GITHUB_STEP_SUMMARY
echo "No action needed." >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
VERSION="${{ steps.get_version.outputs.version }}"
echo "🔗 [View on PyPI](https://pypi.org/project/honeyhive/$VERSION/)" \
>> $GITHUB_STEP_SUMMARY
echo "✅ Workflow complete - version already published"
exit 0
# === BUILD AND PUBLISH (only if version is new) ===
- name: Verify CHANGELOG updated
if: steps.check_pypi.outputs.exists == 'false'
run: |
VERSION="${{ steps.get_version.outputs.version }}"
if ! grep -q "$VERSION" CHANGELOG.md; then
echo "⚠️ Warning: Version $VERSION not found in CHANGELOG.md"
echo "Please update CHANGELOG.md before releasing"
echo ""
echo "This is a warning only - publishing will continue"
else
echo "✅ CHANGELOG.md contains entry for version $VERSION"
fi
- name: Build distribution packages
if: steps.check_pypi.outputs.exists == 'false'
run: |
echo "📦 Building source distribution and wheel..."
python -m build
echo "📋 Package contents:"
ls -lh dist/
- name: Verify package integrity
if: steps.check_pypi.outputs.exists == 'false'
run: |
echo "🔍 Checking package integrity..."
python -m twine check dist/*
echo "✅ Package integrity verified"
- name: Test package installation
if: steps.check_pypi.outputs.exists == 'false'
run: |
echo "🧪 Testing package installation..."
# Create clean test environment
python -m venv test-install
source test-install/bin/activate
# Install the wheel
pip install dist/*.whl
# Test imports
python -c "
import honeyhive
from honeyhive import HoneyHive, HoneyHiveTracer
from honeyhive import trace, evaluate
print(f'✅ Package installation successful')
print(f'HoneyHive version: {honeyhive.__version__}')
# Verify version matches
expected_version = '${{ steps.get_version.outputs.version }}'
if honeyhive.__version__ != expected_version:
print(f'❌ Version mismatch: {honeyhive.__version__} != {expected_version}')
exit(1)
print(f'✅ Version verified: {honeyhive.__version__}')
"
deactivate
echo "✅ Installation test passed"
- name: Publish to PyPI
if: steps.check_pypi.outputs.exists == 'false'
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: |
echo "🚀 Publishing version ${{ steps.get_version.outputs.version }} to PyPI..."
python -m twine upload dist/*
echo "✅ Published to PyPI successfully!"
- name: Verify publication
if: steps.check_pypi.outputs.exists == 'false'
run: |
VERSION="${{ steps.get_version.outputs.version }}"
echo "⏳ Waiting 30 seconds for PyPI to update..."
sleep 30
echo "🔍 Verifying package is available on PyPI..."
max_attempts=5
attempt=1
while [ $attempt -le $max_attempts ]; do
if pip index versions honeyhive | grep -q "$VERSION"; then
echo "✅ Package verified on PyPI!"
echo "🔗 https://pypi.org/project/honeyhive/$VERSION/"
exit 0
fi
echo "Attempt $attempt/$max_attempts: Not yet available, waiting..."
sleep 10
attempt=$((attempt + 1))
done
echo "⚠️ Could not verify package on PyPI within timeout"
echo "This may be a PyPI indexing delay - check manually"
echo "🔗 https://pypi.org/project/honeyhive/$VERSION/"
# === GITHUB RELEASE ===
- name: Extract changelog for version
if: steps.check_pypi.outputs.exists == 'false'
id: changelog
run: |
VERSION="${{ steps.get_version.outputs.version }}"
echo "📝 Extracting changelog for version $VERSION..."
# Extract changelog section for this version
# Matches from "## [VERSION]" until the next "## [" header
changelog_content=$(awk -v ver="$VERSION" '
BEGIN { found=0; content="" }
/^## \[/ {
if (found) exit
if (index($0, ver) > 0) found=1
next
}
found { content = content $0 "\n" }
END { print content }
' CHANGELOG.md)
# If no changelog found, use a default message
if [ -z "$changelog_content" ]; then
changelog_content="No changelog entry found for this version."
fi
# Write to file for multiline output
echo "$changelog_content" > /tmp/changelog_content.txt
echo "changelog_file=/tmp/changelog_content.txt" >> $GITHUB_OUTPUT
echo "✅ Changelog extracted"
- name: Generate release body
if: steps.check_pypi.outputs.exists == 'false'
id: release_body
run: |
VERSION="${{ steps.get_version.outputs.version }}"
# Use Python to generate the release body (handles multiline content safely)
python3 << 'PYTHON_EOF'
import os
version = os.environ.get('VERSION', '${{ steps.get_version.outputs.version }}')
# Read changelog content
try:
with open('/tmp/changelog_content.txt', 'r') as f:
changelog_content = f.read().strip()
except FileNotFoundError:
changelog_content = "No changelog entry found for this version."
# Generate release body with changelog at top
release_body = f"""# HoneyHive Python SDK v{version}
## What's Changed
{changelog_content}
## Installation
```bash
pip install honeyhive=={version}
```
## Links
- [PyPI Package](https://pypi.org/project/honeyhive/{version}/)
- [Documentation](https://honeyhiveai.github.io/python-sdk/)
- [Full Changelog](https://github.com/honeyhiveai/python-sdk/blob/main/CHANGELOG.md)
"""
# Remove leading whitespace from each line (heredoc indentation)
import textwrap
release_body = textwrap.dedent(release_body)
with open('/tmp/release_body.md', 'w') as f:
f.write(release_body)
print("Release body generated successfully")
PYTHON_EOF
echo "body_file=/tmp/release_body.md" >> $GITHUB_OUTPUT
echo "✅ Release body generated"
# The vX.Y.Z tag is created upstream when the release is synced to this
# repo, so this step creates a Release against that existing tag rather
# than cutting a new one. Uses the gh CLI directly instead of a
# third-party action. `--verify-tag` makes the step fail loudly if the
# tag is missing (e.g. a manual dispatch without a prior sync) rather
# than silently cutting one at the default-branch HEAD, which could be
# the wrong commit when dispatched from a non-default branch.
- name: Create GitHub Release
if: steps.check_pypi.outputs.exists == 'false'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VERSION: ${{ steps.get_version.outputs.version }}
run: |
set -euo pipefail
TAG="v${VERSION}"
if [[ "$VERSION" == *rc* || "$VERSION" == *alpha* || "$VERSION" == *beta* ]]; then
PRERELEASE_FLAG="--prerelease"
else
PRERELEASE_FLAG=""
fi
if gh release view "$TAG" >/dev/null 2>&1; then
echo "::notice::Release $TAG already exists — skipping creation"
else
# shellcheck disable=SC2086
gh release create "$TAG" \
--verify-tag \
--title "$TAG" \
--notes-file /tmp/release_body.md \
$PRERELEASE_FLAG
fi
# === SUMMARY ===
- name: Generate release summary
if: steps.check_pypi.outputs.exists == 'false'
run: |
echo "# 🚀 Release Published Successfully" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Version:** \`${{ steps.get_version.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY
echo "**Published:** $(date -u)" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
VERSION="${{ steps.get_version.outputs.version }}"
echo "## 📦 Installation" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY
echo "pip install honeyhive==$VERSION" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "## 🔗 Links" >> $GITHUB_STEP_SUMMARY
echo "- [PyPI Package](https://pypi.org/project/honeyhive/$VERSION/)" \
>> $GITHUB_STEP_SUMMARY
echo "- [GitHub Release]\
(https://github.com/honeyhiveai/python-sdk/releases/tag/v$VERSION)" \
>> $GITHUB_STEP_SUMMARY
echo "- [Documentation](https://honeyhiveai.github.io/python-sdk/)" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "## ✅ Verification" >> $GITHUB_STEP_SUMMARY
echo "- ✅ Package built successfully" >> $GITHUB_STEP_SUMMARY
echo "- ✅ Package integrity verified" >> $GITHUB_STEP_SUMMARY
echo "- ✅ Installation test passed" >> $GITHUB_STEP_SUMMARY
echo "- ✅ Published to PyPI" >> $GITHUB_STEP_SUMMARY
echo "- ✅ GitHub release created" >> $GITHUB_STEP_SUMMARY