Publish to NPM #15
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: Publish to NPM | |
| on: | |
| push: | |
| tags: | |
| - 'v*' | |
| release: | |
| types: [published] | |
| workflow_dispatch: | |
| inputs: | |
| version: | |
| description: 'Version to publish (optional)' | |
| required: false | |
| default: '' | |
| jobs: | |
| publish: | |
| name: Publish to NPM | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| id-token: write | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Install pnpm | |
| uses: pnpm/action-setup@v2 | |
| with: | |
| version: 8 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20.x | |
| cache: 'pnpm' | |
| registry-url: 'https://registry.npmjs.org' | |
| - name: Install dependencies | |
| run: pnpm install --no-frozen-lockfile | |
| - name: Configure Git | |
| run: | | |
| git config --global user.name "github-actions[bot]" | |
| git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
| - name: Check and bump version if needed | |
| id: version_check | |
| if: ${{ github.event.inputs.version == '' }} | |
| run: | | |
| # Get current version from package.json | |
| CURRENT_VERSION=$(node -p "require('./package.json').version") | |
| echo "Current package.json version: $CURRENT_VERSION" | |
| # Get published version from npm (suppress errors if package doesn't exist) | |
| PUBLISHED_VERSION=$(pnpm view clear-router version 2>/dev/null || echo "0.0.0") | |
| echo "Published npm version: $PUBLISHED_VERSION" | |
| # Function to compare versions | |
| compare_versions() { | |
| if [ "$1" = "$2" ]; then | |
| echo "equal" | |
| else | |
| # Use sort -V for version comparison | |
| SORTED=$(printf "%s\n%s" "$1" "$2" | sort -V | head -n1) | |
| if [ "$SORTED" = "$1" ]; then | |
| echo "less" | |
| else | |
| echo "greater" | |
| fi | |
| fi | |
| } | |
| COMPARISON=$(compare_versions "$CURRENT_VERSION" "$PUBLISHED_VERSION") | |
| echo "Version comparison: $COMPARISON" | |
| if [ "$COMPARISON" = "equal" ] || [ "$COMPARISON" = "less" ]; then | |
| echo "Version conflict detected. Auto-bumping version..." | |
| # Parse version components | |
| IFS='.' read -r MAJOR MINOR PATCH <<< "$PUBLISHED_VERSION" | |
| # Bump patch version | |
| NEW_PATCH=$((PATCH + 1)) | |
| NEW_VERSION="${MAJOR}.${MINOR}.${NEW_PATCH}" | |
| echo "New version: $NEW_VERSION" | |
| # Update package.json | |
| pnpm version "$NEW_VERSION" --no-git-tag-version | |
| echo "version_bumped=true" >> $GITHUB_OUTPUT | |
| echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT | |
| else | |
| echo "Version is valid. Proceeding with current version." | |
| echo "version_bumped=false" >> $GITHUB_OUTPUT | |
| echo "new_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Commit and push version bump | |
| if: steps.version_check.outputs.version_bumped == 'true' | |
| run: | | |
| git add package.json | |
| git commit -m "chore: bump version to ${{ steps.version_check.outputs.new_version }} [skip ci]" | |
| git push origin HEAD:main | |
| - name: Set version from input | |
| if: ${{ github.event.inputs.version != '' }} | |
| run: | | |
| echo "Setting version to ${{ github.event.inputs.version }}" | |
| pnpm version "${{ github.event.inputs.version }}" --no-git-tag-version | |
| echo "version_bumped=true" >> $GITHUB_OUTPUT | |
| echo "new_version=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT | |
| git add package.json | |
| git commit -m "chore: set version to ${{ github.event.inputs.version }} [skip ci]" | |
| git push origin HEAD:main | |
| - name: Run linter | |
| run: pnpm lint | |
| - name: Run tests | |
| run: pnpm test | |
| - name: Build project | |
| run: pnpm build | |
| - name: Publish to NPM | |
| run: pnpm publish --no-git-checks | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
| - name: Create release notes | |
| if: steps.version_check.outputs.version_bumped == 'true' | |
| run: | | |
| echo "Published version ${{ steps.version_check.outputs.new_version }} to npm" >> $GITHUB_STEP_SUMMARY | |
| echo "Auto-bumped from published version" >> $GITHUB_STEP_SUMMARY |