Publish npm package #20
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 npm package | |
| on: | |
| workflow_dispatch: | |
| permissions: | |
| contents: read | |
| id-token: write | |
| jobs: | |
| build: | |
| name: Build ${{ matrix.target }} | |
| runs-on: ${{ matrix.os }} | |
| strategy: | |
| matrix: | |
| include: | |
| - os: ubuntu-latest | |
| target: x86_64-unknown-linux-gnu | |
| - os: ubuntu-latest | |
| target: aarch64-unknown-linux-gnu | |
| - os: macos-14 | |
| target: aarch64-apple-darwin | |
| - os: windows-latest | |
| target: x86_64-pc-windows-msvc | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Install Rust | |
| uses: dtolnay/rust-toolchain@stable | |
| with: | |
| targets: ${{ matrix.target }} | |
| - uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| - name: Install cross-compilation toolchain | |
| if: matrix.target == 'aarch64-unknown-linux-gnu' | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu | |
| - name: Cache cargo | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| ~/.cargo/registry/index/ | |
| ~/.cargo/registry/cache/ | |
| ~/.cargo/git/db/ | |
| napi/target/ | |
| key: ${{ runner.os }}-${{ matrix.target }}-cargo-napi-${{ hashFiles('**/Cargo.lock') }} | |
| restore-keys: | | |
| ${{ runner.os }}-${{ matrix.target }}-cargo-napi- | |
| - name: Install dependencies | |
| working-directory: napi | |
| run: npm install --ignore-scripts | |
| - name: Build native addon | |
| working-directory: napi | |
| env: | |
| CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: ${{ matrix.target == 'aarch64-unknown-linux-gnu' && 'aarch64-linux-gnu-gcc' || '' }} | |
| run: npx napi build --platform --release --target ${{ matrix.target }} | |
| - name: Upload native binary | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: bindings-${{ matrix.target }} | |
| path: napi/*.node | |
| if-no-files-found: error | |
| - name: Upload generated JS bindings | |
| if: matrix.target == 'x86_64-unknown-linux-gnu' | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: js-bindings | |
| path: | | |
| napi/index.js | |
| napi/index.d.ts | |
| napi/package.json | |
| napi/README.md | |
| if-no-files-found: error | |
| publish: | |
| name: Publish to npm | |
| needs: build | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| id-token: write | |
| steps: | |
| - uses: actions/setup-node@v4 | |
| with: | |
| # Node 22 ships with npm >= 11, required for OIDC Trusted Publishing. | |
| node-version: '22' | |
| registry-url: 'https://registry.npmjs.org' | |
| - name: Ensure latest npm (for OIDC support) | |
| run: npm install -g npm@latest | |
| - name: Download all artifacts | |
| uses: actions/download-artifact@v4 | |
| with: | |
| path: artifacts | |
| - name: Assemble package | |
| run: | | |
| mkdir -p publish-pkg | |
| cp artifacts/js-bindings/* publish-pkg/ | |
| cp artifacts/bindings-*/*.node publish-pkg/ | |
| echo "=== Package contents ===" | |
| ls -la publish-pkg/ | |
| - name: Publish to npm (OIDC Trusted Publishing — no token needed) | |
| working-directory: publish-pkg | |
| run: npm publish --provenance --access public |