node: build platform specific packages and use as depdendency #48
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
| name: Node - Build and Publish | |
| on: | |
| push: | |
| branches: [ main, master ] | |
| tags: | |
| - 'v*' | |
| pull_request: | |
| branches: [ main, master ] | |
| permissions: | |
| contents: read | |
| jobs: | |
| # Build and Test job runs on all platforms/Node versions | |
| build: | |
| name: Build and Test | |
| runs-on: ${{ matrix.settings.host }} | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| settings: | |
| # Linux x64 | |
| - host: ubuntu-latest | |
| target: x86_64-unknown-linux-gnu | |
| platform: linux | |
| arch: x64 | |
| build: npm run build | |
| # Linux ARM64 (cross-compile) | |
| - host: ubuntu-latest | |
| target: aarch64-unknown-linux-gnu | |
| platform: linux | |
| arch: arm64 | |
| build: | | |
| sudo apt-get update | |
| sudo apt-get install -y gcc-aarch64-linux-gnu | |
| export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc | |
| npm run build -- --target aarch64-unknown-linux-gnu | |
| # macOS x64 (Intel) - use macos-13 for Intel runners | |
| - host: macos-13 | |
| target: x86_64-apple-darwin | |
| platform: darwin | |
| arch: x64 | |
| build: npm run build | |
| # macOS ARM64 (Apple Silicon M1/M2/M3) | |
| - host: macos-latest | |
| target: aarch64-apple-darwin | |
| platform: darwin | |
| arch: arm64 | |
| build: npm run build | |
| # Windows x64 | |
| - host: windows-latest | |
| target: x86_64-pc-windows-msvc | |
| platform: win32 | |
| arch: x64 | |
| build: npm run build | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 22 | |
| registry-url: 'https://registry.npmjs.org' | |
| - name: Install Rust | |
| uses: dtolnay/rust-toolchain@stable | |
| with: | |
| targets: ${{ matrix.settings.target }} | |
| - name: Cache cargo | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| ~/.cargo/registry | |
| ~/.cargo/git | |
| target | |
| key: ${{ matrix.settings.target }}-cargo-${{ hashFiles('**/Cargo.lock') }} | |
| - name: Build native module | |
| working-directory: bindings/nodejs | |
| run: ${{ matrix.settings.build }} | |
| env: | |
| CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: aarch64-linux-gnu-gcc | |
| - name: Move index.node to platform-specific folder | |
| working-directory: bindings/nodejs | |
| shell: bash | |
| run: | | |
| cp index.node pkgs/sentencex-${{ matrix.settings.platform }}-${{ matrix.settings.arch }}/ | |
| - name: Install dependencies | |
| working-directory: bindings/nodejs | |
| run: npm install | |
| - name: Run tests for platform-specific package | |
| working-directory: bindings/nodejs | |
| run: | | |
| npm test | |
| node example.js | |
| node example.cjs | |
| - name: Upload artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: bindings-${{ matrix.settings.target }} | |
| path: bindings/nodejs/pkgs/sentencex-${{ matrix.settings.platform }}-${{ matrix.settings.arch }}/*.node | |
| if-no-files-found: error | |
| # Publish platform-specific packages | |
| publish-platforms: | |
| name: Publish Platform Packages | |
| if: startsWith(github.ref, 'refs/tags/v') | |
| needs: build | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| include: | |
| - target: x86_64-unknown-linux-gnu | |
| platform: linux | |
| arch: x64 | |
| - target: aarch64-unknown-linux-gnu | |
| platform: linux | |
| arch: arm64 | |
| - target: x86_64-apple-darwin | |
| platform: darwin | |
| arch: x64 | |
| - target: aarch64-apple-darwin | |
| platform: darwin | |
| arch: arm64 | |
| - target: x86_64-pc-windows-msvc | |
| platform: win32 | |
| arch: x64 | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 22 | |
| registry-url: 'https://registry.npmjs.org' | |
| - name: Download artifact | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: bindings-${{ matrix.target }} | |
| path: bindings/nodejs/downloaded | |
| - name: Create platform package | |
| working-directory: bindings/nodejs | |
| shell: bash | |
| run: | | |
| cp downloaded/*.node pkgs/sentencex-${{ matrix.platform }}-${{ matrix.arch }}/ | |
| # Extract version from tag | |
| VERSION=${GITHUB_REF#refs/tags/v} | |
| # Create platform-specific package.json | |
| cat > npm/${{ matrix.platform }}-${{ matrix.arch }}/package.json << EOF | |
| { | |
| "name": "sentencex-${{ matrix.platform }}-${{ matrix.arch }}", | |
| "version": "$VERSION", | |
| "os": ["${{ matrix.platform }}"], | |
| "cpu": ["${{ matrix.arch }}"], | |
| "main": "index.node", | |
| "license": "MIT", | |
| "repository": { | |
| "type": "git", | |
| "url": "git+https://github.com/${{ github.repository }}.git" | |
| } | |
| } | |
| EOF | |
| - name: Publish platform package | |
| working-directory: bindings/nodejs/pkgs/sentencex-${{ matrix.platform }}-${{ matrix.arch }} | |
| run: npm publish --access public | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
| # Publish main package with optionalDependencies | |
| publish-main: | |
| name: Publish Main Package | |
| if: startsWith(github.ref, 'refs/tags/v') | |
| needs: publish-platforms | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 22 | |
| registry-url: 'https://registry.npmjs.org' | |
| - name: Get version | |
| id: version | |
| run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT | |
| - name: Update package.json with optionalDependencies | |
| working-directory: bindings/nodejs | |
| run: | | |
| node -e " | |
| const fs = require('fs'); | |
| const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8')); | |
| pkg.version = '${{ steps.version.outputs.VERSION }}'; | |
| pkg.optionalDependencies = { | |
| 'sentencex-linux-x64': '${{ steps.version.outputs.VERSION }}', | |
| 'sentencex-linux-arm64': '${{ steps.version.outputs.VERSION }}', | |
| 'sentencex-darwin-x64': '${{ steps.version.outputs.VERSION }}', | |
| 'sentencex-darwin-arm64': '${{ steps.version.outputs.VERSION }}', | |
| 'sentencex-win32-x64': '${{ steps.version.outputs.VERSION }}' | |
| }; | |
| fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2)); | |
| " | |
| - name: Publish main package | |
| working-directory: bindings/nodejs | |
| run: npm publish --access public | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |