fix: handle version already set in package.json during release #3
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Release | |
| on: | |
| push: | |
| tags: | |
| - 'v*.*.*' | |
| workflow_dispatch: | |
| inputs: | |
| version: | |
| description: 'Release version (e.g., 1.2.0)' | |
| required: true | |
| type: string | |
| jobs: | |
| release: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '22' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci --ignore-scripts | |
| continue-on-error: true | |
| - name: Build | |
| run: npm run build --if-present | |
| continue-on-error: true | |
| - name: Extract version from tag | |
| id: get_version | |
| run: | | |
| if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then | |
| VERSION="${{ github.event.inputs.version }}" | |
| else | |
| VERSION=${GITHUB_REF#refs/tags/v} | |
| fi | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| echo "tag=v$VERSION" >> $GITHUB_OUTPUT | |
| - name: Update package.json version | |
| run: | | |
| CURRENT_VERSION=$(node -p "require('./package.json').version") | |
| TARGET_VERSION="${{ steps.get_version.outputs.version }}" | |
| if [ "$CURRENT_VERSION" != "$TARGET_VERSION" ]; then | |
| npm version $TARGET_VERSION --no-git-tag-version | |
| else | |
| echo "Version already set to $TARGET_VERSION, skipping update" | |
| fi | |
| - name: Generate release notes | |
| id: release_notes | |
| run: | | |
| if [ -f CHANGELOG.md ]; then | |
| # Extract changelog section for this version | |
| VERSION="${{ steps.get_version.outputs.version }}" | |
| awk "/^## \\[$VERSION\\]/,/^## /" CHANGELOG.md | head -n -1 > release_notes.txt || true | |
| else | |
| echo "No CHANGELOG.md found" > release_notes.txt | |
| fi | |
| - name: Create Release | |
| uses: softprops/action-gh-release@v1 | |
| with: | |
| tag_name: ${{ steps.get_version.outputs.tag }} | |
| name: Release ${{ steps.get_version.outputs.tag }} | |
| body_path: release_notes.txt | |
| draft: false | |
| prerelease: false | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Commit version bump | |
| run: | | |
| git config --local user.email "action@github.com" | |
| git config --local user.name "GitHub Action" | |
| git add package.json | |
| git commit -m "chore: bump version to ${{ steps.get_version.outputs.version }}" || exit 0 | |
| git push || exit 0 |