ci: publish npm with provenance (links package to repo automatically) #43
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 | |
| # Runs on every push to main and publishes only when package.json's version | |
| # isn't on npm yet (prerelease versions go under their alpha/beta/rc dist-tag). | |
| # Merges that don't introduce a new version are a no-op. | |
| on: | |
| push: | |
| branches: | |
| - main | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| id-token: write | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| registry-url: 'https://registry.npmjs.org' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Resolve version & publish status | |
| id: v | |
| run: | | |
| VERSION=$(node -p "require('./package.json').version") | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| if npm view strapi-plugin-form-builder-cms@$VERSION version >/dev/null 2>&1; then | |
| echo "publish=false" >> $GITHUB_OUTPUT | |
| echo "ℹ️ $VERSION is already on npm — nothing to publish." | |
| else | |
| echo "publish=true" >> $GITHUB_OUTPUT | |
| echo "🚀 $VERSION is not published yet — will publish." | |
| fi | |
| - name: Type check (server) | |
| if: steps.v.outputs.publish == 'true' | |
| run: npm run test:ts:back | |
| - name: Type check (admin) | |
| if: steps.v.outputs.publish == 'true' | |
| run: npm run test:ts:front | |
| - name: Build | |
| if: steps.v.outputs.publish == 'true' | |
| run: npm run build | |
| - name: Verify plugin | |
| if: steps.v.outputs.publish == 'true' | |
| run: npm run verify | |
| - name: Determine npm dist-tag | |
| id: tag | |
| if: steps.v.outputs.publish == 'true' | |
| run: | | |
| VERSION="${{ steps.v.outputs.version }}" | |
| if [[ "$VERSION" == *"alpha"* ]]; then | |
| echo "tag=alpha" >> $GITHUB_OUTPUT | |
| elif [[ "$VERSION" == *"beta"* ]]; then | |
| echo "tag=beta" >> $GITHUB_OUTPUT | |
| elif [[ "$VERSION" == *"rc"* ]]; then | |
| echo "tag=rc" >> $GITHUB_OUTPUT | |
| else | |
| echo "tag=latest" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Publish to npm | |
| if: steps.v.outputs.publish == 'true' | |
| run: npm publish --provenance --access public --tag ${{ steps.tag.outputs.tag }} | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
| - name: Tag the release | |
| if: steps.v.outputs.publish == 'true' | |
| run: | | |
| git tag v${{ steps.v.outputs.version }} | |
| git push origin v${{ steps.v.outputs.version }} | |
| - name: Create GitHub release | |
| if: steps.v.outputs.publish == 'true' | |
| uses: softprops/action-gh-release@v2 | |
| with: | |
| tag_name: v${{ steps.v.outputs.version }} | |
| name: v${{ steps.v.outputs.version }} | |
| generate_release_notes: true | |
| token: ${{ secrets.GITHUB_TOKEN }} |