1+ name : Publish Package
2+
3+ on :
4+ push :
5+ branches :
6+ - 1.x
7+ workflow_dispatch :
8+
9+ jobs :
10+ verify_version :
11+ runs-on : ubuntu-latest
12+ outputs :
13+ should_publish : ${{ steps.check.outputs.should_publish }}
14+ version : ${{ steps.check.outputs.version }}
15+ steps :
16+ - name : Checkout repository
17+ uses : actions/checkout@v4
18+ with :
19+ fetch-depth : 0
20+
21+ - name : Check if package.json version changed
22+ id : check
23+ run : |
24+ echo "Current branch: ${{ github.ref }}"
25+
26+ # Get current version
27+ CURRENT_VERSION=$(jq -r .version package.json)
28+ echo "Current version: $CURRENT_VERSION"
29+
30+ # Get previous commit hash
31+ git rev-parse HEAD~1 || git rev-parse HEAD
32+ PREV_COMMIT=$(git rev-parse HEAD~1 2>/dev/null || git rev-parse HEAD)
33+
34+ # Check if package.json changed
35+ if git diff --name-only HEAD~1 HEAD | grep "package.json"; then
36+ echo "Package.json was changed in this commit"
37+
38+ # Get previous version if possible
39+ if git show "$PREV_COMMIT:package.json" 2>/dev/null; then
40+ PREV_VERSION=$(git show "$PREV_COMMIT:package.json" | jq -r .version)
41+ echo "Previous version: $PREV_VERSION"
42+
43+ if [ "$CURRENT_VERSION" != "$PREV_VERSION" ]; then
44+ echo "Version changed from $PREV_VERSION to $CURRENT_VERSION"
45+ echo "should_publish=true" >> $GITHUB_OUTPUT
46+ else
47+ echo "Version unchanged"
48+ echo "should_publish=false" >> $GITHUB_OUTPUT
49+ fi
50+ else
51+ echo "First commit with package.json, will publish"
52+ echo "should_publish=true" >> $GITHUB_OUTPUT
53+ fi
54+ else
55+ echo "Package.json not changed in this commit"
56+ echo "should_publish=false" >> $GITHUB_OUTPUT
57+ fi
58+
59+ echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
60+
61+ publish :
62+ needs : verify_version
63+ if : needs.verify_version.outputs.should_publish == 'true'
64+ runs-on : ubuntu-latest
65+ permissions :
66+ contents : write
67+ steps :
68+ - name : Checkout repository
69+ uses : actions/checkout@v4
70+ with :
71+ fetch-depth : 0
72+
73+ - name : Create Git tag
74+ run : |
75+ git config user.name "github-actions[bot]"
76+ git config user.email "github-actions[bot]@users.noreply.github.com"
77+ git tag -a "v${{ needs.verify_version.outputs.version }}" -m "Release v${{ needs.verify_version.outputs.version }}"
78+ git push origin "v${{ needs.verify_version.outputs.version }}"
79+ env :
80+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
81+
82+ - name : Setup Bun
83+ uses : oven-sh/setup-bun@v2
84+
85+ - name : Install dependencies
86+ run : bun install
87+
88+ - name : Build package
89+ run : bun run build
90+
91+ - name : Publish to npm
92+ run : bun publish
93+ env :
94+ NPM_CONFIG_TOKEN : ${{ secrets.NPM_TOKEN }}
95+
96+ create_release :
97+ needs : [verify_version, publish]
98+ if : needs.verify_version.outputs.should_publish == 'true'
99+ runs-on : ubuntu-latest
100+ permissions :
101+ contents : write
102+ steps :
103+ - name : Create GitHub Release
104+ uses : actions/create-release@v1
105+ env :
106+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
107+ with :
108+ tag_name : ' v${{ needs.verify_version.outputs.version }}'
109+ release_name : ' v${{ needs.verify_version.outputs.version }}'
110+ body : ' Release v${{ needs.verify_version.outputs.version }}'
111+ draft : false
112+ prerelease : false
0 commit comments