@infly/libs 2.3.0 → 2.4.0
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/README.md +10 -0
- package/build/docker/minimal-context.js +9 -1
- package/build/docker/target-release.js +104 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -447,3 +447,13 @@ infly-libs docker:bundle --target demo --dry-run
|
|
|
447
447
|
`release:docker` 不是强制构建入口。独立源码仓可以直接使用 Dockerfile 或 Compose 构建,但若镜像要进入这里生成的部署流程,必须自行保证仓库名、不可变完整 40 位提交标签以及 `org.opencontainers.image.revision`、`org.opencontainers.image.version`、`com.infly.platform` 标签与目标配置一致;公共发布入口只是自动化这些校验和映射。部署侧仅为既有镜像保留旧 12 位标签兼容,新构建不再生成 12 位标签。
|
|
448
448
|
|
|
449
449
|
远端发布持锁后读取全部组件镜像基线,保存 .release-history;配置 `runtimeEnv` 时先从服务器总环境文件按白名单原子生成应用专用文件,再仅更新传入组件的服务(up -d --no-deps)。首次接入可由发布入口显式传入 `--initialize-baseline`:仅在 `deploy.env` 不存在时,从配置声明的运行中服务读取唯一且仓库匹配的 RepoDigest,保存权限受限的 `.onboarding-backup` 接入证据并原子创建基线;已有基线时拒绝覆盖,未显式初始化时仍要求基线预先存在。失败恢复镜像环境文件、应用专用环境文件及选中服务;回滚验证失败明确报错。不创建总环境文件,不创建/修改数据库,不更新未配置的基础设施。服务器不需要 npm/Node,仍需 Bash、Docker Compose 和 curl。
|
|
450
|
+
|
|
451
|
+
### 部署仓拥有镜像打包规则
|
|
452
|
+
|
|
453
|
+
外部源码组件可声明 `buildStrategy: source`、`dockerfileFromConfig: Dockerfile.web` 和 `contextFiles: [{from: nginx.conf, to: nginx.conf}]`。Dockerfile 和 from 相对部署仓根目录,to 相对业务源码根目录;不能同时声明 dockerfile,不能越界、写 Git 元数据或跟随越界链接。公共工具将部署文件注入临时上下文,不改写业务工作树或冻结 worktree。
|
|
454
|
+
|
|
455
|
+
源码仍按 source.directory/source.branch 冻结。monorepo 保留根公共包/锁文件,并用冻结快照替换业务目录;独立仓直接使用冻结源码。OCI revision 取业务提交,`com.infly.deployment.revision` 取部署仓提交,monorepo 额外包含 workspace revision。配置和源码在推送前再次验证。
|
|
456
|
+
|
|
457
|
+
`source.standalone: true` 可显式使用独立源码上下文,即使业务仓位于 monorepo 中也不带根公共包;适用于已有独立安装契约的紧急构建。target 的 `recordConfigurationRevision: true` 则让未注入文件的外部源码组件也记录并复核部署仓提交。两者均为可选布尔值,默认保持原有行为。
|
|
458
|
+
|
|
459
|
+
组件可声明 `health: {timeoutSeconds: 60, addHost: api:127.0.0.1}`,复用运行镜像的 HEALTHCHECK,失败不推送本组任何镜像。普通 check 仍用于语法或文件检查;不要对后端启动真实业务进程作本地镜像探针。
|
|
@@ -49,6 +49,7 @@ function copyEntry(io, workspaceRoot, contextRoot, relative) {
|
|
|
49
49
|
function prepareMinimalMonorepoContext({
|
|
50
50
|
workspaceRoot,
|
|
51
51
|
projectDir,
|
|
52
|
+
sourceDir,
|
|
52
53
|
io = fs,
|
|
53
54
|
temporaryRoot = os.tmpdir(),
|
|
54
55
|
} = {}) {
|
|
@@ -62,7 +63,14 @@ function prepareMinimalMonorepoContext({
|
|
|
62
63
|
try {
|
|
63
64
|
for (const entry of ROOT_ENTRIES) copyEntry(io, resolvedWorkspace, contextRoot, entry);
|
|
64
65
|
if (!projectRelativeDir.startsWith(`packages${path.sep}`)) {
|
|
65
|
-
|
|
66
|
+
if (sourceDir) {
|
|
67
|
+
// 部署仓冻结业务源码后,以该快照替换业务目录,公共包仍来自已核验的根工作区。
|
|
68
|
+
const destination = path.join(contextRoot, projectRelativeDir);
|
|
69
|
+
io.mkdirSync(path.dirname(destination), { recursive: true });
|
|
70
|
+
io.cpSync(sourceDir, destination, { recursive: true, dereference: false, filter: shouldCopy });
|
|
71
|
+
} else {
|
|
72
|
+
copyEntry(io, resolvedWorkspace, contextRoot, projectRelativeDir);
|
|
73
|
+
}
|
|
66
74
|
}
|
|
67
75
|
return { contextRoot, projectRelativeDir: projectRelativeDir.split(path.sep).join("/") };
|
|
68
76
|
} catch (error) {
|
|
@@ -4,7 +4,7 @@ const path = require("node:path");
|
|
|
4
4
|
const { execFileSync } = require("node:child_process");
|
|
5
5
|
const { EXIT_SELECTION, inputText, selectOption, selectOptions } = require("../../cli/select-option");
|
|
6
6
|
const { composeRelease } = require("./compose-release");
|
|
7
|
-
const { formatCicdLink } = require("./docker-build-push");
|
|
7
|
+
const { formatCicdLink, healthCheckImage } = require("./docker-build-push");
|
|
8
8
|
const { loadDockerConfig } = require("./config-loader");
|
|
9
9
|
const { detectReleaseContext, detectReleaseMode } = require("./release-mode");
|
|
10
10
|
const { prepareMinimalMonorepoContext } = require("./minimal-context");
|
|
@@ -89,6 +89,24 @@ function inside(root, relative) {
|
|
|
89
89
|
return result;
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
+
function confinedPath(root, relative, io = fs) {
|
|
93
|
+
const candidate = inside(root, relative);
|
|
94
|
+
let existing = candidate;
|
|
95
|
+
while (existing !== root && !io.existsSync(existing)) {
|
|
96
|
+
if (io.lstatSync) {
|
|
97
|
+
try {
|
|
98
|
+
if (io.lstatSync(existing).isSymbolicLink()) throw new Error(`无法解析链接路径: ${relative}`);
|
|
99
|
+
} catch (error) { if (error.code !== "ENOENT") throw error; }
|
|
100
|
+
}
|
|
101
|
+
existing = path.dirname(existing);
|
|
102
|
+
}
|
|
103
|
+
if (io.realpathSync && io.existsSync(existing)) {
|
|
104
|
+
const offset = path.relative(io.realpathSync(root), io.realpathSync(existing));
|
|
105
|
+
if (offset === ".." || offset.startsWith(`..${path.sep}`) || path.isAbsolute(offset)) throw new Error(`链接路径越界: ${relative}`);
|
|
106
|
+
}
|
|
107
|
+
return candidate;
|
|
108
|
+
}
|
|
109
|
+
|
|
92
110
|
function normalizeSourceDirectories(value, name) {
|
|
93
111
|
const directories = Array.isArray(value) ? value : [value];
|
|
94
112
|
if (!directories.length || directories.some((item) => typeof item !== "string" || !item.trim())) {
|
|
@@ -129,6 +147,9 @@ function normalizePreBuildCommand(value, target) {
|
|
|
129
147
|
}
|
|
130
148
|
|
|
131
149
|
function validateReleaseProfile(profile, target) {
|
|
150
|
+
if (profile.recordConfigurationRevision != null && typeof profile.recordConfigurationRevision !== "boolean") {
|
|
151
|
+
throw new Error(`${target} recordConfigurationRevision 必须是布尔值`);
|
|
152
|
+
}
|
|
132
153
|
if (profile.pushBranch != null && (typeof profile.pushBranch !== "string"
|
|
133
154
|
|| !SAFE_GIT_NAME.test(profile.pushBranch) || profile.pushBranch.startsWith("-")
|
|
134
155
|
|| profile.pushBranch.includes(".."))) {
|
|
@@ -262,11 +283,35 @@ function validateComponent(component, name, guardedByProfile = false) {
|
|
|
262
283
|
}
|
|
263
284
|
if (component.source) {
|
|
264
285
|
normalizeSourceDirectories(component.source.directory, name);
|
|
286
|
+
if (component.source.standalone != null && typeof component.source.standalone !== "boolean") {
|
|
287
|
+
throw new Error(`${name} source.standalone 必须是布尔值`);
|
|
288
|
+
}
|
|
265
289
|
if (!component.source.branch || component.source.branch.startsWith("-")) {
|
|
266
290
|
throw new Error(`${name} source 必须配置合法 branch`);
|
|
267
291
|
}
|
|
268
292
|
}
|
|
269
293
|
inside("/config", component.dockerfile || "Dockerfile");
|
|
294
|
+
if (component.dockerfileFromConfig != null) {
|
|
295
|
+
inside("/config", component.dockerfileFromConfig);
|
|
296
|
+
if (!component.source || component.buildStrategy !== "source" || component.dockerfile != null) {
|
|
297
|
+
throw new Error(`${name} dockerfileFromConfig 只支持外部源码构建,不能同时配置 dockerfile`);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (component.contextFiles != null) {
|
|
301
|
+
if (!component.source || component.buildStrategy !== "source" || !Array.isArray(component.contextFiles)
|
|
302
|
+
|| !component.contextFiles.length) throw new Error(`${name} contextFiles 只支持外部源码构建的非空列表`);
|
|
303
|
+
const destinations = new Set();
|
|
304
|
+
for (const entry of component.contextFiles) {
|
|
305
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry)
|
|
306
|
+
|| Object.keys(entry).some((key) => !["from", "to"].includes(key))) throw new Error(`${name} contextFiles 仅支持 from/to`);
|
|
307
|
+
inside("/config", entry.from); inside("/config", entry.to);
|
|
308
|
+
const destination = path.normalize(entry.to);
|
|
309
|
+
if (destination === "." || destination.split(path.sep).includes(".git") || destinations.has(destination)) {
|
|
310
|
+
throw new Error(`${name} contextFiles 目标不能为根、Git 元数据或重复路径`);
|
|
311
|
+
}
|
|
312
|
+
destinations.add(destination);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
270
315
|
inside("/config", component.context || ".");
|
|
271
316
|
if (component.target != null && (typeof component.target !== "string" || !/^[a-zA-Z0-9_.-]+$/.test(component.target))) {
|
|
272
317
|
throw new Error(`${name} target 必须是合法 Docker stage`);
|
|
@@ -309,6 +354,16 @@ function validateComponent(component, name, guardedByProfile = false) {
|
|
|
309
354
|
throw new Error(`${name} check 必须配置 entrypoint、args 数组及可选 addHosts 数组`);
|
|
310
355
|
}
|
|
311
356
|
}
|
|
357
|
+
if (component.health != null) {
|
|
358
|
+
const health = component.health;
|
|
359
|
+
if (!health || typeof health !== "object" || Array.isArray(health)
|
|
360
|
+
|| Object.keys(health).some((key) => !["timeoutSeconds", "addHost"].includes(key))
|
|
361
|
+
|| !Number.isInteger(health.timeoutSeconds) || health.timeoutSeconds < 1 || health.timeoutSeconds > 600
|
|
362
|
+
|| (health.addHost != null && (typeof health.addHost !== "string"
|
|
363
|
+
|| !/^[A-Za-z0-9][A-Za-z0-9._-]*:(?:[A-Fa-f0-9:.]+|host-gateway)$/.test(health.addHost)))) {
|
|
364
|
+
throw new Error(`${name} health 必须声明 1 到 600 秒的 timeoutSeconds 及可选 addHost`);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
312
367
|
}
|
|
313
368
|
|
|
314
369
|
function execute(command, args, options = {}) {
|
|
@@ -437,6 +492,14 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
437
492
|
};
|
|
438
493
|
let releaseError;
|
|
439
494
|
try {
|
|
495
|
+
let configuration;
|
|
496
|
+
if (profile.recordConfigurationRevision || plan.some((item) => item.dockerfileFromConfig || item.contextFiles)) {
|
|
497
|
+
const commit = capture("git", ["-C", projectDir, "rev-parse", "HEAD"]).toLowerCase();
|
|
498
|
+
if (!/^[a-f0-9]{40}$/.test(commit)) throw new Error("无法冻结部署配置版本");
|
|
499
|
+
configuration = { sourceDir: projectDir, commit };
|
|
500
|
+
verifyFrozen(configuration, "部署配置");
|
|
501
|
+
workspaces.set(projectDir, configuration);
|
|
502
|
+
}
|
|
440
503
|
// 所有来源先冻结,再开始任何镜像构建。相同仓库和分支只 fetch 一次,
|
|
441
504
|
// worktree 使用完整 SHA,后续远端分支前移不会把同一组镜像拆成不同版本。
|
|
442
505
|
for (const item of plan) {
|
|
@@ -470,8 +533,16 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
470
533
|
throw new Error(`${item.name} HEAD 与 ${expectedRef || "冻结提交"} 不一致,请先核对并 fetch 对应仓库`);
|
|
471
534
|
}
|
|
472
535
|
item.frozen = frozen;
|
|
473
|
-
|
|
474
|
-
|
|
536
|
+
item.releaseContext = item.source
|
|
537
|
+
? ((!item.source.standalone && (item.dockerfileFromConfig || item.contextFiles))
|
|
538
|
+
? (dependencies.detectReleaseContext || detectReleaseContext)({ projectDir: item.source.directory })
|
|
539
|
+
: { mode: "standalone", contextRoot: frozen.sourceDir, projectRelativeDir: "." })
|
|
540
|
+
: releaseContext;
|
|
541
|
+
if (item.source && item.releaseContext.mode === "standalone") {
|
|
542
|
+
item.releaseContext = { ...item.releaseContext, contextRoot: frozen.sourceDir, projectRelativeDir: "." };
|
|
543
|
+
}
|
|
544
|
+
if (item.buildStrategy === "source" && item.releaseContext.mode === "monorepo") {
|
|
545
|
+
const contextRoot = item.releaseContext.contextRoot;
|
|
475
546
|
let workspace = workspaces.get(contextRoot);
|
|
476
547
|
if (!workspace) {
|
|
477
548
|
const revision = capture("git", ["-C", contextRoot, "rev-parse", "HEAD"]).toLowerCase();
|
|
@@ -492,9 +563,7 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
492
563
|
log(`\x1b[0;32m✓ ${sourceLabel}构建分支:${item.source.branch}\x1b[0m`);
|
|
493
564
|
}
|
|
494
565
|
const buildStrategy = item.buildStrategy || "artifact";
|
|
495
|
-
const componentReleaseContext = item.
|
|
496
|
-
? { mode: "standalone", contextRoot: sourceDir, projectRelativeDir: "." }
|
|
497
|
-
: releaseContext;
|
|
566
|
+
const componentReleaseContext = item.releaseContext;
|
|
498
567
|
let context = buildStrategy === "source"
|
|
499
568
|
? componentReleaseContext.contextRoot
|
|
500
569
|
: inside(sourceDir, item.context || ".");
|
|
@@ -505,7 +574,8 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
505
574
|
workspaceRevision = item.workspace.commit;
|
|
506
575
|
const prepared = (dependencies.prepareMinimalMonorepoContext || prepareMinimalMonorepoContext)({
|
|
507
576
|
workspaceRoot: componentReleaseContext.contextRoot,
|
|
508
|
-
projectDir: sourceDir,
|
|
577
|
+
projectDir: item.source ? item.source.directory : sourceDir,
|
|
578
|
+
...(item.source ? { sourceDir } : {}),
|
|
509
579
|
io,
|
|
510
580
|
});
|
|
511
581
|
tempRoot = prepared.contextRoot;
|
|
@@ -514,6 +584,27 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
514
584
|
dockerfile = inside(context, `${prepared.projectRelativeDir}/${item.dockerfile || "Dockerfile"}`);
|
|
515
585
|
log(`最小上下文: ${prepared.projectRelativeDir} + packages + 根锁文件`);
|
|
516
586
|
}
|
|
587
|
+
// 部署仓提供 Dockerfile/运行配置;独立模式也先复制上下文,绝不改写冻结源码。
|
|
588
|
+
if (item.contextFiles) {
|
|
589
|
+
if (!tempRoot) {
|
|
590
|
+
tempRoot = io.mkdtempSync(path.join(os.tmpdir(), "infly-image-context-"));
|
|
591
|
+
resources.push({ tempRoot });
|
|
592
|
+
context = path.join(tempRoot, "context");
|
|
593
|
+
io.cpSync(sourceDir, context, { recursive: true, dereference: false,
|
|
594
|
+
filter: (file) => !path.relative(sourceDir, file).split(path.sep).includes(".git") });
|
|
595
|
+
}
|
|
596
|
+
const destinationRoot = componentReleaseContext.mode === "monorepo"
|
|
597
|
+
? inside(context, componentReleaseContext.projectRelativeDir) : context;
|
|
598
|
+
for (const entry of item.contextFiles) {
|
|
599
|
+
const from = confinedPath(projectDir, entry.from, io);
|
|
600
|
+
const to = confinedPath(destinationRoot, entry.to, io);
|
|
601
|
+
if (!io.existsSync(from)) throw new Error(`缺少部署构建文件: ${entry.from}`);
|
|
602
|
+
io.mkdirSync(path.dirname(to), { recursive: true });
|
|
603
|
+
io.cpSync(from, to, { recursive: true, dereference: true,
|
|
604
|
+
filter: (file) => { confinedPath(projectDir, path.relative(projectDir, file), io); return true; } });
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
if (item.dockerfileFromConfig) dockerfile = confinedPath(projectDir, item.dockerfileFromConfig, io);
|
|
517
608
|
for (const file of item.requiredFiles || []) {
|
|
518
609
|
if (!io.existsSync(inside(sourceDir, file))) throw new Error(`缺少必需构建产物: ${file}`);
|
|
519
610
|
}
|
|
@@ -553,6 +644,7 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
553
644
|
run("docker", [...buildOptions,
|
|
554
645
|
"--label", `org.opencontainers.image.revision=${commit}`, "--label", `org.opencontainers.image.version=${commit}`,
|
|
555
646
|
...(workspaceRevision ? ["--label", `com.infly.workspace.revision=${workspaceRevision}`] : []),
|
|
647
|
+
...(configuration ? ["--label", `com.infly.deployment.revision=${configuration.commit}`] : []),
|
|
556
648
|
"--label", `com.infly.platform=${target}`, "--tag", image,
|
|
557
649
|
...(latestImage ? ["--tag", latestImage] : []), context], { cwd: projectDir });
|
|
558
650
|
if (capture("docker", ["image", "inspect", "--format", '{{ index .Config.Labels "org.opencontainers.image.revision" }}', image]) !== commit) {
|
|
@@ -562,6 +654,11 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
562
654
|
const hosts = (item.check.addHosts || []).flatMap((host) => ["--add-host", host]);
|
|
563
655
|
run("docker", ["run", "--rm", ...hosts, "--entrypoint", item.check.entrypoint, image, ...item.check.args], { cwd: projectDir });
|
|
564
656
|
}
|
|
657
|
+
if (item.health) {
|
|
658
|
+
await (dependencies.healthCheckImage || healthCheckImage)(image, {
|
|
659
|
+
cwd: projectDir, healthTimeout: String(item.health.timeoutSeconds), healthAddHost: item.health.addHost,
|
|
660
|
+
});
|
|
661
|
+
}
|
|
565
662
|
log(`${item.name}_commit=${commit}`);
|
|
566
663
|
log(`${item.name}=${image}`);
|
|
567
664
|
if (latestImage) log(`${item.name}_latest=${latestImage}`);
|