Skip to content

Commit 0458877

Browse files
committed
Auto-publish on version change (no PAT needed)
1 parent 572ecee commit 0458877

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-typescript
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 npm
66

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

11-
workflow_dispatch:
12-
inputs:
13-
version:
14-
description: 'Version to publish (must match package.json)'
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=$(node -p "require('./package.json').version")
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:package.json > /tmp/old_package.json 2>/dev/null || {
42+
echo "should_publish=true" >> $GITHUB_OUTPUT
43+
echo "First commit with package.json - will publish v$VERSION"
44+
exit 0
45+
}
46+
47+
OLD_VERSION=$(node -p "require('/tmp/old_package.json').version" || 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
@@ -28,28 +68,6 @@ jobs:
2868
steps:
2969
- uses: actions/checkout@v4
3070

31-
- name: Get version
32-
id: version
33-
run: |
34-
if [ "${{ github.event_name }}" = "repository_dispatch" ]; then
35-
VERSION="${{ github.event.client_payload.version }}"
36-
else
37-
VERSION="${{ inputs.version }}"
38-
fi
39-
echo "version=$VERSION" >> $GITHUB_OUTPUT
40-
echo "📦 Publishing version: $VERSION"
41-
42-
- name: Verify version matches package.json
43-
run: |
44-
PKG_VERSION=$(node -p "require('./package.json').version")
45-
if [ "$PKG_VERSION" != "${{ steps.version.outputs.version }}" ]; then
46-
echo "❌ Version mismatch!"
47-
echo " package.json: $PKG_VERSION"
48-
echo " Requested: ${{ steps.version.outputs.version }}"
49-
exit 1
50-
fi
51-
echo "✅ Version verified: $PKG_VERSION"
52-
5371
- name: Setup Node.js
5472
uses: actions/setup-node@v4
5573
with:
@@ -73,32 +91,28 @@ jobs:
7391

7492
- name: Create Git tag
7593
run: |
94+
VERSION="${{ needs.check-version.outputs.version }}"
7695
git config user.name "github-actions[bot]"
7796
git config user.email "github-actions[bot]@users.noreply.github.com"
78-
git tag -a "v${{ steps.version.outputs.version }}" -m "Release v${{ steps.version.outputs.version }}"
79-
git push origin "v${{ steps.version.outputs.version }}"
97+
git tag -a "v$VERSION" -m "Release v$VERSION" || echo "Tag already exists"
98+
git push origin "v$VERSION" || echo "Tag already pushed"
8099
81100
- name: Create GitHub Release
82101
uses: softprops/action-gh-release@v2
83102
with:
84-
tag_name: v${{ steps.version.outputs.version }}
85-
name: v${{ steps.version.outputs.version }}
103+
tag_name: v${{ needs.check-version.outputs.version }}
104+
name: v${{ needs.check-version.outputs.version }}
86105
generate_release_notes: true
87106
draft: false
88-
prerelease: ${{ contains(steps.version.outputs.version, 'beta') || contains(steps.version.outputs.version, 'alpha') }}
107+
prerelease: ${{ contains(needs.check-version.outputs.version, 'beta') || contains(needs.check-version.outputs.version, 'alpha') }}
89108

90109
- name: Summary
91110
run: |
92111
echo "# 🎉 Published to npm" >> $GITHUB_STEP_SUMMARY
93112
echo "" >> $GITHUB_STEP_SUMMARY
94-
echo "**Version:** \`${{ steps.version.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY
113+
echo "**Version:** \`${{ needs.check-version.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY
95114
echo "" >> $GITHUB_STEP_SUMMARY
96115
echo "## Install" >> $GITHUB_STEP_SUMMARY
97116
echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY
98-
echo "npm install @xdevplatform/xdk@${{ steps.version.outputs.version }}" >> $GITHUB_STEP_SUMMARY
117+
echo "npm install @xdevplatform/xdk@${{ needs.check-version.outputs.version }}" >> $GITHUB_STEP_SUMMARY
99118
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
100-
echo "" >> $GITHUB_STEP_SUMMARY
101-
echo "## Links" >> $GITHUB_STEP_SUMMARY
102-
echo "- [npm](https://www.npmjs.com/package/@xdevplatform/xdk/v/${{ steps.version.outputs.version }})" >> $GITHUB_STEP_SUMMARY
103-
echo "- [GitHub Release](https://github.com/${{ github.repository }}/releases/tag/v${{ steps.version.outputs.version }})" >> $GITHUB_STEP_SUMMARY
104-

0 commit comments

Comments
 (0)