|
1 | | -name: Auto Version Bump and Publish |
| 1 | +name: Auto Publish |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
5 | 5 | branches: [main] |
6 | | - paths-ignore: |
7 | | - - "README.md" |
8 | | - - "LICENSE" |
9 | | - - ".gitignore" |
10 | | - - ".npmignore" |
11 | | - - "docs/**" |
| 6 | + workflow_run: |
| 7 | + workflows: ["CI"] |
| 8 | + types: [completed] |
| 9 | + branches: [main] |
| 10 | + |
| 11 | +# Only permissions to create tags and publish to NPM |
| 12 | +permissions: |
| 13 | + contents: write # For creating tags |
| 14 | + id-token: write # For NPM provenance |
12 | 15 |
|
13 | 16 | jobs: |
14 | | - version-and-publish: |
| 17 | + auto-publish: |
15 | 18 | runs-on: ubuntu-latest |
16 | | - |
17 | | - # Only run if the commit doesn't already contain a version bump or skip ci |
18 | | - if: "!contains(github.event.head_commit.message, 'chore: bump version') && !contains(github.event.head_commit.message, '[skip ci]')" |
| 19 | + # Only run if CI passed and this is a push to main |
| 20 | + if: | |
| 21 | + github.event_name == 'push' && |
| 22 | + github.ref == 'refs/heads/main' || |
| 23 | + (github.event_name == 'workflow_run' && |
| 24 | + github.event.workflow_run.conclusion == 'success' && |
| 25 | + github.event.workflow_run.head_branch == 'main') |
19 | 26 |
|
20 | 27 | steps: |
21 | | - - name: Checkout code |
| 28 | + - name: Checkout |
22 | 29 | uses: actions/checkout@v4 |
23 | 30 | with: |
24 | | - # Fetch full history for version bumping |
25 | 31 | fetch-depth: 0 |
26 | | - # Use a token that can push back to the repository |
27 | 32 | token: ${{ secrets.GITHUB_TOKEN }} |
28 | 33 |
|
29 | 34 | - name: Setup Node.js |
30 | 35 | uses: actions/setup-node@v4 |
31 | 36 | with: |
32 | 37 | node-version: "18" |
33 | | - registry-url: "https://registry.npmjs.org/" |
34 | 38 | cache: "npm" |
| 39 | + registry-url: "https://registry.npmjs.org" |
35 | 40 |
|
36 | 41 | - name: Install dependencies |
37 | 42 | run: npm ci |
38 | 43 |
|
39 | | - - name: Run tests |
40 | | - run: npm test |
41 | | - |
42 | | - - name: Build project |
| 44 | + - name: Build |
43 | 45 | run: npm run build |
44 | 46 |
|
45 | | - - name: Configure Git |
46 | | - run: | |
47 | | - git config --local user.email "action@github.com" |
48 | | - git config --local user.name "GitHub Action" |
49 | | -
|
50 | | - - name: Determine version bump type |
51 | | - id: version-bump |
| 47 | + - name: Check if publish needed |
| 48 | + id: check-publish |
52 | 49 | run: | |
53 | | - # Check commit messages to determine bump type |
54 | | - COMMITS=$(git log --oneline $(git describe --tags --abbrev=0)..HEAD 2>/dev/null || git log --oneline) |
55 | | -
|
56 | | - if echo "$COMMITS" | grep -q -i "breaking\|major"; then |
57 | | - echo "bump_type=major" >> $GITHUB_OUTPUT |
58 | | - echo "Detected MAJOR version bump" |
59 | | - elif echo "$COMMITS" | grep -q -i "feat\|feature\|minor"; then |
60 | | - echo "bump_type=minor" >> $GITHUB_OUTPUT |
61 | | - echo "Detected MINOR version bump" |
| 50 | + CURRENT_VERSION=$(node -p "require('./package.json').version") |
| 51 | + PUBLISHED_VERSION=$(npm view $(node -p "require('./package.json').name") version 2>/dev/null || echo "0.0.0") |
| 52 | + echo "Current version: $CURRENT_VERSION" |
| 53 | + echo "Published version: $PUBLISHED_VERSION" |
| 54 | + echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT |
| 55 | +
|
| 56 | + if [ "$CURRENT_VERSION" != "$PUBLISHED_VERSION" ]; then |
| 57 | + echo "publish_needed=true" >> $GITHUB_OUTPUT |
| 58 | + echo "✅ Version bump detected: $PUBLISHED_VERSION → $CURRENT_VERSION" |
62 | 59 | else |
63 | | - echo "bump_type=patch" >> $GITHUB_OUTPUT |
64 | | - echo "Detected PATCH version bump" |
| 60 | + echo "publish_needed=false" >> $GITHUB_OUTPUT |
| 61 | + echo "ℹ️ No version bump needed (current: $CURRENT_VERSION)" |
65 | 62 | fi |
66 | 63 |
|
67 | | - - name: Bump version |
68 | | - id: version |
| 64 | + - name: Create tag |
| 65 | + if: steps.check-publish.outputs.publish_needed == 'true' |
69 | 66 | run: | |
70 | | - # Bump version based on detected type |
71 | | - NEW_VERSION=$(npm version ${{ steps.version-bump.outputs.bump_type }} --no-git-tag-version) |
72 | | - echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT |
73 | | - echo "New version: $NEW_VERSION" |
74 | | -
|
75 | | - - name: Update package-lock.json |
76 | | - run: npm install --package-lock-only |
77 | | - |
78 | | - - name: Commit version bump |
79 | | - run: | |
80 | | - git add package.json package-lock.json |
81 | | - git commit -m "chore: bump version to ${{ steps.version.outputs.new_version }}" |
82 | | - git tag ${{ steps.version.outputs.new_version }} |
83 | | -
|
84 | | - - name: Push changes |
85 | | - run: | |
86 | | - git push origin main |
87 | | - git push origin ${{ steps.version.outputs.new_version }} |
| 67 | + CURRENT_VERSION=$(node -p "require('./package.json').version") |
| 68 | + git config --local user.email "action@github.com" |
| 69 | + git config --local user.name "GitHub Action" |
| 70 | + git tag "v$CURRENT_VERSION" |
| 71 | + git push origin "v$CURRENT_VERSION" |
88 | 72 |
|
89 | 73 | - name: Publish to NPM |
90 | | - run: npm publish |
| 74 | + if: steps.check-publish.outputs.publish_needed == 'true' |
| 75 | + run: npm publish --access public --provenance |
91 | 76 | env: |
92 | 77 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
93 | 78 |
|
94 | 79 | - name: Create GitHub Release |
| 80 | + if: steps.check-publish.outputs.publish_needed == 'true' |
95 | 81 | uses: actions/create-release@v1 |
96 | 82 | env: |
97 | 83 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
98 | 84 | with: |
99 | | - tag_name: ${{ steps.version.outputs.new_version }} |
100 | | - release_name: Release ${{ steps.version.outputs.new_version }} |
101 | | - body: | |
102 | | - ## Changes in ${{ steps.version.outputs.new_version }} |
103 | | -
|
104 | | - Auto-generated release for version ${{ steps.version.outputs.new_version }}. |
105 | | -
|
106 | | - ### Commits included: |
107 | | - ${{ github.event.head_commit.message }} |
108 | | -
|
109 | | - **Full Changelog**: https://github.com/${{ github.repository }}/compare/${{ github.event.before }}...${{ github.sha }} |
| 85 | + tag_name: v${{ steps.check-publish.outputs.current_version }} |
| 86 | + release_name: Release v${{ steps.check-publish.outputs.current_version }} |
110 | 87 | draft: false |
111 | 88 | prerelease: false |
0 commit comments