Skip to content

Commit cce7c04

Browse files
committed
Auto-publish on version change (no PAT needed)
1 parent ed258d2 commit cce7c04

File tree

1 file changed

+57
-43
lines changed

1 file changed

+57
-43
lines changed

.github/workflows/publish.yml

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,63 @@
11
# Publish workflow for xdk-python
2-
# Triggered by the generator repo via repository_dispatch
2+
# Triggers automatically when code is pushed to main (by generator)
33
# Also supports manual triggering for hotfixes
44

55
name: Publish to PyPI
66

77
on:
8-
repository_dispatch:
9-
types: [release]
8+
push:
9+
branches: [main]
10+
paths:
11+
- 'pyproject.toml' # Only publish when version changes
1012

11-
workflow_dispatch:
12-
inputs:
13-
version:
14-
description: 'Version to publish (must match pyproject.toml)'
15-
required: true
16-
type: string
13+
workflow_dispatch: # Manual trigger for hotfixes
1714

1815
jobs:
16+
check-version:
17+
name: Check if version changed
18+
runs-on: ubuntu-latest
19+
outputs:
20+
should_publish: ${{ steps.check.outputs.should_publish }}
21+
version: ${{ steps.check.outputs.version }}
22+
steps:
23+
- uses: actions/checkout@v4
24+
with:
25+
fetch-depth: 2
26+
27+
- name: Check version change
28+
id: check
29+
run: |
30+
VERSION=$(grep '^version = ' pyproject.toml | sed 's/version = "\(.*\)"/\1/')
31+
echo "version=$VERSION" >> $GITHUB_OUTPUT
32+
33+
# Check if this is a manual trigger
34+
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
35+
echo "should_publish=true" >> $GITHUB_OUTPUT
36+
echo "Manual trigger - will publish v$VERSION"
37+
exit 0
38+
fi
39+
40+
# Check if version changed from previous commit
41+
git show HEAD~1:pyproject.toml > /tmp/old_pyproject.toml 2>/dev/null || {
42+
echo "should_publish=true" >> $GITHUB_OUTPUT
43+
echo "First commit with pyproject.toml - will publish v$VERSION"
44+
exit 0
45+
}
46+
47+
OLD_VERSION=$(grep '^version = ' /tmp/old_pyproject.toml | sed 's/version = "\(.*\)"/\1/' || echo "")
48+
49+
if [ "$VERSION" != "$OLD_VERSION" ]; then
50+
echo "should_publish=true" >> $GITHUB_OUTPUT
51+
echo "Version changed: $OLD_VERSION → $VERSION"
52+
else
53+
echo "should_publish=false" >> $GITHUB_OUTPUT
54+
echo "Version unchanged ($VERSION) - skipping publish"
55+
fi
56+
1957
publish:
2058
name: Build and Publish
59+
needs: check-version
60+
if: needs.check-version.outputs.should_publish == 'true'
2161
runs-on: ubuntu-latest
2262
permissions:
2363
contents: write
@@ -29,28 +69,6 @@ jobs:
2969
steps:
3070
- uses: actions/checkout@v4
3171

32-
- name: Get version
33-
id: version
34-
run: |
35-
if [ "${{ github.event_name }}" = "repository_dispatch" ]; then
36-
VERSION="${{ github.event.client_payload.version }}"
37-
else
38-
VERSION="${{ inputs.version }}"
39-
fi
40-
echo "version=$VERSION" >> $GITHUB_OUTPUT
41-
echo "📦 Publishing version: $VERSION"
42-
43-
- name: Verify version matches pyproject.toml
44-
run: |
45-
TOML_VERSION=$(grep '^version = ' pyproject.toml | sed 's/version = "\(.*\)"/\1/')
46-
if [ "$TOML_VERSION" != "${{ steps.version.outputs.version }}" ]; then
47-
echo "❌ Version mismatch!"
48-
echo " pyproject.toml: $TOML_VERSION"
49-
echo " Requested: ${{ steps.version.outputs.version }}"
50-
exit 1
51-
fi
52-
echo "✅ Version verified: $TOML_VERSION"
53-
5472
- name: Set up Python
5573
uses: actions/setup-python@v5
5674
with:
@@ -72,34 +90,30 @@ jobs:
7290

7391
- name: Create Git tag
7492
run: |
93+
VERSION="${{ needs.check-version.outputs.version }}"
7594
git config user.name "github-actions[bot]"
7695
git config user.email "github-actions[bot]@users.noreply.github.com"
77-
git tag -a "v${{ steps.version.outputs.version }}" -m "Release v${{ steps.version.outputs.version }}"
78-
git push origin "v${{ steps.version.outputs.version }}"
96+
git tag -a "v$VERSION" -m "Release v$VERSION" || echo "Tag already exists"
97+
git push origin "v$VERSION" || echo "Tag already pushed"
7998
8099
- name: Create GitHub Release
81100
uses: softprops/action-gh-release@v2
82101
with:
83-
tag_name: v${{ steps.version.outputs.version }}
84-
name: v${{ steps.version.outputs.version }}
102+
tag_name: v${{ needs.check-version.outputs.version }}
103+
name: v${{ needs.check-version.outputs.version }}
85104
generate_release_notes: true
86105
draft: false
87-
prerelease: ${{ contains(steps.version.outputs.version, 'beta') || contains(steps.version.outputs.version, 'alpha') }}
106+
prerelease: ${{ contains(needs.check-version.outputs.version, 'beta') || contains(needs.check-version.outputs.version, 'alpha') }}
88107
files: |
89108
dist/*
90109
91110
- name: Summary
92111
run: |
93112
echo "# 🎉 Published to PyPI" >> $GITHUB_STEP_SUMMARY
94113
echo "" >> $GITHUB_STEP_SUMMARY
95-
echo "**Version:** \`${{ steps.version.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY
114+
echo "**Version:** \`${{ needs.check-version.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY
96115
echo "" >> $GITHUB_STEP_SUMMARY
97116
echo "## Install" >> $GITHUB_STEP_SUMMARY
98117
echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY
99-
echo "pip install xdk==${{ steps.version.outputs.version }}" >> $GITHUB_STEP_SUMMARY
118+
echo "pip install xdk==${{ needs.check-version.outputs.version }}" >> $GITHUB_STEP_SUMMARY
100119
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
101-
echo "" >> $GITHUB_STEP_SUMMARY
102-
echo "## Links" >> $GITHUB_STEP_SUMMARY
103-
echo "- [PyPI](https://pypi.org/project/xdk/${{ steps.version.outputs.version }}/)" >> $GITHUB_STEP_SUMMARY
104-
echo "- [GitHub Release](https://github.com/${{ github.repository }}/releases/tag/v${{ steps.version.outputs.version }})" >> $GITHUB_STEP_SUMMARY
105-

0 commit comments

Comments
 (0)