Skip to content

Commit 352de3c

Browse files
committed
revise CI
1 parent f3124c1 commit 352de3c

File tree

4 files changed

+228
-231
lines changed

4 files changed

+228
-231
lines changed

.github/workflows/auto-publish.yml

Lines changed: 46 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,88 @@
1-
name: Auto Version Bump and Publish
1+
name: Auto Publish
22

33
on:
44
push:
55
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
1215

1316
jobs:
14-
version-and-publish:
17+
auto-publish:
1518
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')
1926
2027
steps:
21-
- name: Checkout code
28+
- name: Checkout
2229
uses: actions/checkout@v4
2330
with:
24-
# Fetch full history for version bumping
2531
fetch-depth: 0
26-
# Use a token that can push back to the repository
2732
token: ${{ secrets.GITHUB_TOKEN }}
2833

2934
- name: Setup Node.js
3035
uses: actions/setup-node@v4
3136
with:
3237
node-version: "18"
33-
registry-url: "https://registry.npmjs.org/"
3438
cache: "npm"
39+
registry-url: "https://registry.npmjs.org"
3540

3641
- name: Install dependencies
3742
run: npm ci
3843

39-
- name: Run tests
40-
run: npm test
41-
42-
- name: Build project
44+
- name: Build
4345
run: npm run build
4446

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
5249
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"
6259
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)"
6562
fi
6663
67-
- name: Bump version
68-
id: version
64+
- name: Create tag
65+
if: steps.check-publish.outputs.publish_needed == 'true'
6966
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"
8872
8973
- name: Publish to NPM
90-
run: npm publish
74+
if: steps.check-publish.outputs.publish_needed == 'true'
75+
run: npm publish --access public --provenance
9176
env:
9277
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
9378

9479
- name: Create GitHub Release
80+
if: steps.check-publish.outputs.publish_needed == 'true'
9581
uses: actions/create-release@v1
9682
env:
9783
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
9884
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 }}
11087
draft: false
11188
prerelease: false

.github/workflows/ci.yml

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@ name: CI
22

33
on:
44
push:
5-
branches: [main, develop]
5+
branches: [main]
66
pull_request:
7-
branches: [main, develop]
7+
branches: [main]
8+
9+
# Minimal permissions - read repo and write to PR branches only
10+
permissions:
11+
contents: write # Needed for pushing to PR branches
12+
pull-requests: write
813

914
jobs:
1015
ci:
1116
runs-on: ubuntu-latest
1217

1318
steps:
14-
- name: Checkout code
19+
- name: Checkout
1520
uses: actions/checkout@v4
1621
with:
17-
# Use a token that can push back to the repository for format fixes
1822
token: ${{ secrets.GITHUB_TOKEN }}
19-
fetch-depth: 0
2023

2124
- name: Setup Node.js
2225
uses: actions/setup-node@v4
@@ -27,58 +30,39 @@ jobs:
2730
- name: Install dependencies
2831
run: npm ci
2932

30-
- name: Build project
31-
run: npm run build
32-
33-
- name: Run linter
34-
run: npm run lint
35-
36-
- name: Check code formatting
33+
- name: Check format
3734
id: format-check
3835
run: |
39-
# Run format and capture if any files were changed
40-
npm run format
36+
npm run format:check
37+
echo "format_needed=$?" >> $GITHUB_OUTPUT
38+
continue-on-error: true
4139

42-
# Check if any files were modified
43-
if git diff --quiet; then
44-
echo "formatting_needed=false" >> $GITHUB_OUTPUT
45-
echo "✅ All files are properly formatted"
46-
else
47-
echo "formatting_needed=true" >> $GITHUB_OUTPUT
48-
echo "❌ Files need formatting"
49-
echo "The following files were reformatted:"
50-
git diff --name-only
51-
fi
52-
53-
- name: Configure Git for auto-fix
54-
if: steps.format-check.outputs.formatting_needed == 'true'
40+
- name: Auto-fix formatting (PR only)
41+
if: github.event_name == 'pull_request' && steps.format-check.outputs.format_needed != '0'
5542
run: |
43+
npm run format
5644
git config --local user.email "action@github.com"
57-
git config --local user.name "GitHub Action Auto-Format"
58-
59-
- name: Commit formatting fixes (PR only)
60-
if: steps.format-check.outputs.formatting_needed == 'true' && github.event_name == 'pull_request'
61-
run: |
45+
git config --local user.name "GitHub Action"
6246
git add .
63-
git commit -m "style: auto-fix code formatting [skip ci]"
64-
git push origin HEAD:${{ github.head_ref }}
65-
echo "✅ Formatting fixes committed to PR branch: ${{ github.head_ref }}"
66-
67-
- name: Fail CI due to formatting issues
68-
if: steps.format-check.outputs.formatting_needed == 'true'
69-
run: |
70-
if [ "${{ github.event_name }}" = "pull_request" ]; then
71-
echo "::error::Code formatting issues were found and automatically fixed."
72-
echo "::error::The formatting fixes have been committed to the PR branch."
73-
echo "::error::Please review the formatting changes and re-run CI."
47+
if git diff --staged --quiet; then
48+
echo "No formatting changes needed after all"
7449
else
75-
echo "::error::Code formatting issues were found in main branch."
76-
echo "::error::Please run 'npm run format' locally and commit the changes."
77-
echo "::error::Auto-fix is only available for Pull Requests."
50+
git commit -m "Auto-fix formatting [skip ci]"
51+
git push origin HEAD:${{ github.head_ref }}
7852
fi
53+
54+
- name: Fail on format issues (main branch)
55+
if: github.ref == 'refs/heads/main' && steps.format-check.outputs.format_needed != '0'
56+
run: |
57+
echo "❌ Code formatting issues detected on main branch"
58+
echo "Please run 'npm run format' locally and commit the changes"
7959
exit 1
8060
81-
- name: Run tests
82-
# Only run tests if formatting was OK
83-
if: steps.format-check.outputs.formatting_needed == 'false'
61+
- name: Lint
62+
run: npm run lint
63+
64+
- name: Build
65+
run: npm run build
66+
67+
- name: Test
8468
run: npm test

