-
-
Notifications
You must be signed in to change notification settings - Fork 245
272 lines (244 loc) · 9.02 KB
/
Copy pathnpm-rust-publish.yml
File metadata and controls
272 lines (244 loc) · 9.02 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
name: npm-rust-publish
on:
workflow_call:
inputs:
version:
required: true
type: string
push:
branches:
- master
permissions:
contents: read
id-token: write
checks: write
env:
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: "true"
defaults:
run:
shell: bash
jobs:
version:
name: resolve version
runs-on: ubuntu-latest
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- name: Checkout
uses: actions/checkout@v5
- name: Read version
id: version
working-directory: rust
run: |
if [ -n "${{ inputs.version }}" ]; then
version="${{ inputs.version }}"
else
version=$(node -p "require('./package.json').version")
fi
echo "version=${version}" >> "$GITHUB_OUTPUT"
build:
name: build ${{ matrix.target.key }}
needs: version
runs-on: ${{ matrix.target.runner }}
timeout-minutes: 30
strategy:
fail-fast: false
max-parallel: 6
matrix:
target:
- key: linux-x64-gnu
packageName: cpd-linux-x64-gnu
os: linux
cpu: x64
libc: glibc
rustTarget: x86_64-unknown-linux-gnu
runner: ubuntu-22.04
- key: linux-arm64-gnu
packageName: cpd-linux-arm64-gnu
os: linux
cpu: arm64
libc: glibc
rustTarget: aarch64-unknown-linux-gnu
runner: ubuntu-22.04-arm
- key: linux-x64-musl
packageName: cpd-linux-x64-musl
os: linux
cpu: x64
libc: musl
rustTarget: x86_64-unknown-linux-musl
runner: ubuntu-latest
- key: darwin-arm64
packageName: cpd-darwin-arm64
os: darwin
cpu: arm64
rustTarget: aarch64-apple-darwin
runner: macos-latest
- key: darwin-x64
packageName: cpd-darwin-x64
os: darwin
cpu: x64
rustTarget: x86_64-apple-darwin
runner: macos-latest
- key: windows-x64-msvc
packageName: cpd-windows-x64-msvc
os: win32
cpu: x64
rustTarget: x86_64-pc-windows-msvc
runner: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v5
- name: Check platform package already published (idempotency)
id: platform-package
run: |
version="${{ needs.version.outputs.version }}"
package_name="${{ matrix.target.packageName }}"
if npm view "${package_name}@${version}" version >/dev/null 2>&1; then
echo "${package_name}@${version} is already published; skipping build"
echo "published=true" >> "$GITHUB_OUTPUT"
else
echo "published=false" >> "$GITHUB_OUTPUT"
fi
- name: Install Rust toolchain
if: steps.platform-package.outputs.published != 'true'
uses: dtolnay/rust-toolchain@stable
with:
toolchain: "1.93"
targets: ${{ matrix.target.rustTarget }}
- name: Cache Cargo registry and target
if: steps.platform-package.outputs.published != 'true'
uses: Swatinem/rust-cache@v2
with:
workspaces: "rust -> rust/target"
- name: Install cross-compilation tools (Linux ARM64)
if: steps.platform-package.outputs.published != 'true' && matrix.target.key == 'linux-arm64-gnu'
run: |
sudo apt-get update
sudo apt-get install -y gcc-aarch64-linux-gnu
echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc" >> $GITHUB_ENV
- name: Install musl tools
if: steps.platform-package.outputs.published != 'true' && matrix.target.key == 'linux-x64-musl'
run: |
sudo apt-get update
sudo apt-get install -y musl-tools
- name: Install cargo-nextest
if: steps.platform-package.outputs.published != 'true'
uses: taiki-e/install-action@nextest
- name: Test
# Skip for darwin-x64: cross-compiled from ARM Mac, tests cannot run on host
if: steps.platform-package.outputs.published != 'true' && matrix.target.key != 'darwin-x64'
working-directory: rust
run: cargo nextest run --workspace --locked --target ${{ matrix.target.rustTarget }} --profile ci
- name: Upload test results
if: always() && steps.platform-package.outputs.published != 'true' && matrix.target.key != 'darwin-x64'
uses: actions/upload-artifact@v4
with:
name: nextest-results-${{ matrix.target.key }}
path: rust/target/nextest/ci/nextest.xml
if-no-files-found: warn
- name: Test Report
if: always() && steps.platform-package.outputs.published != 'true' && matrix.target.key != 'darwin-x64'
uses: dorny/test-reporter@v1
with:
name: Rust Tests (${{ matrix.target.key }})
path: rust/target/nextest/ci/nextest.xml
reporter: java-junit
fail-on-error: false
- name: Build
if: steps.platform-package.outputs.published != 'true'
working-directory: rust
run: cargo build --release --locked --target ${{ matrix.target.rustTarget }} -p jscpd
- name: Create platform package
if: steps.platform-package.outputs.published != 'true'
id: package
working-directory: rust
run: |
package_dir="$(
node scripts/npm-prebuilt-package.mjs \
--target "${{ matrix.target.key }}" \
--bin-dir "target/${{ matrix.target.rustTarget }}/release" \
--out-dir "target/npm-prebuilt"
)"
echo "package_dir=${package_dir}" >> "$GITHUB_OUTPUT"
- name: Smoke test (non-Windows)
if: steps.platform-package.outputs.published != 'true' && matrix.target.os != 'win32'
working-directory: rust
run: |
version="${{ needs.version.outputs.version }}"
binary="./target/${{ matrix.target.rustTarget }}/release/cpd"
BUILT_VERSION=$("$binary" --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || echo "NOT_FOUND")
echo "Built: ${BUILT_VERSION}, Expected: ${version}"
[ "$BUILT_VERSION" = "${version}" ] || { echo "Version mismatch!"; exit 1; }
- name: Install pnpm
if: steps.platform-package.outputs.published != 'true'
uses: pnpm/action-setup@v4
- name: Install Node
if: steps.platform-package.outputs.published != 'true'
uses: actions/setup-node@v5
with:
node-version: "22"
registry-url: "https://registry.npmjs.org"
- name: Publish platform package
if: steps.platform-package.outputs.published != 'true'
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
working-directory: rust
run: |
version="${{ needs.version.outputs.version }}"
package_name="${{ matrix.target.packageName }}"
package_dir="${{ steps.package.outputs.package_dir }}"
if npm view "${package_name}@${version}" version >/dev/null 2>&1; then
echo "${package_name}@${version} is already published; skipping"
exit 0
fi
npm publish "$package_dir" --access public --provenance
publish-main:
name: publish main cpd package
needs:
- version
- build
if: ${{ always() && needs.build.result == 'success' }}
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout
uses: actions/checkout@v5
- name: Install pnpm
uses: pnpm/action-setup@v4
- name: Install Node
uses: actions/setup-node@v5
with:
node-version: "22"
registry-url: "https://registry.npmjs.org"
- name: Verify all platform packages are published
working-directory: rust
run: |
version="${{ needs.version.outputs.version }}"
targets=$(node -e "
const t = require('./npm/prebuilt-targets.json');
Object.values(t).forEach(v => console.log(v.packageName));
")
missing=0
for pkg in $targets; do
if npm view "${pkg}@${version}" version >/dev/null 2>&1; then
echo "${pkg}@${version} is published"
else
missing=$((missing + 1))
echo "::error title=Missing platform package::${pkg}@${version} is not published"
fi
done
if [ "$missing" -gt 0 ]; then
echo "Missing ${missing} platform packages; cannot publish main package"
exit 1
fi
- name: Publish main cpd package
working-directory: rust
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
version="${{ needs.version.outputs.version }}"
if npm view "cpd@${version}" version >/dev/null 2>&1; then
echo "cpd@${version} is already published; skipping"
exit 0
fi
npm publish --access public --provenance