Publish to NPM #5
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: | |
| release: | |
| types: [published] | |
| workflow_dispatch: | |
| inputs: | |
| version: | |
| description: 'Version to publish (e.g., patch, minor, major, or specific version like 1.2.3)' | |
| required: true | |
| default: 'patch' | |
| jobs: | |
| publish: | |
| 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: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| registry-url: 'https://registry.npmjs.org' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Run tests | |
| run: npm run lint | |
| - name: Build library | |
| run: npm run build | |
| - name: Version bump (if manual dispatch) | |
| if: github.event_name == 'workflow_dispatch' | |
| run: | | |
| git config --local user.email "action@github.com" | |
| git config --local user.name "GitHub Action" | |
| npm version ${{ github.event.inputs.version }} --no-git-tag-version | |
| git add package.json package-lock.json | |
| git commit -m "chore: bump version to $(node -p "require('./package.json').version")" | |
| git push | |
| - name: Publish to NPM | |
| run: npm publish --access public | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
| - name: Create GitHub Release (if manual dispatch) | |
| if: github.event_name == 'workflow_dispatch' | |
| run: | | |
| VERSION=$(node -p "require('./package.json').version") | |
| git tag "v$VERSION" | |
| git push origin "v$VERSION" | |
| gh release create "v$VERSION" --title "Release v$VERSION" --generate-notes | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |