Publish to npm #2
Workflow file for this run
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
| # Publishes the package to npm when a GitHub Release is created. | |
| # Uses npm Trusted Publishing (OIDC) — no access token needed. | |
| # | |
| # Setup (one-time on npmjs.com): | |
| # 1. Go to https://www.npmjs.com/package/mg-api-js/access | |
| # 2. Under "Publishing access" → Configure Trusted Publishing | |
| # 3. Add a new trusted publisher: | |
| # - Repository owner: Geotab | |
| # - Repository name: mg-api-js | |
| # - Workflow filename: publish.yml | |
| # - Environment: (leave blank) | |
| # | |
| # Usage: | |
| # 1. Bump the version: npm version patch|minor|major | |
| # 2. Push with tags: git push && git push --tags | |
| # 3. Create a GitHub Release from the new tag and add release notes | |
| # 4. The workflow runs automatically: build → test → publish | |
| name: Publish to npm | |
| on: | |
| release: | |
| types: [published] | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| id-token: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 22.x | |
| - name: Upgrade npm for Trusted Publishing | |
| run: npm install -g npm@latest | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Build | |
| run: npm run build | |
| - name: Run tests | |
| run: npm run test:node | |
| - name: Verify version matches tag | |
| run: | | |
| PACKAGE_VERSION="v$(node -p "require('./package.json').version")" | |
| TAG_NAME="${{ github.event.release.tag_name }}" | |
| if [ "$PACKAGE_VERSION" != "$TAG_NAME" ]; then | |
| echo "Error: package.json version ($PACKAGE_VERSION) does not match release tag ($TAG_NAME)" | |
| exit 1 | |
| fi | |
| - name: Publish to npm | |
| run: npm publish --access public |