-
Notifications
You must be signed in to change notification settings - Fork 2
229 lines (200 loc) · 7.15 KB
/
publish-node.yml
File metadata and controls
229 lines (200 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
name: Publish Node SDK
on:
workflow_call:
workflow_dispatch:
jobs:
build:
strategy:
fail-fast: false
matrix:
settings:
- host: macos-latest
target: aarch64-apple-darwin
- host: ubuntu-22.04
target: x86_64-unknown-linux-gnu
- host: ubuntu-22.04
target: x86_64-unknown-linux-musl
zig: true
- host: ubuntu-22.04
target: aarch64-unknown-linux-gnu
zig: true
- host: ubuntu-22.04
target: aarch64-unknown-linux-musl
zig: true
- host: windows-latest
target: x86_64-pc-windows-msvc
name: Build ${{ matrix.settings.target }}
runs-on: ${{ matrix.settings.host }}
steps:
- uses: actions/checkout@v4
- name: Setup workspace context
shell: bash
run: bash .github/setup-workspace.sh
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.settings.target }}
- name: Install protobuf compiler
shell: bash
run: |
if [[ "${{ matrix.settings.host }}" == ubuntu-* ]]; then
sudo apt-get update && sudo apt-get install -y protobuf-compiler
elif [ "${{ matrix.settings.host }}" = "windows-latest" ]; then
choco install protoc -y
else
brew install protobuf
fi
- name: Install Zig
if: matrix.settings.zig
uses: goto-bus-stop/setup-zig@v2
with:
version: 0.13.0
- name: Install dependencies
working-directory: sdk/node
run: npm install
- name: Build native module
working-directory: sdk/node
shell: bash
run: |
if [ "${{ matrix.settings.zig }}" = "true" ]; then
npx napi build --platform --release --target ${{ matrix.settings.target }} --zig
else
npx napi build --platform --release --target ${{ matrix.settings.target }}
fi
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: node-bindings-${{ matrix.settings.target }}
path: sdk/node/*.node
if-no-files-found: error
publish:
name: Publish to npm
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
registry-url: https://registry.npmjs.org
- name: Install dependencies
working-directory: sdk/node
run: npm install
- name: Download all artifacts
uses: actions/download-artifact@v4
with:
pattern: node-bindings-*
path: sdk/node/artifacts
merge-multiple: true
- name: Move artifacts
working-directory: sdk/node
run: |
mv artifacts/*.node . 2>/dev/null || true
ls -la *.node
- name: Prepublish (generate optionalDependencies)
working-directory: sdk/node
shell: bash
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
set -euo pipefail
test -n "${NPM_TOKEN:-}" || { echo "Missing NPM_TOKEN secret" >&2; exit 1; }
ls -1 *.node
rm -rf npm
mkdir -p npm
VERSION="$(node -p "require('./package.json').version")"
create_pkg() {
local dir="$1"
local pkg_name="$2"
local os="$3"
local cpu="$4"
local node_file="$5"
local libc="${6:-}"
mkdir -p "npm/$dir"
cp "$node_file" "npm/$dir/$node_file"
cat > "npm/$dir/package.json" <<JSON
{
"name": "@a3s-lab/${pkg_name}",
"version": "$VERSION",
"os": ["$os"],
"cpu": ["$cpu"],
"main": "$node_file",
"files": ["$node_file"],
"description": "Native binding for @a3s-lab/code ($dir)",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/A3S-Lab/Code",
"directory": "sdk/node"
}$(if [[ -n "$libc" ]]; then printf ',\n "libc": ["%s"]' "$libc"; fi)
}
JSON
}
create_pkg "darwin-arm64" "code-darwin-arm64" "darwin" "arm64" "index.darwin-arm64.node"
create_pkg "linux-x64-gnu" "code-linux-x64-gnu" "linux" "x64" "index.linux-x64-gnu.node" "glibc"
create_pkg "linux-x64-musl" "code-linux-x64-musl" "linux" "x64" "index.linux-x64-musl.node" "musl"
create_pkg "linux-arm64-gnu" "code-linux-arm64-gnu" "linux" "arm64" "index.linux-arm64-gnu.node" "glibc"
create_pkg "linux-arm64-musl" "code-linux-arm64-musl" "linux" "arm64" "index.linux-arm64-musl.node" "musl"
create_pkg "win32-x64-msvc" "code-win32-x64-msvc" "win32" "x64" "index.win32-x64-msvc.node"
find npm -mindepth 1 -maxdepth 2 -type f | sort
find . -maxdepth 1 -name '*.node' -delete
- name: Publish platform packages
working-directory: sdk/node
shell: bash
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
set -euo pipefail
for pkg in npm/*; do
(
cd "$pkg"
NAME="$(node -p "require('./package.json').name")"
VERSION="$(node -p "require('./package.json').version")"
if npm view "${NAME}@${VERSION}" version >/dev/null 2>&1; then
echo "${NAME}@${VERSION} already exists on npm, skipping."
exit 0
fi
set +e
OUTPUT="$(npm publish --access public --ignore-scripts 2>&1)"
STATUS=$?
set -e
echo "$OUTPUT"
if [ "$STATUS" -ne 0 ]; then
if echo "$OUTPUT" | grep -Eqi "previously published|cannot publish over|already exists"; then
echo "${NAME}@${VERSION} was already published, treating as success."
exit 0
fi
exit "$STATUS"
fi
)
done
- name: Publish to npm
working-directory: sdk/node
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
shell: bash
run: |
set -euo pipefail
NAME="$(node -p "require('./package.json').name")"
VERSION="$(node -p "require('./package.json').version")"
if npm view "${NAME}@${VERSION}" version >/dev/null 2>&1; then
echo "${NAME}@${VERSION} already exists on npm, skipping."
exit 0
fi
set +e
OUTPUT="$(npm publish --access public --ignore-scripts 2>&1)"
STATUS=$?
set -e
echo "$OUTPUT"
if [ "$STATUS" -ne 0 ]; then
if echo "$OUTPUT" | grep -Eqi "previously published|cannot publish over|already exists"; then
echo "${NAME}@${VERSION} was already published, treating as success."
exit 0
fi
exit "$STATUS"
fi