-
Notifications
You must be signed in to change notification settings - Fork 107
547 lines (465 loc) · 19.9 KB
/
nodejs.yml
File metadata and controls
547 lines (465 loc) · 19.9 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
name: Node.js CI
on:
push:
branches: [master]
pull_request:
branches: [master]
merge_group:
jobs:
typecheck:
runs-on: ubuntu-latest
concurrency:
group: typecheck-${{ github.workflow }}-#${{ github.event.pull_request.number || github.head_ref || github.ref }}
cancel-in-progress: true
steps:
- name: Checkout Git Source
uses: actions/checkout@v6
- name: Use Node.js
uses: actions/setup-node@v6
with:
node-version: 22
- name: setup utoo
uses: utooland/setup-utoo@v1
- name: Install Dependencies
run: ut
- name: Lint
run: ut lint
- name: Format
run: ut fmtcheck
- name: Typecheck
run: ut typecheck
- name: Build
run: ut tsc && ut tsc:prod
test-deployment:
runs-on: ubuntu-latest
concurrency:
group: test-deployment-${{ github.workflow }}-#${{ github.event.pull_request.number || github.head_ref || github.ref }}
cancel-in-progress: true
services:
mysql:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: true
MYSQL_DATABASE: cnpmcore
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5
redis:
image: redis
ports:
- 6379:6379
steps:
- name: Checkout Git Source
uses: actions/checkout@v6
- name: Use Node.js
uses: actions/setup-node@v6
with:
node-version: 22
- name: setup utoo
uses: utooland/setup-utoo@v1
- name: Install Dependencies
run: ut
# https://github.com/elastic/elastic-github-actions/blob/master/elasticsearch/README.md
- name: Configure sysctl limits
run: |
sudo swapoff -a
sudo sysctl -w vm.swappiness=1
sudo sysctl -w fs.file-max=262144
sudo sysctl -w vm.max_map_count=262144
- name: Runs Elasticsearch
uses: elastic/elastic-github-actions/elasticsearch@98431b45a60cbb6ace481778827bc8e11f12fd65 # master
with:
stack-version: 8.18.0
security-enabled: false
- name: Wait for Elasticsearch and create index
run: |
while ! curl -s http://localhost:9200 | grep -q "elasticsearch"; do
echo "Waiting for Elasticsearch to be ready..."
sleep 1
done
echo "✅ Elasticsearch is ready"
echo "📦 Creating index with proper mappings..."
CREATE_RESULT=$(curl -sS -X PUT "http://localhost:9200/cnpmcore_packages" \
-H 'content-type: application/json' \
-d @docs/elasticsearch-index.json)
echo "Create index result: ${CREATE_RESULT}"
if ! echo "$CREATE_RESULT" | grep -q '"acknowledged":true'; then
echo "::error::Failed to create ES index"
exit 1
fi
echo "✅ ES index created with custom analyzers and mappings"
- name: Test Deployment
run: |
ut build
echo "Preparing database..."
CNPMCORE_DATABASE_NAME=cnpmcore bash ./prepare-database-mysql.sh
echo "Starting cnpmcore with Elasticsearch enabled..."
CNPMCORE_FORCE_LOCAL_FS=true CNPMCORE_CONFIG_ENABLE_ES=true CNPMCORE_CONFIG_ES_CLIENT_NODE=http://localhost:9200 CNPMCORE_CONFIG_SEARCH_FILTER_DEPRECATED=true ut start:foreground &
sleep 5
echo "Checking cnpmcore is ready..."
set -Eeuo pipefail
URL="http://127.0.0.1:7001"
PATTERN="instance_start_time"
TIMEOUT=60
TMP="$(mktemp)"
echo "🔎 Health check $URL, expect 200 & body contains: $PATTERN"
deadline=$((SECONDS + TIMEOUT))
last_status=""
health_ok=false
while (( SECONDS < deadline )); do
last_status="$(curl -sS -o "$TMP" -w '%{http_code}' "$URL" || true)"
echo "last_status=$last_status"
echo "body=$(cat $TMP)"
if [[ "$last_status" == "200" ]] && grep -q "$PATTERN" "$TMP"; then
echo "✅ OK"
rm -f "$TMP"
health_ok=true
break
fi
sleep 1
done
if [[ "$health_ok" != "true" ]]; then
echo "::error::❌ Health check failed: status=$last_status, pattern not matched or timeout"
echo "---- Response body (last try) ----"
cat "$TMP" || true
rm -f "$TMP"
exit 1
fi
- name: Test Search API with Elasticsearch
run: |
set -Eeuo pipefail
BASE_URL="http://127.0.0.1:7001"
echo "📦 Publishing a test package..."
# Login as admin user (admins can register even when allowPublicRegistration is false)
NPM_UA="npm/10.0.0 node/v22.0.0 linux x64"
TOKEN=$(curl -sS -X PUT "${BASE_URL}/-/user/org.couchdb.user:cnpmcore_admin" \
-H 'content-type: application/json' \
-H "user-agent: ${NPM_UA}" \
-d '{"name":"cnpmcore_admin","password":"testpassword123","email":"admin@cnpmjs.org","type":"user"}' \
| node -e "process.stdout.write(JSON.parse(require('fs').readFileSync('/dev/stdin','utf8')).token || '')")
if [ -z "$TOKEN" ]; then
echo "::error::Failed to create admin user"
exit 1
fi
echo "✅ Got admin auth token"
# Publish a test package (using valid tgz data from test fixtures)
TGZ_DATA="H4sIAAAAAAAAA+2SsWrDMBCGPfspDg2ZinOyEgeylg6Zu2YR8rVRHEtGkkOg5N0jWaFdujVQAv6W4/7/dHcSGqTq5Ccthxyro7emeDCI2KxWkOKmaaaIdc4TouZQ8FqgwI3AdVMgF8ijho9e5DdGH6SLq/y1T74LfMcn4asEYEb2xLbA+q4O5ENv2/FE7CVZZ3JeW5NcrLDiWW3JK6eHcHey2Es9Zdq0dIkfKau50EcjjYpCmpDKSB0s7Nmbc9ZtwVhIBviBlP7Q1O4ZLBZAFx2As3jyOnWTYzhY9zPzpBUZPy2/e39l5bX87wedmZmZeRJuheTX2wAIAAA="
PACKAGE_JSON=$(cat <<EOF
{
"name": "@cnpm/search-test-pkg",
"version": "1.0.0",
"description": "A test package for search integration testing",
"keywords": ["search", "test", "integration"],
"readme": "# search-test-pkg",
"_attachments": {
"@cnpm/search-test-pkg-1.0.0.tgz": {
"content_type": "application/octet-stream",
"data": "${TGZ_DATA}",
"length": 251
}
},
"dist-tags": {
"latest": "1.0.0"
},
"versions": {
"1.0.0": {
"name": "@cnpm/search-test-pkg",
"version": "1.0.0",
"description": "A test package for search integration testing",
"keywords": ["search", "test", "integration"]
}
}
}
EOF
)
PUBLISH_RESULT=$(curl -sS -X PUT "${BASE_URL}/@cnpm/search-test-pkg" \
-H "authorization: Bearer ${TOKEN}" \
-H 'content-type: application/json' \
-H "user-agent: ${NPM_UA}" \
-d "${PACKAGE_JSON}")
echo "Publish result: ${PUBLISH_RESULT}"
if ! echo "$PUBLISH_RESULT" | grep -q '"ok":true'; then
echo "::error::Failed to publish test package"
exit 1
fi
echo "✅ Published @cnpm/search-test-pkg@1.0.0"
# Sync to search index
echo "🔄 Syncing package to search index..."
SYNC_RESULT=$(curl -sS -X PUT "${BASE_URL}/-/v1/search/sync/@cnpm/search-test-pkg" \
-H "authorization: Bearer ${TOKEN}")
echo "Sync result: ${SYNC_RESULT}"
echo "✅ Synced to search index"
# Flush ES index to make document searchable immediately
curl -sS -X POST "http://localhost:9200/cnpmcore_packages/_refresh"
echo ""
# Verify document exists in ES directly
echo "🔍 Checking ES document directly..."
ES_DOC=$(curl -sS "http://localhost:9200/cnpmcore_packages/_doc/@cnpm%2Fsearch-test-pkg")
echo "ES document: ${ES_DOC}"
echo "$ES_DOC" | node -e "
const doc = JSON.parse(require('fs').readFileSync('/dev/stdin','utf8'));
if (!doc.found) { console.log('❌ Document not found in ES'); process.exit(1); }
const pkg = doc._source.package;
if (pkg.deprecated) { console.log('❌ Non-deprecated package should not have deprecated field'); process.exit(1); }
console.log('✅ ES document exists, no deprecated field');
"
# Search for the package via cnpmcore search API
echo "🔍 Searching for test package via API..."
SEARCH_RESULT=$(curl -sS "${BASE_URL}/-/v1/search?text=search-test-pkg&size=10")
echo "Search result: ${SEARCH_RESULT}"
echo "$SEARCH_RESULT" | node -e "
const data = JSON.parse(require('fs').readFileSync('/dev/stdin','utf8'));
const found = data.objects && data.objects.some(o => o.package.name === '@cnpm/search-test-pkg');
if (found) { console.log('✅ Package found in search results'); process.exit(0); }
else { console.log('❌ Package not found in search results'); process.exit(1); }
"
# ---- Test deprecated package filtering ----
echo ""
echo "========================================="
echo "Testing deprecated package search filter"
echo "========================================="
echo "🔄 Deprecating the package..."
DEPRECATE_JSON=$(cat <<'EOF'
{
"name": "@cnpm/search-test-pkg",
"versions": {
"1.0.0": {
"name": "@cnpm/search-test-pkg",
"version": "1.0.0",
"deprecated": "This package is deprecated for testing"
}
}
}
EOF
)
DEPRECATE_RESULT=$(curl -sS -X PUT "${BASE_URL}/@cnpm/search-test-pkg" \
-H "authorization: Bearer ${TOKEN}" \
-H 'content-type: application/json' \
-H "user-agent: ${NPM_UA}" \
-d "${DEPRECATE_JSON}")
echo "Deprecate result: ${DEPRECATE_RESULT}"
if ! echo "$DEPRECATE_RESULT" | grep -q '"ok":true'; then
echo "::error::Failed to deprecate package"
exit 1
fi
echo "✅ Package deprecated"
# Re-sync to update search index with deprecated flag
echo "🔄 Re-syncing deprecated package to search index..."
SYNC_RESULT2=$(curl -sS -X PUT "${BASE_URL}/-/v1/search/sync/@cnpm/search-test-pkg" \
-H "authorization: Bearer ${TOKEN}")
echo "Sync result: ${SYNC_RESULT2}"
# Flush ES index
curl -sS -X POST "http://localhost:9200/cnpmcore_packages/_refresh"
echo ""
# Verify deprecated field is now in ES document
echo "🔍 Checking ES document after deprecation..."
ES_DOC2=$(curl -sS "http://localhost:9200/cnpmcore_packages/_doc/@cnpm%2Fsearch-test-pkg")
echo "ES document: ${ES_DOC2}"
echo "$ES_DOC2" | node -e "
const doc = JSON.parse(require('fs').readFileSync('/dev/stdin','utf8'));
if (!doc.found) { console.log('❌ Document not found in ES'); process.exit(1); }
const pkg = doc._source.package;
if (pkg.deprecated !== 'This package is deprecated for testing') {
console.log('❌ Expected deprecated field, got:', pkg.deprecated);
process.exit(1);
}
console.log('✅ ES document has deprecated field:', pkg.deprecated);
"
# Search via cnpmcore API — deprecated package should be filtered out
echo "🔍 Searching via API (deprecated should be filtered)..."
SEARCH_RESULT2=$(curl -sS "${BASE_URL}/-/v1/search?text=search-test-pkg&size=10")
echo "Search result: ${SEARCH_RESULT2}"
echo "$SEARCH_RESULT2" | node -e "
const data = JSON.parse(require('fs').readFileSync('/dev/stdin','utf8'));
const found = data.objects && data.objects.some(o => o.package.name === '@cnpm/search-test-pkg');
if (!found) { console.log('✅ Deprecated package correctly filtered from search results'); process.exit(0); }
else { console.log('❌ Deprecated package should not appear in search results'); process.exit(1); }
"
# Verify the document still exists in ES directly (just filtered from search)
echo "🔍 Verifying package still exists in ES (just filtered from search)..."
ES_SEARCH3=$(curl -sS "http://localhost:9200/cnpmcore_packages/_search" \
-H 'content-type: application/json' \
-d '{"query":{"match_all":{}}}')
echo "ES search result: ${ES_SEARCH3}"
echo "$ES_SEARCH3" | node -e "
const data = JSON.parse(require('fs').readFileSync('/dev/stdin','utf8'));
const found = data.hits && data.hits.hits.some(h => h._source.package.name === '@cnpm/search-test-pkg');
if (found) { console.log('✅ Deprecated package still exists in ES (just filtered from search)'); process.exit(0); }
else { console.log('❌ Package should still exist in ES'); process.exit(1); }
"
echo ""
echo "✅ All search integration tests passed"
- name: Stop cnpmcore
if: always()
run: npx eggctl stop || true
test-postgresql-fs-nfs:
strategy:
fail-fast: false
matrix:
node-version: [22, 24]
os: [ubuntu-latest]
name: test on postgresql (node@${{ matrix.node-version }})
concurrency:
group: test-postgresql-fs-nfs-${{ github.workflow }}-#${{ github.event.pull_request.number || github.head_ref || github.ref }}-${{ matrix.node-version }}
cancel-in-progress: true
runs-on: ${{ matrix.os }}
services:
# https://docs.github.com/en/actions/use-cases-and-examples/using-containerized-services/creating-postgresql-service-containers
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres
# Provide the password for postgres
env:
POSTGRES_PASSWORD: postgres
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps tcp port 5432 on service container to the host
- 5432:5432
steps:
- name: Checkout Git Source
uses: actions/checkout@v6
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v6
with:
node-version: ${{ matrix.node-version }}
- name: setup utoo
uses: utooland/setup-utoo@v1
- name: Install Dependencies
run: ut
# https://github.com/elastic/elastic-github-actions/blob/master/elasticsearch/README.md
- name: Configure sysctl limits
run: |
sudo swapoff -a
sudo sysctl -w vm.swappiness=1
sudo sysctl -w fs.file-max=262144
sudo sysctl -w vm.max_map_count=262144
- name: Runs Elasticsearch
uses: elastic/elastic-github-actions/elasticsearch@98431b45a60cbb6ace481778827bc8e11f12fd65 # master
with:
stack-version: 8.18.0
security-enabled: false
- name: Wait for Elasticsearch to be ready
run: |
curl -v http://localhost:9200
while ! curl -s http://localhost:9200 | grep -q "elasticsearch"; do
echo "Waiting for Elasticsearch to be ready..."
sleep 1
done
- name: Continuous Integration
run: ut ci:postgresql
env:
# The hostname used to communicate with the PostgreSQL service container
POSTGRES_HOST: localhost
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
# The default PostgreSQL port
POSTGRES_PORT: 5432
CNPMCORE_CONFIG_ENABLE_ES: true
CNPMCORE_CONFIG_ES_CLIENT_NODE: http://localhost:9200
- name: Code Coverage
uses: codecov/codecov-action@v6
with:
token: ${{ secrets.CODECOV_TOKEN }}
test-mysql57-fs-nfs:
strategy:
fail-fast: false
matrix:
node-version: [22, 24]
os: [ubuntu-latest]
enableJSONBuilder: [true, false]
name: test on mysql (node@${{ matrix.node-version }}, enableJSONBuilder@${{ matrix.enableJSONBuilder }})
concurrency:
group: test-mysql57-fs-nfs-${{ github.workflow }}-#${{ github.event.pull_request.number || github.head_ref || github.ref }}-${{ matrix.node-version }}-${{ matrix.enableJSONBuilder }}
cancel-in-progress: true
runs-on: ${{ matrix.os }}
services:
mysql:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: true
MYSQL_DATABASE: cnpmcore_unittest
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5
steps:
- name: Checkout Git Source
uses: actions/checkout@v6
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v6
with:
node-version: ${{ matrix.node-version }}
- name: setup utoo
uses: utooland/setup-utoo@v1
- name: Install Dependencies
run: ut
- name: Continuous Integration
run: ut ci
env:
CNPMCORE_CONFIG_ENABLE_JSON_BUILDER: ${{ matrix.enableJSONBuilder }}
- name: Code Coverage
uses: codecov/codecov-action@v6
with:
token: ${{ secrets.CODECOV_TOKEN }}
test-mysql57-s3-nfs:
if: ${{ github.ref_name == 'master' }}
strategy:
fail-fast: false
matrix:
node-version: [22, 24]
os: [ubuntu-latest]
concurrency:
group: test-mysql57-s3-nfs-${{ github.workflow }}-#${{ github.event.pull_request.number || github.head_ref || github.ref }}-${{ matrix.node-version }}
cancel-in-progress: true
runs-on: ${{ matrix.os }}
services:
mysql:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: true
MYSQL_DATABASE: cnpmcore_unittest
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5
steps:
- name: Checkout Git Source
uses: actions/checkout@v6
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v6
with:
node-version: ${{ matrix.node-version }}
- name: setup utoo
uses: utooland/setup-utoo@v1
- name: Install Dependencies
run: ut
- name: Continuous Integration
run: ut ci "test/cli/npm/install.test.ts"
env:
CNPMCORE_NFS_TYPE: s3
CNPMCORE_NFS_REMOVE_BEFORE_UPLOAD: true
CNPMCORE_NFS_S3_CLIENT_BUCKET: cnpmcore-unittest-github-nodejs-${{ matrix.node-version }}
CNPMCORE_NFS_S3_CLIENT_ENDPOINT: ${{ secrets.CNPMCORE_NFS_S3_ENDPOINT }}
CNPMCORE_NFS_S3_CLIENT_ID: ${{ secrets.CNPMCORE_NFS_S3_ID }}
CNPMCORE_NFS_S3_CLIENT_SECRET: ${{ secrets.CNPMCORE_NFS_S3_SECRET }}
CNPMCORE_NFS_S3_CLIENT_FORCE_PATH_STYLE: true
# CNPMCORE_NFS_S3_CLIENT_DISABLE_URL: true
- name: Code Coverage
uses: codecov/codecov-action@v6
with:
token: ${{ secrets.CODECOV_TOKEN }}
done:
runs-on: ubuntu-latest
needs:
- test-postgresql-fs-nfs
- test-mysql57-fs-nfs
- typecheck
steps:
- run: exit 1
if: ${{ always() && (contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')) }}