@xcanwin/manyoyo 4.2.0 → 4.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/image-build.js +68 -32
- package/package.json +1 -1
package/lib/image-build.js
CHANGED
|
@@ -253,19 +253,50 @@ function extractBuildArgValues(args) {
|
|
|
253
253
|
return values;
|
|
254
254
|
}
|
|
255
255
|
|
|
256
|
-
function
|
|
256
|
+
function isBuildCapabilityError(error) {
|
|
257
|
+
const combined = [
|
|
258
|
+
error && error.message ? error.message : '',
|
|
259
|
+
error && error.stderr ? String(error.stderr) : '',
|
|
260
|
+
error && error.stdout ? String(error.stdout) : ''
|
|
261
|
+
].join('\n').toLowerCase();
|
|
262
|
+
|
|
263
|
+
const patterns = [
|
|
264
|
+
/unknown flag/,
|
|
265
|
+
/unknown shorthand flag/,
|
|
266
|
+
/unknown option/,
|
|
267
|
+
/unrecognized option/,
|
|
268
|
+
/unknown instruction:\s*"?((case|if|then|fi|for|while|do|done|esac))"?/,
|
|
269
|
+
/buildx .* not available/,
|
|
270
|
+
/buildx .* not found/,
|
|
271
|
+
/buildx .* not enabled/,
|
|
272
|
+
/'buildx' is not a docker command/,
|
|
273
|
+
/the --load option requires buildx/,
|
|
274
|
+
/unsupported.*--load/,
|
|
275
|
+
/does not support.*--load/,
|
|
276
|
+
/driver .* not supported/,
|
|
277
|
+
/no such plugin.*buildx/
|
|
278
|
+
];
|
|
279
|
+
return patterns.some(pattern => pattern.test(combined));
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function buildBuildkitRunArgs(ctx, dockerfilePath, fullImageTag, imageBuildArgs) {
|
|
257
283
|
const dockerfileRelativePath = path.relative(ctx.rootDir, dockerfilePath).split(path.sep).join('/');
|
|
258
284
|
const buildArgs = extractBuildArgValues(imageBuildArgs);
|
|
259
285
|
const args = [
|
|
260
286
|
'run', '--rm', '--privileged',
|
|
287
|
+
'--network', `host`,
|
|
261
288
|
'--volume', `${ctx.rootDir}:/workspace`,
|
|
262
289
|
'--entrypoint', 'buildctl-daemonless.sh',
|
|
263
|
-
'moby/buildkit:latest',
|
|
290
|
+
'docker.io/moby/buildkit:latest',
|
|
264
291
|
'build',
|
|
265
292
|
'--frontend', 'dockerfile.v0',
|
|
266
293
|
'--local', 'context=/workspace',
|
|
267
294
|
'--local', 'dockerfile=/workspace',
|
|
268
|
-
'--opt', `filename=${dockerfileRelativePath}
|
|
295
|
+
'--opt', `filename=${dockerfileRelativePath}`,
|
|
296
|
+
'--opt', `build-arg:HTTPS_PROXY=$HTTPS_PROXY`,
|
|
297
|
+
'--opt', `build-arg:HTTPS_PROXY=$HTTPS_PROXY`,
|
|
298
|
+
'--opt', `build-arg:ALL_PROXY=$ALL_PROXY`,
|
|
299
|
+
'--opt', `build-arg:NO_PROXY=$NO_PROXY`
|
|
269
300
|
];
|
|
270
301
|
|
|
271
302
|
for (const value of buildArgs) {
|
|
@@ -397,48 +428,53 @@ async function buildImage(options = {}) {
|
|
|
397
428
|
'--progress=plain',
|
|
398
429
|
'--no-cache'
|
|
399
430
|
];
|
|
431
|
+
const buildkitRunArgs = buildBuildkitRunArgs(ctx, dockerfilePath, fullImageTag, imageBuildArgs);
|
|
400
432
|
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
if (usePodmanBuildkit) {
|
|
407
|
-
ctx.log(`${ctx.dockerCmd} ${buildkitRunArgs.map(quoteShellArg).join(' ')} | ${ctx.dockerCmd} load\n`);
|
|
408
|
-
} else {
|
|
409
|
-
ctx.log(`${ctx.dockerCmd} ${buildArgs.map(quoteShellArg).join(' ')}\n`);
|
|
433
|
+
function logBuildSuccess() {
|
|
434
|
+
ctx.log(`\n${GREEN}✅ 镜像构建成功: ${fullImageTag}${NC}`);
|
|
435
|
+
ctx.log(`${BLUE}使用镜像:${NC}`);
|
|
436
|
+
ctx.log(` ${ctx.manyoyoName} -n test --in ${ctx.imageName} --iv ${version}-${imageTool} -y c`);
|
|
437
|
+
ctx.pruneDanglingImages();
|
|
410
438
|
}
|
|
411
439
|
|
|
440
|
+
ctx.log(`${BLUE}准备执行命令:${NC}`);
|
|
441
|
+
ctx.log(`${ctx.dockerCmd} ${buildArgs.map(quoteShellArg).join(' ')}\n`);
|
|
442
|
+
|
|
412
443
|
if (!ctx.yesMode) {
|
|
413
444
|
await ctx.askQuestion('❔ 是否继续构建? [ 直接回车=继续, ctrl+c=取消 ]: ');
|
|
414
445
|
ctx.log('');
|
|
415
446
|
}
|
|
416
447
|
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
448
|
+
try {
|
|
449
|
+
ctx.runCmd(ctx.dockerCmd, buildArgs, { stdio: 'inherit' });
|
|
450
|
+
logBuildSuccess();
|
|
451
|
+
return;
|
|
452
|
+
} catch (e) {
|
|
453
|
+
const stderrText = e && e.stderr ? String(e.stderr).trim() : '';
|
|
454
|
+
const stdoutText = e && e.stdout ? String(e.stdout).trim() : '';
|
|
455
|
+
const hasDiagnostics = Boolean(stderrText || stdoutText);
|
|
456
|
+
const capabilityError = isBuildCapabilityError(e);
|
|
457
|
+
|
|
458
|
+
if (!capabilityError && hasDiagnostics) {
|
|
459
|
+
ctx.error(`${RED}错误: 镜像构建失败${NC}`);
|
|
460
|
+
ctx.exit(1);
|
|
424
461
|
return;
|
|
425
|
-
} catch (e) {
|
|
426
|
-
ctx.log(`${YELLOW}⚠️ BuildKit 构建失败,回退到 podman build...${NC}`);
|
|
427
|
-
if (e && e.message) {
|
|
428
|
-
ctx.log(`${YELLOW}原因: ${e.message}${NC}`);
|
|
429
|
-
}
|
|
430
|
-
ctx.log('');
|
|
431
|
-
ctx.log(`${BLUE}回退命令:${NC}`);
|
|
432
|
-
ctx.log(`${ctx.dockerCmd} ${buildArgs.map(quoteShellArg).join(' ')}\n`);
|
|
433
462
|
}
|
|
463
|
+
if (!capabilityError && !hasDiagnostics) {
|
|
464
|
+
ctx.log(`${YELLOW}⚠️ 未捕获到构建器错误详情,尝试回退到 BuildKit...${NC}`);
|
|
465
|
+
}
|
|
466
|
+
ctx.log(`${YELLOW}⚠️ 直接 build 失败,回退到 BuildKit...${NC}`);
|
|
467
|
+
if (e && e.message) {
|
|
468
|
+
ctx.log(`${YELLOW}原因: ${e.message}${NC}`);
|
|
469
|
+
}
|
|
470
|
+
ctx.log('');
|
|
471
|
+
ctx.log(`${BLUE}回退命令:${NC}`);
|
|
472
|
+
ctx.log(`${ctx.dockerCmd} ${buildkitRunArgs.map(quoteShellArg).join(' ')} | ${ctx.dockerCmd} load\n`);
|
|
434
473
|
}
|
|
435
474
|
|
|
436
475
|
try {
|
|
437
|
-
ctx.
|
|
438
|
-
|
|
439
|
-
ctx.log(`${BLUE}使用镜像:${NC}`);
|
|
440
|
-
ctx.log(` ${ctx.manyoyoName} -n test --in ${ctx.imageName} --iv ${version}-${imageTool} -y c`);
|
|
441
|
-
ctx.pruneDanglingImages();
|
|
476
|
+
await ctx.runCmdPipeline(ctx.dockerCmd, buildkitRunArgs, ctx.dockerCmd, ['load'], { stdio: 'inherit' });
|
|
477
|
+
logBuildSuccess();
|
|
442
478
|
} catch (e) {
|
|
443
479
|
ctx.error(`${RED}错误: 镜像构建失败${NC}`);
|
|
444
480
|
ctx.exit(1);
|