@infly/libs 2.1.7 → 2.1.9

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 CHANGED
@@ -273,8 +273,33 @@ Windows 本地构建连接 `dockerDesktopLinuxEngine` 失败时,入口尝试
273
273
  "releaseScripts": {
274
274
  "backend": "release:backend:docker"
275
275
  },
276
+ "targetPresets": {
277
+ "custom": {
278
+ "components": {
279
+ "backend": {
280
+ "dockerfile": "Dockerfile",
281
+ "context": ".",
282
+ "imageEnv": "API_IMAGE",
283
+ "services": ["api", "worker"]
284
+ }
285
+ },
286
+ "deployment": {
287
+ "releaseComposeFile": "deploy/compose.release.yml",
288
+ "runtimeEnv": {
289
+ "sourceFile": ".env",
290
+ "targetFile": "runtime/app.env",
291
+ "variables": [
292
+ { "name": "APP_SECRET" },
293
+ { "name": "APP_REGION_PROD", "sources": ["APP_REGION_PROD", "APP_REGION"] },
294
+ { "name": "APP_OPTIONAL", "optional": true }
295
+ ]
296
+ }
297
+ }
298
+ }
299
+ },
276
300
  "targets": {
277
301
  "demo": {
302
+ "extends": "custom",
278
303
  "label": "示例平台",
279
304
  "components": {
280
305
  "frontend": {
@@ -291,17 +316,12 @@ Windows 本地构建连接 `dockerDesktopLinuxEngine` 失败时,入口尝试
291
316
  "services": ["web"]
292
317
  },
293
318
  "backend": {
294
- "label": "后端",
295
319
  "repository": "registry.example.com/demo/api",
296
320
  "source": {
297
321
  "directory": "../api",
298
322
  "url": "https://example.com/api.git",
299
323
  "branch": "release/demo"
300
- },
301
- "dockerfile": "Dockerfile",
302
- "context": ".",
303
- "imageEnv": "API_IMAGE",
304
- "services": ["api", "worker"]
324
+ }
305
325
  }
306
326
  },
307
327
  "deployment": {
@@ -312,7 +332,6 @@ Windows 本地构建连接 `dockerDesktopLinuxEngine` 失败时,入口尝试
312
332
  "composeFile": "compose.yml",
313
333
  "secretEnvFile": ".env",
314
334
  "releaseEnvFile": "deploy.env",
315
- "releaseComposeFile": "deploy/compose.release.yml",
316
335
  "health": { "urls": ["https://example.com/"] }
317
336
  }
318
337
  }
@@ -325,10 +344,12 @@ Windows 本地构建连接 `dockerDesktopLinuxEngine` 失败时,入口尝试
325
344
  - `files`:从组件源码仓复制到独立临时上下文,仅发送指定产物;配置后 `dockerfile` 相对临时上下文。未配置时 `context`、`dockerfile` 相对组件源码仓。
326
345
  - `requiredFiles`:构建前必须存在的源码仓相对路径,例如前端产物的 `index.html`;缺失时停止构建。
327
346
  - `source`:可选独立源码仓;从项目目录解析 `directory`,fetch 后使用指定分支的临时 detached worktree,不切换当前开发分支。缺少源码仓时明确说明未执行构建或推送,打印编号化的 clone、fetch 和重跑步骤,全部构建停止,CLI 返回 2。
347
+ - `targetPresets` 与 `targets.*.extends`:可选的具名公共目标配置。preset 可以继续 `extends` 另一个 preset;目标只继承显式指定的 preset,未声明 `extends` 的历史目标不受影响。同名普通对象递归合并;数组、字符串、数字、布尔值和 `null` 由下层整项替换,不按数组下标混合。未知 preset 或循环继承直接失败。本地构建、Compose 发布和部署包生成共用同一解析规则。
328
348
  - `releaseScripts`:可选的组件重跑脚本映射。源码仓缺失时优先提示 `pnpm run <脚本> -- --platform ...`;脚本名只允许字母、数字、冒号、点、下划线和连字符。未配置时提示通用 `pnpm exec infly-libs docker:release` 命令。
