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: | |
| workflow_dispatch: | |
| inputs: | |
| version: | |
| description: Package version in x.y.z format | |
| required: true | |
| type: string | |
| npm_token: | |
| description: npm token used for publish | |
| required: true | |
| type: string | |
| permissions: | |
| contents: write | |
| id-token: write | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v6 | |
| with: | |
| fetch-depth: 0 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v6 | |
| with: | |
| node-version: lts/* | |
| registry-url: https://registry.npmjs.org | |
| package-manager-cache: false | |
| - name: Validate version format | |
| env: | |
| VERSION: ${{ inputs.version }} | |
| run: | | |
| if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then | |
| echo "Invalid version: $VERSION" | |
| echo "Expected format: x.y.z" | |
| exit 1 | |
| fi | |
| - name: Configure git user | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| - name: Ensure tag does not exist | |
| env: | |
| VERSION: ${{ inputs.version }} | |
| run: | | |
| if git rev-parse "v$VERSION" >/dev/null 2>&1; then | |
| echo "Tag v$VERSION already exists" | |
| exit 1 | |
| fi | |
| - name: Update package version and commit | |
| env: | |
| VERSION: ${{ inputs.version }} | |
| run: | | |
| npm version "$VERSION" --no-git-tag-version | |
| git add package.json | |
| git commit -m "chore(release): v$VERSION" | |
| - name: Build package | |
| run: | | |
| npm install | |
| npm run build | |
| - name: Publish to npm | |
| env: | |
| NPM_TOKEN: ${{ inputs.npm_token }} | |
| run: npm publish --access public --provenance | |
| - name: Create and push tag | |
| env: | |
| VERSION: ${{ inputs.version }} | |
| run: | | |
| git tag "v$VERSION" | |
| git push origin HEAD | |
| git push origin "v$VERSION" | |
| - name: Pack npm tarball | |
| run: | | |
| npm pack --pack-destination dist | |
| - name: Create GitHub release | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| VERSION: ${{ inputs.version }} | |
| run: | | |
| gh release create "v$VERSION" \ | |
| --title "v$VERSION" \ | |
| --generate-notes \ | |
| dist/* |