name: Buld and Publish NPM Package
on:
push:
tags:
- 'v*' workflow_dispatch:
jobs:
build:
name: Build for ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
include:
- os: ubuntu-latest
platform: linux
- os: windows-latest
platform: win32
- os: macos-latest
platform: darwin
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4 with:
node-version: '16.x'
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
- name: Install node-bindgen CLI
run: cargo install nj-cli
- name: Install dependencies
run: npm ci || npm install
- name: Install dependencies (Linux)
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt-get update
sudo apt-get install -y libnode-dev
- name: Build with node-bindgen
run: nj-cli build --release
- name: Create platform directory
run: |
mkdir -p dist/${{ matrix.platform }}
# Copy the built binary to the platform-specific directory
if [ -f "dist/index.node" ]; then
cp dist/index.node dist/${{ matrix.platform }}/
elif [ -f "native/index.node" ]; then
cp native/index.node dist/${{ matrix.platform }}/
else
echo "Could not find .node file"
find . -name "*.node"
exit 1
fi
shell: bash
- name: Upload binary
uses: actions/upload-artifact@v4 with:
name: ${{ matrix.platform }}-binary
path: dist/${{ matrix.platform }}/
publish:
name: Publish Package
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4 with:
node-version: '16.x'
registry-url: 'https://registry.npmjs.org'
- name: Download all platform binaries
uses: actions/download-artifact@v4 with:
path: dist
- name: Organize binaries
run: |
# Move files from the artifact directories to the appropriate platform directories
mkdir -p dist/linux
mkdir -p dist/win32
mkdir -p dist/darwin
cp -r dist/linux-binary/* dist/linux/ || echo "No Linux binary found"
cp -r dist/win32-binary/* dist/win32/ || echo "No Windows binary found"
cp -r dist/darwin-binary/* dist/darwin/ || echo "No macOS binary found"
# List final structure
find dist -type f
- name: Publish to NPM
run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}