@infly/libs 2.2.2 → 2.2.4
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
|
@@ -405,4 +405,4 @@ infly-libs docker:bundle --target demo --dry-run
|
|
|
405
405
|
|
|
406
406
|
`docker:release` 不是强制构建入口。独立源码仓可以直接使用 Dockerfile 或 Compose 构建,但若镜像要进入这里生成的部署流程,必须自行保证仓库名、不可变完整 40 位提交标签以及 `org.opencontainers.image.revision`、`org.opencontainers.image.version`、`com.infly.platform` 标签与目标配置一致;公共发布入口只是自动化这些校验和映射。部署侧仅为既有镜像保留旧 12 位标签兼容,新构建不再生成 12 位标签。
|
|
407
407
|
|
|
408
|
-
远端发布持锁后读取全部组件镜像基线,保存 .release-history;配置 `runtimeEnv` 时先从服务器总环境文件按白名单原子生成应用专用文件,再仅更新传入组件的服务(up -d --no-deps
|
|
408
|
+
远端发布持锁后读取全部组件镜像基线,保存 .release-history;配置 `runtimeEnv` 时先从服务器总环境文件按白名单原子生成应用专用文件,再仅更新传入组件的服务(up -d --no-deps)。首次接入可由发布入口显式传入 `--initialize-baseline`:仅在 `deploy.env` 不存在时,从配置声明的运行中服务读取唯一且仓库匹配的 RepoDigest,保存权限受限的 `.onboarding-backup` 接入证据并原子创建基线;已有基线时拒绝覆盖,未显式初始化时仍要求基线预先存在。失败恢复镜像环境文件、应用专用环境文件及选中服务;回滚验证失败明确报错。不创建总环境文件,不创建/修改数据库,不更新未配置的基础设施。服务器不需要 npm/Node,仍需 Bash、Docker Compose 和 curl。
|
|
@@ -5,6 +5,11 @@ source "$bundle_dir/config.sh"
|
|
|
5
5
|
: "${CCR_USERNAME:?未注入镜像仓库用户名}"
|
|
6
6
|
: "${CCR_PASSWORD:?未注入镜像仓库密码}"
|
|
7
7
|
: "${SSH_KNOWN_HOSTS:?未注入已核验的 SSH host keys}"
|
|
8
|
+
initialize_baseline=false
|
|
9
|
+
if [[ "${1:-}" == "--initialize-baseline" ]]; then
|
|
10
|
+
initialize_baseline=true
|
|
11
|
+
shift
|
|
12
|
+
fi
|
|
8
13
|
[[ $# -gt 0 ]] || { echo '需要组件=正式镜像参数' >&2; exit 2; }
|
|
9
14
|
local_tmp=$(mktemp -d)
|
|
10
15
|
remote_tmp=""
|
|
@@ -25,6 +30,8 @@ umask 077
|
|
|
25
30
|
printf '{"auths":{"%s":{"auth":"%s"}}}\n' "$registry" "$auth_value" > "$local_tmp/config.json"
|
|
26
31
|
scp "${ssh_options[@]}" "$bundle_dir/release.sh" "$bundle_dir/health-check.sh" "$bundle_dir/config.sh" \
|
|
27
32
|
"$bundle_dir/docker-compose.release.yml" "$local_tmp/config.json" "$ssh_target:$remote_tmp/"
|
|
28
|
-
remote_command=(sudo env "DOCKER_CONFIG=$remote_tmp" bash "$remote_tmp/release.sh"
|
|
33
|
+
remote_command=(sudo env "DOCKER_CONFIG=$remote_tmp" bash "$remote_tmp/release.sh")
|
|
34
|
+
if [[ "$initialize_baseline" == true ]]; then remote_command+=(--initialize-baseline); fi
|
|
35
|
+
remote_command+=("$@")
|
|
29
36
|
printf -v remote_quoted '%q ' "${remote_command[@]}"
|
|
30
37
|
ssh "${ssh_options[@]}" "$ssh_target" "$remote_quoted"
|
|
@@ -2,14 +2,20 @@
|
|
|
2
2
|
set -euo pipefail
|
|
3
3
|
bundle_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)
|
|
4
4
|
source "$bundle_dir/config.sh"
|
|
5
|
+
initialize_baseline=false
|
|
6
|
+
if [[ "${1:-}" == "--initialize-baseline" ]]; then
|
|
7
|
+
initialize_baseline=true
|
|
8
|
+
shift
|
|
9
|
+
fi
|
|
5
10
|
[[ $# -gt 0 ]] || { echo '需要组件=正式镜像参数' >&2; exit 2; }
|
|
6
11
|
cd "$project_dir"
|
|
7
|
-
for file in "$compose_file" "$secret_env_file" "$
|
|
12
|
+
for file in "$compose_file" "$secret_env_file" "$bundle_dir/docker-compose.release.yml"; do
|
|
8
13
|
[[ -f "$file" ]] || { echo "部署缺少文件: $file" >&2; exit 1; }
|
|
9
14
|
done
|
|
10
15
|
# 先持有目录锁再读基线,避免前后端两个任务同时发布时使用过期基线。
|
|
11
16
|
mkdir "$lock_dir" 2>/dev/null || { echo "已有发布任务持有锁: $project_dir/$lock_dir" >&2; exit 1; }
|
|
12
17
|
next_env=""
|
|
18
|
+
baseline_env_tmp=""
|
|
13
19
|
runtime_env_backup=""
|
|
14
20
|
runtime_env_changed=false
|
|
15
21
|
runtime_env_had_previous=false
|
|
@@ -31,11 +37,65 @@ cleanup() {
|
|
|
31
37
|
local status=$?
|
|
32
38
|
if [[ "$release_complete" != true ]]; then restore_runtime_env || true; fi
|
|
33
39
|
[[ -z "$next_env" ]] || rm -f -- "$next_env"
|
|
40
|
+
[[ -z "$baseline_env_tmp" ]] || rm -f -- "$baseline_env_tmp"
|
|
34
41
|
[[ -z "$runtime_env_backup" ]] || rm -f -- "$runtime_env_backup"
|
|
35
42
|
rmdir "$lock_dir" 2>/dev/null || true
|
|
36
43
|
return "$status"
|
|
37
44
|
}
|
|
38
45
|
trap cleanup EXIT
|
|
46
|
+
initialize_release_baseline() {
|
|
47
|
+
[[ "$initialize_baseline" == true ]] || {
|
|
48
|
+
[[ -f "$release_env_file" ]] || { echo "部署缺少文件: $release_env_file;首次接入请显式选择初始化部署基线" >&2; return 1; }
|
|
49
|
+
return 0
|
|
50
|
+
}
|
|
51
|
+
[[ ! -e "$release_env_file" ]] || { echo "部署基线已存在,拒绝首次初始化覆盖: $project_dir/$release_env_file" >&2; return 1; }
|
|
52
|
+
|
|
53
|
+
local backup_root backup_dir i service container_id image_id repository
|
|
54
|
+
local -a group digests
|
|
55
|
+
backup_root="$project_dir/.onboarding-backup"
|
|
56
|
+
mkdir -p -- "$backup_root"
|
|
57
|
+
chmod 700 -- "$backup_root"
|
|
58
|
+
backup_dir=$(mktemp -d "$backup_root/${target}-$(date -u +%Y%m%dT%H%M%SZ)-XXXXXX")
|
|
59
|
+
chmod 700 -- "$backup_dir"
|
|
60
|
+
cp -- "$compose_file" "$backup_dir/compose.yml"
|
|
61
|
+
cp -- "$secret_env_file" "$backup_dir/secret.env"
|
|
62
|
+
chmod 600 -- "$backup_dir/compose.yml" "$backup_dir/secret.env"
|
|
63
|
+
|
|
64
|
+
local -a base_compose=(docker compose --env-file "$secret_env_file" -f "$compose_file")
|
|
65
|
+
"${base_compose[@]}" ps > "$backup_dir/compose-ps.txt"
|
|
66
|
+
baseline_env_tmp=$(mktemp "${release_env_file}.bootstrap.XXXXXX")
|
|
67
|
+
for i in "${!component_names[@]}"; do
|
|
68
|
+
read -r -a group <<< "${service_groups[$i]}"
|
|
69
|
+
image_id=""
|
|
70
|
+
for service in "${group[@]}"; do
|
|
71
|
+
container_id=$("${base_compose[@]}" ps -q "$service")
|
|
72
|
+
[[ -n "$container_id" ]] || { echo "首次初始化找不到服务容器: $service" >&2; return 1; }
|
|
73
|
+
[[ "$(docker inspect --format '{{.State.Running}}' "$container_id")" == true ]] || {
|
|
74
|
+
echo "首次初始化要求服务正在运行: $service" >&2; return 1;
|
|
75
|
+
}
|
|
76
|
+
printf '%s=%s\n' "$service" "$container_id" >> "$backup_dir/container-ids.txt"
|
|
77
|
+
if [[ -z "$image_id" ]]; then
|
|
78
|
+
image_id=$(docker inspect --format '{{.Image}}' "$container_id")
|
|
79
|
+
elif [[ "$image_id" != "$(docker inspect --format '{{.Image}}' "$container_id")" ]]; then
|
|
80
|
+
echo "同一组件的服务镜像不一致,拒绝初始化: ${component_names[$i]}" >&2
|
|
81
|
+
return 1
|
|
82
|
+
fi
|
|
83
|
+
done
|
|
84
|
+
repository=${repositories[$i]}
|
|
85
|
+
mapfile -t digests < <(docker image inspect --format '{{range .RepoDigests}}{{println .}}{{end}}' "$image_id" | awk -v prefix="$repository@sha256:" 'index($0, prefix) == 1')
|
|
86
|
+
[[ "${#digests[@]}" == 1 && "${digests[0]}" =~ ^${repository//./\\.}@sha256:[0-9a-f]{64}$ ]] || {
|
|
87
|
+
echo "当前镜像缺少唯一且匹配配置仓库的 RepoDigest: ${component_names[$i]} ($repository)" >&2
|
|
88
|
+
return 1
|
|
89
|
+
}
|
|
90
|
+
printf '%s=%s\n' "${image_keys[$i]}" "${digests[0]}" >> "$baseline_env_tmp"
|
|
91
|
+
done
|
|
92
|
+
chmod 600 -- "$baseline_env_tmp" "$backup_dir/compose-ps.txt" "$backup_dir/container-ids.txt"
|
|
93
|
+
mv -- "$baseline_env_tmp" "$release_env_file"
|
|
94
|
+
baseline_env_tmp=""
|
|
95
|
+
chmod 600 -- "$release_env_file"
|
|
96
|
+
echo "首次部署基线已初始化;接入备份: $backup_dir"
|
|
97
|
+
}
|
|
98
|
+
initialize_release_baseline
|
|
39
99
|
current_images=()
|
|
40
100
|
next_images=()
|
|
41
101
|
selected_indexes=()
|
|
@@ -274,6 +274,10 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
274
274
|
if (!/^[a-f0-9]{40}$/.test(commit) || commit !== git("rev-parse", `${expectedRef}^{commit}`).toLowerCase()) {
|
|
275
275
|
throw new Error(`${item.name} HEAD 与 ${expectedRef} 不一致,请先核对并 fetch 对应仓库`);
|
|
276
276
|
}
|
|
277
|
+
if (item.source) {
|
|
278
|
+
const sourceLabel = item.name === "backend" ? "后端" : (item.label || item.name);
|
|
279
|
+
log(`\x1b[0;32m✓ ${sourceLabel}构建分支:${item.source.branch}\x1b[0m`);
|
|
280
|
+
}
|
|
277
281
|
let context = inside(sourceDir, item.context || ".");
|
|
278
282
|
let dockerfile = inside(sourceDir, item.dockerfile || "Dockerfile");
|
|
279
283
|
for (const file of item.requiredFiles || []) {
|