329
349
  - 无 `source` 时构建当前项目,要求干净工作树且 HEAD 等于 `expectedRef`。有 `source` 时校验临时工作树对应的 origin 分支。
330
350
  - `check`:可选镜像内校验,使用 `entrypoint`、`args` 数组及 `addHosts` 数组;失败不推送。配置命令具有代码执行权限,应与源码一样评审,不能来自不可信输入。
331
351
  - `deployment`:仅用于部署包生成;`ready=false` 会阻止生成。可设置 `lockName`,默认 `.infly-release.lock`。服务归属和 imageEnv 必须唯一。
352
+ - `runtimeEnv`:可选的服务器运行时环境文件白名单。`sourceFile` 必须是部署目录内已存在的总环境文件,公共脚本不会创建它,并在校验通过后收紧为 600 权限;`targetFile` 是自动生成的应用专用文件。`variables` 默认必需且按 `name` 输出,可用 `sources` 声明按顺序读取的候选源变量,用 `optional=true` 声明可选项。源文件不存在、不是普通文件、必需变量缺失/为空/重复时,在拉取或重建容器前失败;生成使用临时文件原子替换并设置 600 权限。失败时恢复已有专用文件;若这是首次生成且容器已经开始重建,则保留新文件,保证旧镜像可继续通过同一挂载路径回滚。脚本不执行 `source`,也不输出变量值。
332
353
  - `health`:容器运行检查、可选 `exec: [{service, command: [...]}]`,以及 HTTP URL 重试;默认 attempts=12、intervalSeconds=5、timeoutSeconds=20。不要配置迁移、业务写入或数据库命令。
333
354
  - 路径、分支、镜像、服务、健康检查由项目配置决定;公共包不内置项目映射。Dockerfile、Compose、Nginx 由项目保留。
334
355
 
@@ -344,6 +365,6 @@ infly-libs docker:bundle --target demo --dry-run
344
365
 
345
366
  构建成功后逐组件输出完整提交版本、`组件=仓库:<12位提交版本>` 和 `组件_latest=仓库:latest` 机器行;每个组件的一次构建同时生成不可变提交标签与 `latest`,启用推送时先推送不可变标签,再更新 `latest`。全部构建结束后打印 `✅ 完成` 汇总块,明确区分“不可变提交版本”和“最新构建镜像”。可选 `--cicd-url`(支持等号和空格两种传参形式)在推送成功后于汇总块尾输出蓝色发布系统链接;“发布系统:”与中文 URL 分为两行,URL 独占一行,OSC 8 显式超链接使用百分号编码后的标准 URL 作为点击目标,确保支持该协议的终端可通过 Ctrl + 鼠标左键完整打开。不传时保持原输出。链接必须是合法 HTTP/HTTPS URL,不含用户名或密码。
346
367
 
347
- `docker:bundle` 只生成本地脚本包,不自动部署;输出须是项目内尚不存在的独立子目录。包内包含 config.sh、Compose 覆盖文件和公共 shell 脚本。Jenkins 固定公共包版本后生成,使用既有凭据,调用 `deploy.sh component=repository:commit` 传输并执行。包内仍保留 `promote-image.sh` 供既有 qc 晋升流程兼容使用;低频定制平台直接在本地推送不可变提交标签与 `latest`,Jenkins 不再调用晋升脚本。运行部署脚本会操作服务器,必须由开发者手动触发既定任务。
368
+ `docker:bundle` 只生成本地脚本包,不自动部署;输出须是项目内尚不存在的独立子目录。包内包含 config.sh、Compose 覆盖文件和公共 shell 脚本。Jenkins 可直接传入部署项目 `package.json` 声明的精确版本或 caret 范围获取公共包;`docker:bundle` 会在执行时输出实际解析到的 `@infly/libs` 精确版本,供 Jenkins 日志追溯。镜像不需要 Infly 专用版本标签,因此独立项目使用普通 `docker build` 生成的镜像仍可部署。Jenkins 使用既有凭据,调用 `deploy.sh component=repository:commit` 传输并执行。包内仍保留 `promote-image.sh` 供既有 qc 晋升流程兼容使用;低频定制平台直接在本地推送不可变提交标签与 `latest`,Jenkins 不再调用晋升脚本。运行部署脚本会操作服务器,必须由开发者手动触发既定任务。
348
369
 
