Publish Package to npm #48
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 to npm | |
| on: | |
| release: | |
| types: [published] | |
| workflow_run: | |
| workflows: ["GitHub Release"] | |
| types: [completed] | |
| branches: [main] | |
| jobs: | |
| publish: | |
| if: >- | |
| github.event_name == 'release' || | |
| (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success') | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '22' | |
| registry-url: 'https://registry.npmjs.org' | |
| cache: 'npm' | |
| - name: Decide whether publish is needed | |
| id: need | |
| run: | | |
| if [ "${{ github.event_name }}" = "release" ]; then | |
| echo "publish=true" >> "$GITHUB_OUTPUT" | |
| exit 0 | |
| fi | |
| VERSION=$(node -p "require('./package.json').version") | |
| if npm view "micro-contracts@${VERSION}" version >/dev/null 2>&1; then | |
| echo "publish=false" >> "$GITHUB_OUTPUT" | |
| echo "Version ${VERSION} is already on npm" | |
| else | |
| echo "publish=true" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Install dependencies | |
| if: steps.need.outputs.publish == 'true' | |
| run: npm ci --legacy-peer-deps | |
| - name: Build | |
| if: steps.need.outputs.publish == 'true' | |
| run: npm run build | |
| - name: Bundle | |
| if: steps.need.outputs.publish == 'true' | |
| run: npm run bundle:min | |
| - name: Run tests | |
| if: steps.need.outputs.publish == 'true' | |
| run: npm run test:ci | |
| - name: Publish to npm | |
| if: steps.need.outputs.publish == 'true' | |
| run: npm publish --access public | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
| - name: Upload bundle to GitHub Release | |
| if: steps.need.outputs.publish == 'true' | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| run: | | |
| TAG="v$(node -p "require('./package.json').version")" | |
| gh release upload "$TAG" dist/micro-contracts.bundle.mjs --clobber |