@infly/libs 2.1.5 → 2.1.6

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.
@@ -15,6 +15,7 @@ trap cleanup EXIT
15
15
  current_images=()
16
16
  next_images=()
17
17
  selected_indexes=()
18
+ selected_components=()
18
19
  selected_services=()
19
20
  for i in "${!component_names[@]}"; do
20
21
  key=${image_keys[$i]}
@@ -37,6 +38,7 @@ for pair in "$@"; do
37
38
  [[ "$suffix" != "$image" && "$suffix" =~ ^[0-9a-f]{12}$ ]] || { echo "只接受本组件正式 commit 镜像: $name" >&2; exit 1; }
38
39
  next_images[$i]=$image
39
40
  selected_indexes+=("$i")
41
+ selected_components+=("$name")
40
42
  read -r -a group <<< "${service_groups[$i]}"
41
43
  selected_services+=("${group[@]}")
42
44
  done
@@ -65,10 +67,13 @@ rollback() {
65
67
  echo "发布失败,恢复基线: $previous_env" >&2
66
68
  cp -- "$previous_env" "$release_env_file"
67
69
  chmod 600 "$release_env_file"
68
- if ! "${compose[@]}" up -d --no-deps "${selected_services[@]}" || ! bash "$bundle_dir/health-check.sh"; then
70
+ if ! "${compose[@]}" up -d --no-deps "${selected_services[@]}" \
71
+ || ! run_post_deploy "${selected_components[@]}" \
72
+ || ! bash "$bundle_dir/health-check.sh"; then
69
73
  echo "自动回滚未通过验证,需人工恢复: $project_dir/$previous_env" >&2
70
74
  fi
71
75
  }
72
76
  if ! "${compose[@]}" up -d --no-deps "${selected_services[@]}"; then rollback; exit 1; fi
77
+ if ! run_post_deploy "${selected_components[@]}"; then rollback; exit 1; fi
73
78
  if ! bash "$bundle_dir/health-check.sh"; then rollback; exit 1; fi
74
79
  echo "发布成功: $target; 上一版本记录: $project_dir/$previous_env"
@@ -14,7 +14,7 @@ function renderDeploymentConfig(config, target) {
14
14
  if (!deploy || deploy.ready !== true) throw new Error(`平台 ${target} 尚未完成人工服务器核验,禁止生产发布`);
15
15
  const components = Object.entries(profile.components || {});
16
16
  if (!components.length) throw new Error("部署必须配置 components");
17
- const repositories = [], keys = [], serviceGroups = [];
17
+ const repositories = [], keys = [], serviceGroups = [], postDeployByComponent = [];
18
18
  for (const [name, component] of components) {
19
19
  required(name, "component", /^[a-z][a-z0-9_-]*$/);
20
20
  const repository = required(component.repository, `${name}.repository`, /^[a-z0-9][a-z0-9.:/_-]+$/);
@@ -23,10 +23,20 @@ function renderDeploymentConfig(config, target) {
23
23
  keys.push(required(component.imageEnv, `${name}.imageEnv`, /^[A-Z][A-Z0-9_]*$/));
24
24
  if (!Array.isArray(component.services) || !component.services.length) throw new Error(`${name} 缺少 services`);
25
25
  serviceGroups.push(component.services.map((service) => required(service, "service", /^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/)).join(" "));
26
+ if (component.postDeploy != null && !Array.isArray(component.postDeploy)) throw new Error(`${name}.postDeploy 必须是数组`);
27
+ postDeployByComponent.push(component.postDeploy || []);
26
28
  }
27
29
  if (new Set(keys).size !== keys.length) throw new Error("imageEnv 不得重复");
28
30
  const allServices = serviceGroups.flatMap((group) => group.split(" "));
29
31
  if (new Set(allServices).size !== allServices.length) throw new Error("组件不能重复拥有同一个服务");
32
+ for (let i = 0; i < postDeployByComponent.length; i++) {
33
+ for (const action of postDeployByComponent[i]) {
34
+ if (!action || !allServices.includes(action.service) || !Array.isArray(action.command) || !action.command.length
35
+ || !action.command.every((arg) => typeof arg === "string" && arg && !arg.includes("\0"))) {
36
+ throw new Error(`${components[i][0]}.postDeploy 必须绑定已配置服务和非空命令数组`);
37
+ }
38
+ }
39
+ }
30
40
  const lines = ["# 由 @infly/libs 根据项目配置生成;只含非敏感参数。",
31
41
  `target=${quote(required(target, "target", /^[a-zA-Z0-9_-]+$/))}`,
32
42
  `registry=${quote(required(config.registry, "registry", /^[a-zA-Z0-9.:-]+$/))}`,
@@ -42,6 +52,15 @@ function renderDeploymentConfig(config, target) {
42
52
  array("component_names", components.map(([name]) => name)), array("repositories", repositories),
43
53
  array("image_keys", keys), array("service_groups", serviceGroups), array("health_services", allServices),
44
54
  ];
55
+ lines.push("run_post_deploy() {", " local component", " for component in \"$@\"; do", " case \"$component\" in");
56
+ for (let i = 0; i < components.length; i++) {
57
+ lines.push(` ${quote(components[i][0])})`);
58
+ for (const action of postDeployByComponent[i]) {
59
+ lines.push(` \"\${compose[@]}\" exec -T ${quote(action.service)} ${action.command.map(quote).join(" ")} || return $?`);
60
+ }
61
+ lines.push(" ;;");
62
+ }
63
+ lines.push(" *) echo \"未知发布组件: $component\" >&2; return 2 ;;", " esac", " done", "}");
45
64
  const health = deploy.health || {};
46
65
  if (!Array.isArray(health.urls) || !health.urls.length) throw new Error("必须配置 health.urls");
47
66
  for (const url of health.urls) if (!/^https?:\/\//.test(url)) throw new Error("健康检查 URL 必须是 HTTP(S)");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infly/libs",
3
- "version": "2.1.5",
3
+ "version": "2.1.6",
4
4
  "description": "不受前端框架限制的独立工具库",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",