.github/workflows/manual-publish.yml

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,23 @@ on:
1212
- patch
1313
- minor
1414
- major
15-
skip_tests:
16-
description: "Skip tests before publishing"
15+
dry_run:
16+
description: "Dry run (no actual publish)"
1717
required: false
1818
default: false
1919
type: boolean
2020

21+
# Enhanced permissions for manual version bumping and publishing
22+
permissions:
23+
contents: write # For creating commits, tags, and releases
24+
id-token: write # For NPM provenance
25+
2126
jobs:
2227
manual-publish:
2328
runs-on: ubuntu-latest
2429

2530
steps:
26-
- name: Checkout code
31+
- name: Checkout
2732
uses: actions/checkout@v4
2833
with:
2934
fetch-depth: 0
@@ -33,17 +38,16 @@ jobs:
3338
uses: actions/setup-node@v4
3439
with:
3540
node-version: "18"
36-
registry-url: "https://registry.npmjs.org/"
3741
cache: "npm"
42+
registry-url: "https://registry.npmjs.org"
3843

3944
- name: Install dependencies
4045
run: npm ci
4146

4247
- name: Run tests
43-
if: ${{ !inputs.skip_tests }}
4448
run: npm test
4549

46-
- name: Build project
50+
- name: Build
4751
run: npm run build
4852

4953
- name: Configure Git
@@ -54,41 +58,67 @@ jobs:
5458
- name: Bump version
5559
id: version
5660
run: |
57-
NEW_VERSION=$(npm version ${{ inputs.version_type }} --no-git-tag-version)
58-
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
59-
echo "Manual version bump: $NEW_VERSION"
61+
if [ "${{ inputs.dry_run }}" = "true" ]; then
62+
echo "🔍 DRY RUN MODE - No changes will be made"
63+
CURRENT_VERSION=$(node -p "require('./package.json').version")
64+
echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
65+
echo "new_version=dry-run-$CURRENT_VERSION" >> $GITHUB_OUTPUT
66+
else
67+
NEW_VERSION=$(npm version ${{ inputs.version_type }} --no-git-tag-version)
68+
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
69+
echo "✅ Version bumped to: $NEW_VERSION"
70+
fi
6071
6172
- name: Update package-lock.json
73+
if: inputs.dry_run == false
6274
run: npm install --package-lock-only
6375

6476
- name: Commit version bump
77+
if: inputs.dry_run == false
6578
run: |
6679
git add package.json package-lock.json
67-
git commit -m "chore: manual bump version to ${{ steps.version.outputs.new_version }}"
68-
git tag ${{ steps.version.outputs.new_version }}
80+
git commit -m "chore: bump version to ${{ steps.version.outputs.new_version }}"
81+
git tag "${{ steps.version.outputs.new_version }}"
6982
7083
- name: Push changes
84+
if: inputs.dry_run == false
7185
run: |
7286
git push origin main
73-
git push origin ${{ steps.version.outputs.new_version }}
87+
git push origin "${{ steps.version.outputs.new_version }}"
7488
7589
- name: Publish to NPM
76-
run: npm publish
90+
if: inputs.dry_run == false
91+
run: npm publish --access public --provenance
7792
env:
7893
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
7994

8095
- name: Create GitHub Release
96+
if: inputs.dry_run == false
8197
uses: actions/create-release@v1
8298
env:
8399
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
84100
with:
85101
tag_name: ${{ steps.version.outputs.new_version }}
86102
release_name: Release ${{ steps.version.outputs.new_version }}
87103
body: |
88-
## Manual Release ${{ steps.version.outputs.new_version }}
104+
## Changes in ${{ steps.version.outputs.new_version }}
89105
90-
This release was manually triggered with a ${{ inputs.version_type }} version bump.
106+
Manual release for version ${{ steps.version.outputs.new_version }}.
91107
92-
**Full Changelog**: https://github.com/${{ github.repository }}/commits/main
108+
Version bump type: **${{ inputs.version_type }}**
93109
draft: false
94110
prerelease: false
111+
112+
- name: Summary
113+
run: |
114+
if [ "${{ inputs.dry_run }}" = "true" ]; then
115+
echo "🔍 **DRY RUN COMPLETED**"
116+
echo "- Current version: ${{ steps.version.outputs.current_version }}"
117+
echo "- Would bump to: ${{ inputs.version_type }}"
118+
echo "- No changes were made"
119+
else
120+
echo "✅ **PUBLISH COMPLETED**"
121+
echo "- New version: ${{ steps.version.outputs.new_version }}"
122+
echo "- Published to NPM: https://www.npmjs.com/package/$(node -p 'require(\"./package.json\").name')"
123+
echo "- GitHub release created"
124+
fi

0 commit comments

Comments
 (0)