Skip to content

Commit d8f963b

Browse files
author
Maxim Titovich
committed
Enhance automation: Add auto-versioning and release creation to CI/CD
1 parent 9497e6f commit d8f963b

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

.github/workflows/publish.yml

+63-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,24 @@ on:
1010
- 'build/**'
1111
release:
1212
types: [created]
13+
workflow_dispatch:
14+
inputs:
15+
force_publish:
16+
description: 'Force publish even if version exists'
17+
required: false
18+
type: boolean
19+
default: false
1320

1421
jobs:
1522
publish:
1623
runs-on: ubuntu-latest
24+
permissions:
25+
contents: write
26+
packages: write
1727
steps:
1828
- uses: actions/checkout@v3
29+
with:
30+
fetch-depth: 0
1931

2032
- name: Use Node.js
2133
uses: actions/setup-node@v3
@@ -24,6 +36,11 @@ jobs:
2436
cache: 'npm'
2537
registry-url: 'https://registry.npmjs.org'
2638

39+
- name: Setup Git
40+
run: |
41+
git config user.name "GitHub Actions Bot"
42+
git config user.email "[email protected]"
43+
2744
- name: Install dependencies
2845
run: npm ci
2946

@@ -52,9 +69,54 @@ jobs:
5269
echo "Version ${{ steps.extract_version.outputs.version }} doesn't exist yet"
5370
fi
5471
continue-on-error: true
72+
73+
# Auto bump version if it already exists and force_publish is true or triggered by push
74+
- name: Auto bump version if needed
75+
id: auto_bump
76+
if: |
77+
(steps.version_check.outputs.exists == 'true') &&
78+
((github.event_name == 'push') || (github.event_name == 'workflow_dispatch' && inputs.force_publish))
79+
run: |
80+
echo "Auto-bumping version because version ${{ steps.extract_version.outputs.version }} already exists"
81+
npm version patch -m "Auto bump version to %s [skip ci]"
82+
echo "new_version=$(node -p "require('./package.json').version")" >> $GITHUB_OUTPUT
83+
84+
# Get final version to use
85+
- name: Determine final version
86+
id: final_version
87+
run: |
88+
if [[ "${{ steps.auto_bump.outputs.new_version }}" != "" ]]; then
89+
echo "version=${{ steps.auto_bump.outputs.new_version }}" >> $GITHUB_OUTPUT
90+
echo "Using auto-bumped version: ${{ steps.auto_bump.outputs.new_version }}"
91+
else
92+
echo "version=${{ steps.extract_version.outputs.version }}" >> $GITHUB_OUTPUT
93+
echo "Using original version: ${{ steps.extract_version.outputs.version }}"
94+
fi
95+
96+
# Push changes if version was bumped
97+
- name: Push changes if version was bumped
98+
if: steps.auto_bump.outputs.new_version != ''
99+
run: |
100+
git push
101+
git push --tags
102+
103+
# Create GitHub release if version was bumped or if the version doesn't exist (for manual releases)
104+
- name: Create GitHub Release
105+
if: |
106+
(steps.auto_bump.outputs.new_version != '') ||
107+
(steps.version_check.outputs.exists == 'false')
108+
uses: softprops/action-gh-release@v1
109+
with:
110+
tag_name: v${{ steps.final_version.outputs.version }}
111+
name: Release v${{ steps.final_version.outputs.version }}
112+
generate_release_notes: true
113+
env:
114+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
55115

56116
- name: Publish to npm
57-
if: steps.version_check.outputs.exists != 'true'
117+
if: |
118+
(steps.version_check.outputs.exists == 'false') ||
119+
(steps.auto_bump.outputs.new_version != '')
58120
run: npm publish
59121
env:
60122
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

README.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -346,17 +346,24 @@ You can see the workflow details in `.github/workflows/ci.yml`.
346346
The publishing workflow runs when:
347347
- A change is pushed to the main branch that modifies `package.json` or source code
348348
- A new GitHub release is created
349+
- Manually triggered via GitHub Actions interface
349350

350351
It performs the following steps:
351352
1. Runs all checks and builds the package
352353
2. Extracts the package version from package.json
353354
3. Checks if that version already exists on npm
354-
4. If the version is new, publishes the package to npm
355+
4. If the version exists, automatically bumps the patch version
356+
5. Creates a GitHub release for the new version
357+
6. Publishes the package to npm
355358

356359
To use this workflow, you need to:
357360
1. Add an `NPM_TOKEN` secret to your GitHub repository settings
358-
2. Increase the version number in package.json when ready to publish
359-
3. Push to the main branch or create a new GitHub release
361+
2. Either:
362+
- Push changes to the main branch (auto-versioning will handle the rest)
363+
- Manually trigger the workflow from the Actions tab
364+
- Create a GitHub release
365+
366+
The workflow supports automatic version bumping when a version conflict is detected, making continuous delivery seamless.
360367

361368
You can see the workflow details in `.github/workflows/publish.yml`.
362369

0 commit comments

Comments
 (0)