ci: automatically publish new versions 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: Release | |
| on: | |
| workflow_dispatch: | |
| pull_request: | |
| types: [closed] | |
| branches: [main] | |
| permissions: | |
| contents: write | |
| concurrency: | |
| group: release-${{ github.repository }} | |
| cancel-in-progress: false | |
| jobs: | |
| release: | |
| if: github.event_name != 'pull_request' || github.event.pull_request.merged == true | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| ref: main | |
| - uses: actions/setup-node@v4 | |
| with: | |
| node-version: 22 | |
| registry-url: https://registry.npmjs.org | |
| - run: npm run check | |
| - run: npm test | |
| - run: npm run test:package | |
| - id: package | |
| shell: bash | |
| run: | | |
| echo "name=$(node -p \"require('./package.json').name\")" >> "$GITHUB_OUTPUT" | |
| echo "version=$(node -p \"require('./package.json').version\")" >> "$GITHUB_OUTPUT" | |
| - name: Create GitHub release | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| VERSION: ${{ steps.package.outputs.version }} | |
| shell: bash | |
| run: | | |
| tag="v${VERSION}" | |
| if gh release view "$tag" >/dev/null 2>&1; then | |
| echo "$tag already exists" | |
| else | |
| gh release create "$tag" \ | |
| --target main \ | |
| --title "$tag" \ | |
| --notes-file CHANGELOG.md | |
| fi | |
| - name: Check npm version | |
| id: npm | |
| env: | |
| PACKAGE_NAME: ${{ steps.package.outputs.name }} | |
| PACKAGE_VERSION: ${{ steps.package.outputs.version }} | |
| shell: bash | |
| run: | | |
| published="$(npm view "${PACKAGE_NAME}@${PACKAGE_VERSION}" version 2>/dev/null || true)" | |
| if [ "$published" = "$PACKAGE_VERSION" ]; then | |
| echo "publish=false" >> "$GITHUB_OUTPUT" | |
| echo "${PACKAGE_NAME}@${PACKAGE_VERSION} already exists on npm" | |
| else | |
| echo "publish=true" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Publish package to npm | |
| if: steps.npm.outputs.publish == 'true' | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
| shell: bash | |
| run: | | |
| if [ -z "${NODE_AUTH_TOKEN:-}" ]; then | |
| echo "::error::Repository secret NPM_TOKEN is not configured" | |
| exit 1 | |
| fi | |
| npm publish --access public |