node: build platform specific packages and use as depdendency #52
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 | |
| - host: ubuntu-24.04-arm | |
| target: aarch64-unknown-linux-gnu | |
| platform: linux | |
| arch: arm64 | |
| build: npm run build | |
| # 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: Get version | |
| id: version | |
| shell: bash | |
| run: | | |
| if [[ "${{ github.ref }}" == refs/tags/* ]]; then | |
| # It's a tag - use tag name and strip 'v' prefix if present | |
| VERSION="${{ github.ref_name }}" | |
| VERSION="${VERSION#v}" | |
| else | |
| # It's a branch/commit - use short SHA | |
| VERSION="${{ github.sha }}" | |
| VERSION="${VERSION:0:7}" | |
| fi | |
| echo "VERSION=$VERSION" >> $GITHUB_OUTPUT | |
| echo "Resolved version: $VERSION" | |
| - name: Prepare platform-specific package | |
| working-directory: bindings/nodejs | |
| shell: bash | |
| run: | | |
| # Create platform-specific package.json with version | |
| cat > pkgs/sentencex-${{ matrix.settings.platform }}-${{ matrix.settings.arch }}/package.json << EOF | |
| { | |
| "name": "sentencex-${{ matrix.settings.platform }}-${{ matrix.settings.arch }}", | |
| "version": "$VERSION", | |
| "os": ["${{ matrix.settings.platform }}"], | |
| "cpu": ["${{ matrix.settings.arch }}"], | |
| "main": "index.node", | |
| "license": "MIT", | |
| "repository": { | |
| "type": "git", | |
| "url": "git+https://github.com/${{ github.repository }}.git" | |
| } | |
| } | |
| EOF | |
| # Zip the platform-specific folder | |
| zip -r sentencex-${{ matrix.settings.platform }}-${{ matrix.settings.arch }}-${{steps.version.outputs.VERSION}}.zip pkgs/sentencex-${{ matrix.settings.platform }}-${{ matrix.settings.arch }} | |
| - name: Upload artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: bindings-${{ matrix.settings.target }} | |
| path: bindings/nodejs/sentencex-${{ matrix.settings.platform }}-${{ matrix.settings.arch }}-${{ steps.version.outputs.VERSION }}.zip | |
| 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: Publish platform package | |
| working-directory: bindings/nodejs/downloaded | |
| 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: Zip main package | |
| working-directory: bindings/nodejs | |
| run: zip -r sentencex-main-${{ steps.version.outputs.VERSION}}.zip . | |
| - name: Upload main package artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: sentencex-main | |
| path: bindings/nodejs/sentencex-main-${{ steps.version.outputs.VERSION }}.zip | |
| if-no-files-found: error | |
| - name: Publish main package | |
| working-directory: bindings/nodejs | |
| run: npm publish --access public | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |