|
| 1 | +name: Publish to npm |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + version: |
| 9 | + description: 'Version to publish (e.g., 1.0.0)' |
| 10 | + required: true |
| 11 | + type: string |
| 12 | + |
| 13 | +jobs: |
| 14 | + publish: |
| 15 | + name: Publish Package |
| 16 | + runs-on: ubuntu-latest |
| 17 | + permissions: |
| 18 | + contents: write |
| 19 | + id-token: write |
| 20 | + |
| 21 | + steps: |
| 22 | + - name: Checkout code |
| 23 | + uses: actions/checkout@v4 |
| 24 | + with: |
| 25 | + fetch-depth: 0 |
| 26 | + |
| 27 | + - name: Setup Node.js |
| 28 | + uses: actions/setup-node@v4 |
| 29 | + with: |
| 30 | + node-version: '20.x' |
| 31 | + registry-url: 'https://registry.npmjs.org' |
| 32 | + cache: 'npm' |
| 33 | + |
| 34 | + - name: Install dependencies |
| 35 | + run: npm ci |
| 36 | + |
| 37 | + - name: Run tests |
| 38 | + run: npm test |
| 39 | + |
| 40 | + - name: Build |
| 41 | + run: npm run build |
| 42 | + |
| 43 | + - name: Verify package |
| 44 | + run: npm pack --dry-run |
| 45 | + |
| 46 | + - name: Extract version from release |
| 47 | + if: github.event_name == 'release' |
| 48 | + id: version |
| 49 | + run: | |
| 50 | + VERSION=${GITHUB_REF#refs/tags/v} |
| 51 | + VERSION=${VERSION#refs/tags/} |
| 52 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 53 | + echo "Publishing version: $VERSION" |
| 54 | +
|
| 55 | + - name: Verify version matches package.json |
| 56 | + if: github.event_name == 'release' |
| 57 | + run: | |
| 58 | + PACKAGE_VERSION=$(node -p "require('./package.json').version") |
| 59 | + RELEASE_VERSION="${{ steps.version.outputs.version }}" |
| 60 | + if [ "$PACKAGE_VERSION" != "$RELEASE_VERSION" ]; then |
| 61 | + echo "Error: package.json version ($PACKAGE_VERSION) doesn't match release tag ($RELEASE_VERSION)" |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | + echo "Version verified: $PACKAGE_VERSION" |
| 65 | +
|
| 66 | + - name: Update package.json version (manual) |
| 67 | + if: github.event_name == 'workflow_dispatch' |
| 68 | + run: | |
| 69 | + npm version "${{ github.event.inputs.version }}" --no-git-tag-version |
| 70 | + echo "Updated package.json to version ${{ github.event.inputs.version }}" |
| 71 | +
|
| 72 | + - name: Publish to npm |
| 73 | + uses: JS-DevTools/npm-publish@v3 |
| 74 | + with: |
| 75 | + token: ${{ secrets.NPM_TOKEN }} |
| 76 | + registry: https://registry.npmjs.org |
| 77 | + access: public |
0 commit comments