Publish Package #1
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 Package | |
| on: | |
| release: | |
| types: [published] | |
| workflow_dispatch: | |
| jobs: | |
| publish-npm: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| cache: 'npm' | |
| registry-url: 'https://registry.npmjs.org' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Run tests | |
| run: npm test | |
| - name: Build package | |
| run: npm run build | |
| - name: Check if version exists on npm | |
| id: check-npm-version | |
| 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 "Version $PACKAGE_VERSION already exists on npm" | |
| echo "exists=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "Version $PACKAGE_VERSION does not exist on npm" | |
| echo "exists=false" >> $GITHUB_OUTPUT | |
| fi | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
| - name: Publish to npm | |
| if: steps.check-npm-version.outputs.exists == 'false' | |
| run: npm publish --access public | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
| - name: Skip npm publish | |
| if: steps.check-npm-version.outputs.exists == 'true' | |
| run: echo "Skipping npm publish - version already exists" | |
| publish-github: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| cache: 'npm' | |
| registry-url: 'https://npm.pkg.github.com' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Build package | |
| run: npm run build | |
| - name: Check if version exists on GitHub Packages | |
| id: check-github-version | |
| 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 "Version $PACKAGE_VERSION already exists on GitHub Packages" | |
| echo "exists=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "Version $PACKAGE_VERSION does not exist on GitHub Packages" | |
| echo "exists=false" >> $GITHUB_OUTPUT | |
| fi | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Publish to GitHub Packages | |
| if: steps.check-github-version.outputs.exists == 'false' | |
| run: npm publish --access public | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Skip GitHub Packages publish | |
| if: steps.check-github-version.outputs.exists == 'true' | |
| run: echo "Skipping GitHub Packages publish - version already exists" |