349
- 远端发布持锁后读取全部组件镜像基线,保存 .release-history,仅更新传入组件的服务(up -d --no-deps)。失败恢复镜像环境文件及选中服务;回滚验证失败明确报错。不创建/修改数据库,不更新未配置的基础设施。服务器不需要 npm/Node,仍需 Bash、Docker Compose 和 curl。
370
+ 远端发布持锁后读取全部组件镜像基线,保存 .release-history;配置 `runtimeEnv` 时先从服务器总环境文件按白名单原子生成应用专用文件,再仅更新传入组件的服务(up -d --no-deps)。失败恢复镜像环境文件、应用专用环境文件及选中服务;回滚验证失败明确报错。不创建总环境文件,不创建/修改数据库,不更新未配置的基础设施。服务器不需要 npm/Node,仍需 Bash、Docker Compose 和 curl。
package/bin/cli.js CHANGED
@@ -92,8 +92,12 @@ function parseArgs(argv) {
92
92
  function parseProjectArgs(argv) {
93
93
  const options = parseArgs(argv);
94
94
  const hasExplicitEnv = argv.some((arg) => arg === "--env" || arg.startsWith("--env="));
95
+ const hasNoWebhook = argv.some((arg) =>
96
+ ["--nowebhook", "--noWebhook", "--no-webhook"].includes(arg)
97
+ );
95
98
  delete options.local;
96
99
  if (!hasExplicitEnv) delete options.env;
100
+ if (hasNoWebhook) options.noWebhook = true;
97
101
  return options;
98
102
  }
99
103
  async function main(argv = process.argv.slice(2)) {
@@ -102,6 +106,7 @@ async function main(argv = process.argv.slice(2)) {
102
106
  "docker:bundle": () => {
103
107
  const { parseReleaseArgs } = require("../build/docker/target-release");
104
108
  const result = require("../build/docker/deployment-bundle").createDeploymentBundle(parseReleaseArgs(argv.slice(1)));
109
+ console.log(`部署工具实际版本: @infly/libs@${require("../package.json").version}`);
105
110
  console.log(JSON.stringify(result, null, 2));
106
111
  return result;
107
112
  },
@@ -6,6 +6,7 @@ const {
6
6
  selectOption: defaultSelectOption,
7
7
  } = require("../../cli/select-option");
8
8
  const { composeRelease: defaultComposeRelease } = require("./compose-release");
9
+ const { resolveDockerConfig } = require("./target-config");
9
10
 
10
11
  function loadTargetConfig(configPath) {
11
12
  const resolvedPath = path.resolve(configPath);
@@ -41,7 +42,8 @@ async function runComposeCommand(options = {}, dependencies = {}) {
41
42
  : options.configObject
42
43
  ? { config: options.configObject, configDir: options.configDir || process.cwd() }
43
44
  : loadPackageTargetConfig(options.cwd || process.cwd());
44
- const targets = loaded.config?.targets;
45
+ const config = resolveDockerConfig(loaded.config);
46
+ const targets = config?.targets;
45
47
  if (!targets || Object.keys(targets).length === 0) {
46
48
  throw new Error("Docker target config must define at least one target.");
47
49
  }
@@ -10,7 +10,31 @@ done
10
10
  # 先持有目录锁再读基线,避免前后端两个任务同时发布时使用过期基线。
11
11
  mkdir "$lock_dir" 2>/dev/null || { echo "已有发布任务持有锁: $project_dir/$lock_dir" >&2; exit 1; }
12
12
  next_env=""
13
- cleanup() { [[ -z "$next_env" ]] || rm -f -- "$next_env"; rmdir "$lock_dir" 2>/dev/null || true; }
13
+ runtime_env_backup=""
14
+ runtime_env_changed=false
15
+ runtime_env_had_previous=false
16
+ deployment_started=false
17
+ release_complete=false
18
+ restore_runtime_env() {
19
+ [[ "$runtime_env_changed" == true ]] || return 0
20
+ if [[ "$runtime_env_had_previous" == true ]]; then
21
+ cp -- "$runtime_env_backup" "$runtime_env_target" && chmod 600 "$runtime_env_target"
22
+ elif [[ "$deployment_started" == true ]]; then
23
+ # 首次生成时旧镜像的回滚 Compose 仍引用该挂载文件,保留它才能完成服务恢复。
24
+ echo "首次生成的运行时环境文件已保留用于回滚: $project_dir/$runtime_env_target" >&2
25
+ else
26
+ rm -f -- "$runtime_env_target"
27
+ fi
28
+ runtime_env_changed=false
29
+ }
30
+ cleanup() {
31
+ local status=$?
32
+ if [[ "$release_complete" != true ]]; then restore_runtime_env || true; fi
33
+ [[ -z "$next_env" ]] || rm -f -- "$next_env"
34
+ [[ -z "$runtime_env_backup" ]] || rm -f -- "$runtime_env_backup"
35
+ rmdir "$lock_dir" 2>/dev/null || true
36
+ return "$status"
37
+ }
14
38
  trap cleanup EXIT
15
39
  current_images=()
16
40
  next_images=()
@@ -52,6 +76,54 @@ chmod 600 "$previous_env"
52
76
  next_env=$(mktemp "${release_env_file}.next.XXXXXX")
53
77
  # 发布环境文件仅保存配置声明的镜像,敏感值仍由独立环境文件提供。
54
78
  for i in "${!image_keys[@]}"; do printf '%s=%s\n' "${image_keys[$i]}" "${next_images[$i]}"; done > "$next_env"
79
+ prepare_runtime_env() {
80
+ [[ "$runtime_env_enabled" == true ]] || return 0
81
+ [[ -f "$runtime_env_source" && ! -L "$runtime_env_source" ]] || { echo "部署总环境文件不存在或不是普通文件: $project_dir/$runtime_env_source" >&2; return 1; }
82
+ chmod 600 -- "$runtime_env_source"
83
+ local target_dir next_runtime_env group candidate count line value found
84
+ target_dir=$(dirname -- "$runtime_env_target")
85
+ if [[ "$target_dir" != "." ]]; then
86
+ if [[ ! -e "$target_dir" ]]; then
87
+ mkdir -p -- "$target_dir"
88
+ chmod 700 -- "$target_dir"
89
+ fi
90
+ [[ -d "$target_dir" && ! -L "$target_dir" ]] || { echo "运行时环境目录无效: $project_dir/$target_dir" >&2; return 1; }
91
+ fi
92
+ if [[ -e "$runtime_env_target" ]]; then
93
+ [[ -f "$runtime_env_target" && ! -L "$runtime_env_target" ]] || { echo "运行时环境文件不是普通文件: $project_dir/$runtime_env_target" >&2; return 1; }
94
+ runtime_env_backup=$(mktemp "${runtime_env_target}.previous.XXXXXX")
95
+ cp -- "$runtime_env_target" "$runtime_env_backup"
96
+ chmod 600 "$runtime_env_backup"
97
+ runtime_env_had_previous=true
98
+ fi
99
+ next_runtime_env=$(mktemp "${runtime_env_target}.next.XXXXXX")
100
+ for i in "${!runtime_env_names[@]}"; do
101
+ found=false
102
+ read -r -a group <<< "${runtime_env_source_groups[$i]}"
103
+ for candidate in "${group[@]}"; do
104
+ count=$(grep -c "^${candidate}=" "$runtime_env_source" || true)
105
+ [[ "$count" -le 1 ]] || { echo "部署总环境文件中的变量不唯一: $candidate" >&2; rm -f -- "$next_runtime_env"; return 1; }
106
+ [[ "$count" == 1 ]] || continue
107
+ line=$(grep "^${candidate}=" "$runtime_env_source")
108
+ line=${line%$'\r'}
109
+ value=${line#*=}
110
+ [[ -n "$value" ]] || { echo "部署总环境文件中的变量为空: $candidate" >&2; rm -f -- "$next_runtime_env"; return 1; }
111
+ printf '%s=%s\n' "${runtime_env_names[$i]}" "$value" >> "$next_runtime_env"
112
+ found=true
113
+ break
114
+ done
115
+ if [[ "$found" != true && "${runtime_env_required[$i]}" == true ]]; then
116
+ echo "部署总环境文件缺少必需变量: ${runtime_env_names[$i]}" >&2
117
+ rm -f -- "$next_runtime_env"
118
+ return 1
119
+ fi
120
+ done
121
+ chmod 600 "$next_runtime_env"
122
+ runtime_env_changed=true
123
+ mv -f -- "$next_runtime_env" "$runtime_env_target"
124
+ export RUNTIME_ENV_FILE="./$runtime_env_target"
125
+ }
126
+ prepare_runtime_env
55
127
  compose=(docker compose --env-file "$secret_env_file" --env-file "$next_env" -f "$compose_file" -f "$bundle_dir/docker-compose.release.yml")
56
128
  "${compose[@]}" config --quiet
57
129
  "${compose[@]}" pull "${selected_services[@]}"
@@ -67,13 +139,16 @@ rollback() {
67
139
  echo "发布失败,恢复基线: $previous_env" >&2
68
140
  cp -- "$previous_env" "$release_env_file"
69
141
  chmod 600 "$release_env_file"
142
+ restore_runtime_env
70
143
  if ! "${compose[@]}" up -d --no-deps "${selected_services[@]}" \
71
144
  || ! run_post_deploy "${selected_components[@]}" \
72
145
  || ! bash "$bundle_dir/health-check.sh"; then
73
146
  echo "自动回滚未通过验证,需人工恢复: $project_dir/$previous_env" >&2
74
147
  fi
75
148
  }
149
+ deployment_started=true
76
150
  if ! "${compose[@]}" up -d --no-deps "${selected_services[@]}"; then rollback; exit 1; fi
77
151
  if ! run_post_deploy "${selected_components[@]}"; then rollback; exit 1; fi
78
152
  if ! bash "$bundle_dir/health-check.sh"; then rollback; exit 1; fi
153
+ release_complete=true
79
154
  echo "发布成功: $target; 上一版本记录: $project_dir/$previous_env"
@@ -1,5 +1,6 @@
1
1
  const fs = require("node:fs");
2
2
  const path = require("node:path");
3
+ const { resolveDockerConfig } = require("./target-config");
3
4
 
4
5
  const quote = (value) => "'" + String(value).replaceAll("'", "'\\''") + "'";
5
6
  const array = (name, values) => `${name}=(${values.map(quote).join(" ")})`;
@@ -8,7 +9,43 @@ function required(value, name, pattern) {
8
9
  return value;
9
10
  }
10
11
 
12
+ function deploymentPath(value, name) {
13
+ const result = required(value, name, /^[a-zA-Z0-9_./-]+$/);
14
+ if (result.startsWith("/") || result.split("/").includes("..")) throw new Error(`${name} 必须位于部署目录内`);
15
+ return result;
16
+ }
17
+
18
+ function normalizeRuntimeEnv(deploy) {
19
+ if (deploy.runtimeEnv == null) return null;
20
+ const runtimeEnv = deploy.runtimeEnv;
21
+ if (!runtimeEnv || !Array.isArray(runtimeEnv.variables) || !runtimeEnv.variables.length) {
22
+ throw new Error("runtimeEnv.variables 必须是非空数组");
23
+ }
24
+ const sourceFile = deploymentPath(runtimeEnv.sourceFile, "runtimeEnv.sourceFile");
25
+ const targetFile = deploymentPath(runtimeEnv.targetFile, "runtimeEnv.targetFile");
26
+ if (sourceFile === targetFile) throw new Error("runtimeEnv 源文件和目标文件不能相同");
27
+ const variables = runtimeEnv.variables.map((variable, index) => {
28
+ if (!variable || typeof variable !== "object" || Array.isArray(variable)) {
29
+ throw new Error(`runtimeEnv.variables[${index}] 配置无效`);
30
+ }
31
+ const name = required(variable.name, `runtimeEnv.variables[${index}].name`, /^[A-Z][A-Z0-9_]*$/);
32
+ const sources = variable.sources == null ? [name] : variable.sources;
33
+ if (!Array.isArray(sources) || !sources.length) throw new Error(`runtimeEnv.variables[${index}].sources 配置无效`);
34
+ for (const source of sources) required(source, `runtimeEnv.variables[${index}].sources`, /^[A-Z][A-Z0-9_]*$/);
35
+ if (new Set(sources).size !== sources.length) throw new Error(`runtimeEnv.variables[${index}].sources 不得重复`);
36
+ if (variable.optional != null && typeof variable.optional !== "boolean") {
37
+ throw new Error(`runtimeEnv.variables[${index}].optional 必须是布尔值`);
38
+ }
39
+ return { name, sources, required: variable.optional !== true };
40
+ });
41
+ if (new Set(variables.map((variable) => variable.name)).size !== variables.length) {
42
+ throw new Error("runtimeEnv 目标变量不得重复");
43
+ }
44
+ return { sourceFile, targetFile, variables };
45
+ }
46
+
11
47
  function renderDeploymentConfig(config, target) {
48
+ config = resolveDockerConfig(config);
12
49
  const profile = config?.targets?.[target];
13
50
  const deploy = profile?.deployment;
14
51
  if (!deploy || deploy.ready !== true) throw new Error(`平台 ${target} 尚未完成人工服务器核验,禁止生产发布`);
@@ -37,21 +74,28 @@ function renderDeploymentConfig(config, target) {
37
74
  }
38
75
  }
39
76
  }
77
+ const runtimeEnv = normalizeRuntimeEnv(deploy);
40
78
  const lines = ["# 由 @infly/libs 根据项目配置生成;只含非敏感参数。",
41
79
  `target=${quote(required(target, "target", /^[a-zA-Z0-9_-]+$/))}`,
42
80
  `registry=${quote(required(config.registry, "registry", /^[a-zA-Z0-9.:-]+$/))}`,
43
81
  `server=${quote(required(deploy.server, "server", /^[a-zA-Z0-9][a-zA-Z0-9.-]*$/))}`,
44
82
  `ssh_user=${quote(required(deploy.sshUser, "sshUser", /^[a-z_][a-z0-9_-]*$/))}`,
45
83
  `project_dir=${quote(required(deploy.directory, "directory", /^\/(?!$)[a-zA-Z0-9_./-]+$/))}`,
46
- ...[["compose_file", "composeFile"], ["secret_env_file", "secretEnvFile"], ["release_env_file", "releaseEnvFile"]].map(([shell, json]) => {
47
- const value = required(deploy[json], json, /^[a-zA-Z0-9_./-]+$/);
48
- if (value.startsWith("/") || value.split("/").includes("..")) throw new Error(`${json} 必须位于部署目录内`);
49
- return `${shell}=${quote(value)}`;
50
- }),
84
+ ...[["compose_file", "composeFile"], ["secret_env_file", "secretEnvFile"], ["release_env_file", "releaseEnvFile"]]
85
+ .map(([shell, json]) => `${shell}=${quote(deploymentPath(deploy[json], json))}`),
51
86
  `lock_dir=${quote(required(deploy.lockName || ".infly-release.lock", "lockName", /^\.[a-zA-Z0-9_-]+(?:\.[a-zA-Z0-9_-]+)*$/))}`,
52
87
  array("component_names", components.map(([name]) => name)), array("repositories", repositories),
53
88
  array("image_keys", keys), array("service_groups", serviceGroups), array("health_services", allServices),
54
89
  ];
90
+ lines.push(`runtime_env_enabled=${quote(runtimeEnv ? "true" : "false")}`);
91
+ if (runtimeEnv) {
92
+ lines.push(`runtime_env_source=${quote(runtimeEnv.sourceFile)}`, `runtime_env_target=${quote(runtimeEnv.targetFile)}`,
93
+ array("runtime_env_names", runtimeEnv.variables.map((variable) => variable.name)),
94
+ array("runtime_env_source_groups", runtimeEnv.variables.map((variable) => variable.sources.join(" "))),
95
+ array("runtime_env_required", runtimeEnv.variables.map((variable) => String(variable.required))));
96
+ } else {
97
+ lines.push("runtime_env_source=''", "runtime_env_target=''", "runtime_env_names=()", "runtime_env_source_groups=()", "runtime_env_required=()");
98
+ }
55
99
  lines.push("run_post_deploy() {", " local component", " for component in \"$@\"; do", " case \"$component\" in");
56
100
  for (let i = 0; i < components.length; i++) {
57
101
  lines.push(` ${quote(components[i][0])})`);
@@ -81,9 +125,10 @@ function renderDeploymentConfig(config, target) {
81
125
 
82
126
  function createDeploymentBundle(options = {}) {
83
127
  const projectDir = path.resolve(options.projectDir || process.cwd());
84
- const config = options.configObject || (options.config
128
+ const rawConfig = options.configObject || (options.config
85
129
  ? JSON.parse(fs.readFileSync(path.resolve(projectDir, options.config), "utf8"))
86
130
  : JSON.parse(fs.readFileSync(path.join(projectDir, "package.json"), "utf8")).infly?.docker);
131
+ const config = resolveDockerConfig(rawConfig);
87
132
  const rendered = renderDeploymentConfig(config, options.target);
88
133
  if (options.dryRun) return { target: options.target, config: rendered };
89
134
  const output = path.resolve(projectDir, options.output || ".infly-deploy");
@@ -0,0 +1,50 @@
1
+ function isPlainObject(value) {
2
+ return value != null && typeof value === "object" && !Array.isArray(value);
3
+ }
4
+
5
+ function clone(value) {
6
+ if (Array.isArray(value)) return value.map(clone);
7
+ if (isPlainObject(value)) return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, clone(item)]));
8
+ return value;
9
+ }
10
+
11
+ function mergeTargetConfig(base, override) {
12
+ if (!isPlainObject(base) || !isPlainObject(override)) return clone(override);
13
+ const result = clone(base);
14
+ for (const [key, value] of Object.entries(override)) {
15
+ result[key] = isPlainObject(value) && isPlainObject(result[key])
16
+ ? mergeTargetConfig(result[key], value)
17
+ : clone(value);
18
+ }
19
+ return result;
20
+ }
21
+
22
+ function resolveDockerConfig(config) {
23
+ if (!isPlainObject(config)) return config;
24
+ const presets = config.targetPresets || {};
25
+ if (!isPlainObject(presets)) throw new Error("infly.docker.targetPresets 必须是对象");
26
+ const resolvedPresets = new Map();
27
+ const resolvePreset = (name, chain = []) => {
28
+ if (resolvedPresets.has(name)) return resolvedPresets.get(name);
29
+ if (!Object.hasOwn(presets, name) || !isPlainObject(presets[name])) throw new Error(`未知 Docker targetPreset: ${name}`);
30
+ if (chain.includes(name)) throw new Error(`Docker targetPreset 循环继承: ${[...chain, name].join(" -> ")}`);
31
+ const preset = presets[name];
32
+ const parent = preset.extends == null ? {} : resolvePreset(preset.extends, [...chain, name]);
33
+ const own = { ...preset };
34
+ delete own.extends;
35
+ const resolved = mergeTargetConfig(parent, own);
36
+ resolvedPresets.set(name, resolved);
37
+ return resolved;
38
+ };
39
+ const targets = {};
40
+ for (const [name, target] of Object.entries(config.targets || {})) {
41
+ if (!isPlainObject(target)) throw new Error(`Docker target 配置无效: ${name}`);
42
+ const base = target.extends == null ? {} : resolvePreset(target.extends);
43
+ const own = { ...target };
44
+ delete own.extends;
45
+ targets[name] = mergeTargetConfig(base, own);
46
+ }
47
+ return { ...config, targets };
48
+ }
49
+
50
+ module.exports = { mergeTargetConfig, resolveDockerConfig };
@@ -5,6 +5,7 @@ const { execFileSync } = require("node:child_process");
5
5
  const { EXIT_SELECTION, selectOption, selectOptions } = require("../../cli/select-option");
6
6
  const { composeRelease } = require("./compose-release");
7
7
  const { formatCicdLink } = require("./docker-build-push");
8
+ const { resolveDockerConfig } = require("./target-config");
8
9
 
9
10
  async function checkDocker(run, projectDir, buildx = true, dependencies = {}, autoStart = true) {
10
11
  const quiet = { cwd: projectDir, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] };
@@ -111,9 +112,10 @@ function execute(command, args, options = {}) {
111
112
 
112
113
  async function runTargetRelease(options = {}, dependencies = {}) {
113
114
  const projectDir = path.resolve(options.projectDir || process.cwd());
114
- const config = options.configObject || (options.config
115
+ const rawConfig = options.configObject || (options.config
115
116
  ? JSON.parse(fs.readFileSync(path.resolve(projectDir, options.config), "utf8"))
116
117
  : JSON.parse(fs.readFileSync(path.join(projectDir, "package.json"), "utf8")).infly?.docker);
118
+ const config = resolveDockerConfig(rawConfig);
117
119
  const targets = config?.targets;
118
120
  if (!targets || !Object.keys(targets).length) throw new Error("请配置 infly.docker.targets");
119
121
  const releaseScripts = config?.releaseScripts || {};
@@ -268,7 +268,13 @@ async function runProjectCommand(options = {}, dependencies = {}) {
268
268
  const steps = [
269
269
  { command: buildConfig.before, extraArgs: lifecycleArgs },
270
270
  { command: buildConfig.command, extraArgs: buildArgs },
271
- { command: buildConfig.after, extraArgs: lifecycleArgs },
271
+ {
272
+ command: buildConfig.after,
273
+ extraArgs: [
274
+ ...lifecycleArgs,
275
+ ...(options.noWebhook ? ["--nowebhook"] : []),
276
+ ],
277
+ },
272
278
  ];
273
279
 
274
280
  for (const step of steps) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infly/libs",
3
- "version": "2.1.7",
3
+ "version": "2.1.9",
4
4
  "description": "不受前端框架限制的独立工具库",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -59,7 +59,7 @@
59
59
  "author": "Kahal",
60
60
  "license": "ISC",
61
61
  "dependencies": {
62
- "@infly/ts-libs": "^1.0.0",
62
+ "@infly/ts-libs": "^1.0.1",
63
63
  "connect": "^3.7.0",
64
64
  "dotenv": "16.6.1",
65
65
  "enquirer": "^2.4.1",
@@ -516,13 +516,19 @@ function checkAndUpdateRootPackages(packagesDir, targetBranch = "master") {
516
516
  console.log(global.logColor.info, `🔄 保持当前分支 ${currentBranch},合并 ${targetBranch} 的最新更改...`);
517
517
 
518
518
  // 拉取远程最新的 targetBranch
519
- execSync(`git fetch origin ${targetBranch}`, {
520
- cwd: rootDir,
521
- stdio: "inherit"
522
- });
523
-
524
-
525
- console.log(global.logColor.success, `✅ admin-monorepo[${currentBranch}]: 已合并 ${targetBranch} 的最新更改到 packages、docs 等目录`);
519
+ execSync(`git fetch origin ${targetBranch}`, {
520
+ cwd: rootDir,
521
+ stdio: "inherit"
522
+ });
523
+
524
+ // packages 是主仓普通目录,必须让当前 master 快进到远端提交后再开始构建。
525
+ // 使用 --ff-only,分支出现分叉时直接失败,避免构建流程自动制造合并提交。
526
+ execSync(`git merge --ff-only origin/${targetBranch}`, {
527
+ cwd: rootDir,
528
+ stdio: "inherit"
529
+ });
530
+
531
+ console.log(global.logColor.success, `✅ admin-monorepo[${currentBranch}]: 已合并 ${targetBranch} 的最新更改到 packages、docs 等目录`);
526
532
  return [{ module: "admin-monorepo", success: true }];
527
533
  }
528
534