Update publish-npm.yml #17
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 and Publish NPM Package | |
| on: | |
| push: | |
| branches: | |
| - main | |
| jobs: | |
| create_release: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| outputs: | |
| version: ${{ steps.package_version.outputs.version }} | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Setup Node | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 22 | |
| - name: Get package version | |
| id: package_version | |
| run: | | |
| echo "version=$(node -p "require('./package.json').version")" >> "$GITHUB_OUTPUT" | |
| - name: Create git tag if missing | |
| run: | | |
| VERSION="v${{ steps.package_version.outputs.version }}" | |
| git fetch --tags | |
| if git rev-parse "$VERSION" >/dev/null 2>&1; then | |
| echo "Tag $VERSION already exists. Skipping tag creation." | |
| else | |
| echo "Creating tag $VERSION" | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git tag "$VERSION" | |
| git push origin "$VERSION" | |
| fi | |
| - name: Create or update GitHub release | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| VERSION="v${{ steps.package_version.outputs.version }}" | |
| if gh release view "$VERSION" >/dev/null 2>&1; then | |
| echo "Release $VERSION already exists. Updating release." | |
| gh release edit "$VERSION" \ | |
| --title "$VERSION" \ | |
| --notes "Release $VERSION" | |
| else | |
| echo "Creating release $VERSION" | |
| gh release create "$VERSION" \ | |
| --title "$VERSION" \ | |
| --notes "Release $VERSION" | |
| fi | |
| publish: | |
| runs-on: ubuntu-latest | |
| needs: create_release | |
| permissions: | |
| contents: read | |
| id-token: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Setup Node | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 22 | |
| registry-url: https://registry.npmjs.org/ | |
| - name: Show tool versions | |
| run: | | |
| node --version | |
| npm --version | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Build package | |
| run: npm run build | |
| - name: Check if version already exists on npm | |
| id: npm_check | |
| run: | | |
| PACKAGE_NAME=$(node -p "require('./package.json').name") | |
| PACKAGE_VERSION=$(node -p "require('./package.json').version") | |
| if npm view "${PACKAGE_NAME}@${PACKAGE_VERSION}" version >/dev/null 2>&1; then | |
| echo "exists=true" >> "$GITHUB_OUTPUT" | |
| echo "Version ${PACKAGE_NAME}@${PACKAGE_VERSION} already exists on npm. Skipping publish." | |
| else | |
| echo "exists=false" >> "$GITHUB_OUTPUT" | |
| echo "Version ${PACKAGE_NAME}@${PACKAGE_VERSION} does not exist on npm. Publishing." | |
| fi | |
| - name: Publish to NPM with trusted publishing | |
| if: steps.npm_check.outputs.exists == 'false' | |
| run: npm publish --provenance --access public |