Skip to content

Commit afd76b0

Browse files
authored
chore: Add NAPI cross compilation workflow (#34)
1 parent 9cf6c53 commit afd76b0

File tree

1 file changed

+214
-0
lines changed

1 file changed

+214
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
name: Node Napi Cross compile testing
2+
env:
3+
DEBUG: napi:*
4+
APP_NAME: peerdas-kzg
5+
MACOSX_DEPLOYMENT_TARGET: '10.13'
6+
permissions:
7+
contents: write
8+
id-token: write
9+
10+
on:
11+
workflow_dispatch:
12+
inputs:
13+
ref:
14+
description: The reference (branch/tag/commit) to checkout
15+
required: true
16+
17+
jobs:
18+
build:
19+
strategy:
20+
fail-fast: false
21+
matrix:
22+
settings:
23+
- host: macos-latest
24+
target: x86_64-apple-darwin
25+
- host: macos-latest
26+
target: aarch64-apple-darwin
27+
- host: ubuntu-latest
28+
target: x86_64-unknown-linux-gnu
29+
- host: ubuntu-latest
30+
target: aarch64-unknown-linux-gnu
31+
- host: ubuntu-latest
32+
target: x86_64-pc-windows-msvc
33+
- host: ubuntu-latest
34+
target: aarch64-pc-windows-msvc
35+
36+
name: Build - ${{ matrix.settings.target }}
37+
runs-on: ${{ matrix.settings.host }}
38+
steps:
39+
- uses: actions/checkout@v4
40+
with:
41+
ref: ${{ inputs.ref }}
42+
- name: Setup node
43+
uses: actions/setup-node@v4
44+
with:
45+
node-version: 20
46+
- name: Install Rust toolchain
47+
uses: dtolnay/rust-toolchain@stable
48+
with:
49+
targets: ${{ matrix.settings.target }}
50+
51+
- name: Install llvm-preview-tools
52+
if: runner.os == 'Linux' && contains(matrix.settings.target, 'windows')
53+
run: rustup component add llvm-tools-preview
54+
- name: Install dependencies
55+
run: yarn install
56+
working-directory: bindings/node
57+
- name: Setup Zig (Linux only)
58+
uses: goto-bus-stop/setup-zig@v2
59+
with:
60+
version: 0.13.0
61+
- name: Install cargo-zigbuild (Linux only)
62+
if: runner.os == 'Linux'
63+
run: cargo install cargo-zigbuild
64+
- name: Install cargo-xwin (Windows on Linux only)
65+
if: runner.os == 'Linux' && contains(matrix.settings.target, 'windows')
66+
run: cargo install cargo-xwin
67+
- name: Build (macOS)
68+
if: runner.os == 'macOS'
69+
run: |
70+
rustup target add ${{ matrix.settings.target }}
71+
yarn build --release --target ${{ matrix.settings.target }}
72+
working-directory: bindings/node
73+
- name: Build (Linux)
74+
if: runner.os == 'Linux' && !contains(matrix.settings.target, 'windows')
75+
run: yarn build --zig --release --target ${{ matrix.settings.target }}
76+
working-directory: bindings/node
77+
- name: Build (Windows on Linux)
78+
if: runner.os == 'Linux' && contains(matrix.settings.target, 'windows')
79+
run: yarn build --release --target ${{ matrix.settings.target }}
80+
working-directory: bindings/node
81+
- name: Upload artifact
82+
uses: actions/upload-artifact@v4
83+
with:
84+
name: bindings-${{ matrix.settings.target }}
85+
path: bindings/node/${{ env.APP_NAME }}.*.node
86+
if-no-files-found: error
87+
88+
test:
89+
needs: build
90+
strategy:
91+
fail-fast: false
92+
matrix:
93+
settings:
94+
- host: macos-13
95+
target: x86_64-apple-darwin
96+
- host: macos-14
97+
target: aarch64-apple-darwin
98+
- host: ubuntu-latest
99+
target: x86_64-unknown-linux-gnu
100+
- host: ubuntu-latest
101+
target: aarch64-unknown-linux-gnu
102+
- host: windows-latest
103+
target: x86_64-pc-windows-msvc
104+
- host: windows-latest
105+
target: aarch64-pc-windows-msvc
106+
node:
107+
- '20'
108+
109+
name: Test - ${{ matrix.settings.target }} - node@${{ matrix.node }}
110+
runs-on: ${{ matrix.settings.host }}
111+
steps:
112+
- uses: actions/checkout@v4
113+
with:
114+
ref: ${{ inputs.ref }}
115+
- name: Setup node
116+
uses: actions/setup-node@v4
117+
with:
118+
node-version: ${{ matrix.node }}
119+
cache: yarn
120+
cache-dependency-path: 'bindings/node'
121+
- name: Install dependencies
122+
run: yarn install
123+
working-directory: bindings/node
124+
- name: Download artifacts
125+
uses: actions/download-artifact@v4
126+
with:
127+
name: bindings-${{ matrix.settings.target }}
128+
path: bindings/node
129+
130+
# Native testing for macOS (both Intel and ARM)
131+
- name: Test bindings (macOS)
132+
if: startsWith(matrix.settings.host, 'macos')
133+
run: yarn test
134+
working-directory: bindings/node
135+
136+
# Native testing for x86_64 Linux
137+
- name: Test bindings (Linux x86_64)
138+
if: matrix.settings.target == 'x86_64-unknown-linux-gnu'
139+
run: yarn test
140+
working-directory: bindings/node
141+
142+
# Emulated testing for aarch64 Linux
143+
- name: Set up QEMU
144+
if: matrix.settings.target == 'aarch64-unknown-linux-gnu'
145+
uses: docker/setup-qemu-action@v3
146+
with:
147+
platforms: arm64
148+
- name: Test bindings (Linux ARM64)
149+
if: matrix.settings.target == 'aarch64-unknown-linux-gnu'
150+
uses: addnab/docker-run-action@v3
151+
with:
152+
image: node:${{ matrix.node }}-slim
153+
options: '--platform linux/arm64 -v ${{ github.workspace }}:/build -w /build'
154+
run: |
155+
cd bindings/node
156+
yarn config set supportedArchitectures.cpu "arm64"
157+
yarn config set supportedArchitectures.libc "glibc"
158+
yarn install
159+
yarn test
160+
161+
# Native testing for x86_64 Windows
162+
- name: Test bindings (Windows x86_64)
163+
if: matrix.settings.target == 'x86_64-pc-windows-msvc'
164+
run: yarn test
165+
working-directory: bindings/node
166+
167+
# Placeholder for Windows ARM64 testing
168+
# - name: Test bindings (Windows ARM64)
169+
# if: matrix.settings.target == 'aarch64-pc-windows-msvc'
170+
# shell: powershell
171+
# run: |
172+
# Write-Host "Testing Windows ARM64 is not implemented yet."
173+
# working-directory: bindings/node
174+
175+
publish:
176+
name: Publish
177+
runs-on: ubuntu-latest
178+
needs:
179+
- test
180+
defaults:
181+
run:
182+
working-directory: bindings/node
183+
steps:
184+
- uses: actions/checkout@v4
185+
with:
186+
ref: ${{ inputs.ref }}
187+
- name: Setup node
188+
uses: actions/setup-node@v4
189+
with:
190+
node-version: 20
191+
cache: yarn
192+
cache-dependency-path: 'bindings/node'
193+
- name: Install dependencies
194+
run: yarn install
195+
- name: Download all artifacts
196+
uses: actions/download-artifact@v4
197+
with:
198+
path: artifacts
199+
- name: Move artifacts
200+
run: yarn artifacts
201+
- name: Publish
202+
run: |
203+
npm config set provenance true
204+
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
205+
if [[ ${{ inputs.ref }} =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
206+
npm publish --access public
207+
elif [[ ${{ inputs.ref }} =~ ^[a-zA-Z0-9_-]+$ ]]; then
208+
npm publish --tag next --access public
209+
else
210+
echo "Invalid ref format. Skipping publish."
211+
fi
212+
env:
213+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
214+
NPM_TOKEN: ${{ secrets.NPM_RELEASE_TOKEN }}

0 commit comments

Comments
 (0)