@infly/libs 2.5.0 → 2.7.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 +502 -469
- package/adapters/vue2/project-preview.js +9 -16
- package/adapters/webpack/auto-export.js +53 -52
- package/bin/cli.js +3 -2
- package/build/build-dist/index.js +34 -44
- package/build/build-utils/command.js +58 -57
- package/build/build-utils/deps.js +28 -28
- package/build/build-utils/git.js +246 -254
- package/build/build-utils/preview.js +79 -78
- package/build/build-utils/webhook.js +135 -135
- package/build/docker/compose-release.js +7 -9
- package/build/docker/deployment/health-check.sh +5 -1
- package/build/docker/deployment/promote-image.sh +75 -5
- package/build/docker/deployment/release.sh +5 -1
- package/build/docker/docker-build-push.js +23 -23
- package/build/docker/project-install.js +42 -35
- package/build/docker/release-docker.js +7 -25
- package/build/docker/release-mode.js +7 -25
- package/build/docker/release-paths.js +30 -0
- package/build/docker/release-validation.js +248 -0
- package/build/docker/source-snapshot.js +2 -1
- package/build/docker/target-image.js +227 -0
- package/build/docker/target-release.js +540 -860
- package/build/project-files.js +47 -47
- package/cli/progress.js +4 -11
- package/cli/terminal-style.js +40 -0
- package/core/async/concurrency.js +29 -29
- package/jenkins/README.md +94 -0
- package/jenkins/src/com/infly/jenkins/BuildProvenance.groovy +16 -0
- package/jenkins/src/com/infly/jenkins/PipelineJson.groovy +28 -0
- package/jenkins/src/com/infly/jenkins/ReleaseNotification.groovy +86 -0
- package/jenkins/vars/inflyNotify.groovy +83 -0
- package/package.json +4 -1
- package/script/git-automation/check-packages.js +5 -12
- package/script/git-automation/git-clone.js +6 -5
- package/script/git-automation/git-configure.js +3 -3
- package/script/git-automation/git-submodule.js +3 -2
- package/script/git-automation/git-utils.js +59 -59
- package/script/git-automation/index.js +813 -828
- package/script/git-automation/submodule-utils.js +16 -16
- package/workspace/config.js +144 -144
|
@@ -1,135 +1,135 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const { formatReleaseResult, resolveWebhookUrl } = require("../release-result");
|
|
3
|
-
|
|
4
|
-
function shouldSkipWebhook(args = process.argv, env = process.env) {
|
|
5
|
-
return args.some((arg) => ["--nowebhook", "--noWebhook", "--no-webhook"].includes(arg))
|
|
6
|
-
|| [env.npm_config_nowebhook, env.npm_config_noWebhook, env.NO_WEBHOOK, env.DISABLE_WEBHOOK]
|
|
7
|
-
.some((value) => ["1", "true", "yes", "on"].includes(String(value || "").toLowerCase()));
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function getWebhookMessage(args = process.argv, env = process.env) {
|
|
11
|
-
const configured = env.npm_config_m || env.npm_config_message || env.WEBHOOK_MESSAGE || env.BUILD_MESSAGE;
|
|
12
|
-
if (configured) return String(configured).trim();
|
|
13
|
-
for (let index = 0; index < args.length; index++) {
|
|
14
|
-
const arg = args[index] || "";
|
|
15
|
-
if (["-m", "--message"].includes(arg)) return String(args[index + 1] || "").trim();
|
|
16
|
-
if (arg.startsWith("-m=")) return arg.slice(3).trim();
|
|
17
|
-
if (arg.startsWith("--message=")) return arg.slice(10).trim();
|
|
18
|
-
}
|
|
19
|
-
return "";
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async function sendWebhookContent(content, options = {}, dependencies = {}) {
|
|
23
|
-
const env = dependencies.env || process.env;
|
|
24
|
-
const logger = dependencies.logger || console;
|
|
25
|
-
const webhookUrl = resolveWebhookUrl(options, env);
|
|
26
|
-
if (!webhookUrl) return false;
|
|
27
|
-
const { webhookAtUser = [], gitInfo = {} } = options;
|
|
28
|
-
const mentions = Array.isArray(webhookAtUser) ? webhookAtUser : webhookAtUser[gitInfo.branch];
|
|
29
|
-
try {
|
|
30
|
-
const response = await (dependencies.httpClient || require("axios")).post(webhookUrl, {
|
|
31
|
-
msgtype: "text", text: {
|
|
32
|
-
content: String(content || "").replace(/%s/g, ""), mentioned_mobile_list: mentions,
|
|
33
|
-
},
|
|
34
|
-
}, { timeout: 15000, headers: { "Content-Type": "application/json" } });
|
|
35
|
-
if (response.data?.errcode !== 0) throw new Error('WEBHOOK_REJECTED');
|
|
36
|
-
return true;
|
|
37
|
-
} catch (error) {
|
|
38
|
-
// Axios error/config 可能包含完整凭据 URL,日志只输出受限错误码。
|
|
39
|
-
const code = /^[A-Z0-9_]{1,40}$/.test(error.code || '') ? error.code : 'WEBHOOK_FAILED';
|
|
40
|
-
logger.error("企业微信推送失败", code);
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function getRecordContent(record, deployUrl) {
|
|
46
|
-
if (record?.extraParams?.releaseResult) return formatReleaseResult(record.extraParams.releaseResult);
|
|
47
|
-
const { publishText = "", extraParams = {} } = record || {};
|
|
48
|
-
const {
|
|
49
|
-
gitInfo = {},
|
|
50
|
-
gitAutoPushReposBranchMap,
|
|
51
|
-
repos,
|
|
52
|
-
newFileName,
|
|
53
|
-
projectName,
|
|
54
|
-
webhookExtraText = ""
|
|
55
|
-
} = extraParams;
|
|
56
|
-
const { branch, author } = gitInfo || {};
|
|
57
|
-
|
|
58
|
-
if (gitAutoPushReposBranchMap) {
|
|
59
|
-
const targetBranch = gitAutoPushReposBranchMap[branch] || branch || "";
|
|
60
|
-
return [
|
|
61
|
-
publishText,
|
|
62
|
-
`构建文件:已推送对应仓库【${repos || ""}】- 分支【${targetBranch}】`,
|
|
63
|
-
`构建人:${author || ""}`,
|
|
64
|
-
deployUrl ? `部署系统:${deployUrl}` : ""
|
|
65
|
-
]
|
|
66
|
-
.filter(Boolean)
|
|
67
|
-
.join("\n");
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const gitPath =
|
|
71
|
-
projectName && branch && newFileName
|
|
72
|
-
? `https://gitee.com/gdinfly_1/${projectName}/blob/${branch}/${newFileName}`
|
|
73
|
-
: "";
|
|
74
|
-
|
|
75
|
-
return [
|
|
76
|
-
publishText,
|
|
77
|
-
gitPath ? `构建文件下载地址:${gitPath}` : "",
|
|
78
|
-
webhookExtraText
|
|
79
|
-
]
|
|
80
|
-
.filter(Boolean)
|
|
81
|
-
.join("\n");
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
async function postBatchWebhook(batchWebhookFile, extraArgs = [], mode, dependencies = {}) {
|
|
85
|
-
if (shouldSkipWebhook(extraArgs, dependencies.env || process.env)) {
|
|
86
|
-
console.log("\n已跳过企业微信机器人汇总推送(nowebhook)");
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (!fs.existsSync(batchWebhookFile)) {
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const records = fs
|
|
95
|
-
.readFileSync(batchWebhookFile, "utf8")
|
|
96
|
-
.split(/\r?\n/)
|
|
97
|
-
.filter(Boolean)
|
|
98
|
-
.map((line) => JSON.parse(line));
|
|
99
|
-
|
|
100
|
-
if (records.length === 0) {
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const [firstRecord] = records;
|
|
105
|
-
const { deployUrl = "" } =
|
|
106
|
-
firstRecord.extraParams || {};
|
|
107
|
-
|
|
108
|
-
let content = [
|
|
109
|
-
records.map((record) => getRecordContent(record, deployUrl)).join("\n\n")
|
|
110
|
-
].join("\n");
|
|
111
|
-
// 防御性过滤:防止运行时数据中的 %s 等格式化占位符泄露到推送内容
|
|
112
|
-
content = (content || "").replace(/%s/g, "");
|
|
113
|
-
if (await sendWebhookContent(content, firstRecord.extraParams, dependencies)) {
|
|
114
|
-
console.log("\n已发送批量构建企业微信汇总推送");
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function postVersionFileAndMsg(publishText, extraParams = {}, dependencies = {}) {
|
|
119
|
-
const env = dependencies.env || process.env;
|
|
120
|
-
const args = dependencies.args || process.argv;
|
|
121
|
-
if (shouldSkipWebhook(args, env)) return;
|
|
122
|
-
let content = getRecordContent({ publishText, extraParams }, extraParams.deployUrl);
|
|
123
|
-
const message = getWebhookMessage(args, env);
|
|
124
|
-
if (message && !extraParams.releaseResult) content += `\n${message}`;
|
|
125
|
-
return sendWebhookContent(content, extraParams, dependencies);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
module.exports = {
|
|
129
|
-
getRecordContent,
|
|
130
|
-
getWebhookMessage,
|
|
131
|
-
shouldSkipWebhook,
|
|
132
|
-
sendWebhookContent,
|
|
133
|
-
postVersionFileAndMsg,
|
|
134
|
-
postBatchWebhook
|
|
135
|
-
};
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const { formatReleaseResult, resolveWebhookUrl } = require("../release-result");
|
|
3
|
+
|
|
4
|
+
function shouldSkipWebhook(args = process.argv, env = process.env) {
|
|
5
|
+
return args.some((arg) => ["--nowebhook", "--noWebhook", "--no-webhook"].includes(arg))
|
|
6
|
+
|| [env.npm_config_nowebhook, env.npm_config_noWebhook, env.NO_WEBHOOK, env.DISABLE_WEBHOOK]
|
|
7
|
+
.some((value) => ["1", "true", "yes", "on"].includes(String(value || "").toLowerCase()));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function getWebhookMessage(args = process.argv, env = process.env) {
|
|
11
|
+
const configured = env.npm_config_m || env.npm_config_message || env.WEBHOOK_MESSAGE || env.BUILD_MESSAGE;
|
|
12
|
+
if (configured) return String(configured).trim();
|
|
13
|
+
for (let index = 0; index < args.length; index++) {
|
|
14
|
+
const arg = args[index] || "";
|
|
15
|
+
if (["-m", "--message"].includes(arg)) return String(args[index + 1] || "").trim();
|
|
16
|
+
if (arg.startsWith("-m=")) return arg.slice(3).trim();
|
|
17
|
+
if (arg.startsWith("--message=")) return arg.slice(10).trim();
|
|
18
|
+
}
|
|
19
|
+
return "";
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function sendWebhookContent(content, options = {}, dependencies = {}) {
|
|
23
|
+
const env = dependencies.env || process.env;
|
|
24
|
+
const logger = dependencies.logger || console;
|
|
25
|
+
const webhookUrl = resolveWebhookUrl(options, env);
|
|
26
|
+
if (!webhookUrl) return false;
|
|
27
|
+
const { webhookAtUser = [], gitInfo = {} } = options;
|
|
28
|
+
const mentions = Array.isArray(webhookAtUser) ? webhookAtUser : webhookAtUser[gitInfo.branch];
|
|
29
|
+
try {
|
|
30
|
+
const response = await (dependencies.httpClient || require("axios")).post(webhookUrl, {
|
|
31
|
+
msgtype: "text", text: {
|
|
32
|
+
content: String(content || "").replace(/%s/g, ""), mentioned_mobile_list: mentions,
|
|
33
|
+
},
|
|
34
|
+
}, { timeout: 15000, headers: { "Content-Type": "application/json" } });
|
|
35
|
+
if (response.data?.errcode !== 0) throw new Error('WEBHOOK_REJECTED');
|
|
36
|
+
return true;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
// Axios error/config 可能包含完整凭据 URL,日志只输出受限错误码。
|
|
39
|
+
const code = /^[A-Z0-9_]{1,40}$/.test(error.code || '') ? error.code : 'WEBHOOK_FAILED';
|
|
40
|
+
logger.error("企业微信推送失败", code);
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function getRecordContent(record, deployUrl) {
|
|
46
|
+
if (record?.extraParams?.releaseResult) return formatReleaseResult(record.extraParams.releaseResult);
|
|
47
|
+
const { publishText = "", extraParams = {} } = record || {};
|
|
48
|
+
const {
|
|
49
|
+
gitInfo = {},
|
|
50
|
+
gitAutoPushReposBranchMap,
|
|
51
|
+
repos,
|
|
52
|
+
newFileName,
|
|
53
|
+
projectName,
|
|
54
|
+
webhookExtraText = ""
|
|
55
|
+
} = extraParams;
|
|
56
|
+
const { branch, author } = gitInfo || {};
|
|
57
|
+
|
|
58
|
+
if (gitAutoPushReposBranchMap) {
|
|
59
|
+
const targetBranch = gitAutoPushReposBranchMap[branch] || branch || "";
|
|
60
|
+
return [
|
|
61
|
+
publishText,
|
|
62
|
+
`构建文件:已推送对应仓库【${repos || ""}】- 分支【${targetBranch}】`,
|
|
63
|
+
`构建人:${author || ""}`,
|
|
64
|
+
deployUrl ? `部署系统:${deployUrl}` : ""
|
|
65
|
+
]
|
|
66
|
+
.filter(Boolean)
|
|
67
|
+
.join("\n");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const gitPath =
|
|
71
|
+
projectName && branch && newFileName
|
|
72
|
+
? `https://gitee.com/gdinfly_1/${projectName}/blob/${branch}/${newFileName}`
|
|
73
|
+
: "";
|
|
74
|
+
|
|
75
|
+
return [
|
|
76
|
+
publishText,
|
|
77
|
+
gitPath ? `构建文件下载地址:${gitPath}` : "",
|
|
78
|
+
webhookExtraText
|
|
79
|
+
]
|
|
80
|
+
.filter(Boolean)
|
|
81
|
+
.join("\n");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async function postBatchWebhook(batchWebhookFile, extraArgs = [], mode, dependencies = {}) {
|
|
85
|
+
if (shouldSkipWebhook(extraArgs, dependencies.env || process.env)) {
|
|
86
|
+
console.log("\n已跳过企业微信机器人汇总推送(nowebhook)");
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (!fs.existsSync(batchWebhookFile)) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const records = fs
|
|
95
|
+
.readFileSync(batchWebhookFile, "utf8")
|
|
96
|
+
.split(/\r?\n/)
|
|
97
|
+
.filter(Boolean)
|
|
98
|
+
.map((line) => JSON.parse(line));
|
|
99
|
+
|
|
100
|
+
if (records.length === 0) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const [firstRecord] = records;
|
|
105
|
+
const { deployUrl = "" } =
|
|
106
|
+
firstRecord.extraParams || {};
|
|
107
|
+
|
|
108
|
+
let content = [
|
|
109
|
+
records.map((record) => getRecordContent(record, deployUrl)).join("\n\n")
|
|
110
|
+
].join("\n");
|
|
111
|
+
// 防御性过滤:防止运行时数据中的 %s 等格式化占位符泄露到推送内容
|
|
112
|
+
content = (content || "").replace(/%s/g, "");
|
|
113
|
+
if (await sendWebhookContent(content, firstRecord.extraParams, dependencies)) {
|
|
114
|
+
console.log("\n已发送批量构建企业微信汇总推送");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function postVersionFileAndMsg(publishText, extraParams = {}, dependencies = {}) {
|
|
119
|
+
const env = dependencies.env || process.env;
|
|
120
|
+
const args = dependencies.args || process.argv;
|
|
121
|
+
if (shouldSkipWebhook(args, env)) return;
|
|
122
|
+
let content = getRecordContent({ publishText, extraParams }, extraParams.deployUrl);
|
|
123
|
+
const message = getWebhookMessage(args, env);
|
|
124
|
+
if (message && !extraParams.releaseResult) content += `\n${message}`;
|
|
125
|
+
return sendWebhookContent(content, extraParams, dependencies);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
module.exports = {
|
|
129
|
+
getRecordContent,
|
|
130
|
+
getWebhookMessage,
|
|
131
|
+
shouldSkipWebhook,
|
|
132
|
+
sendWebhookContent,
|
|
133
|
+
postVersionFileAndMsg,
|
|
134
|
+
postBatchWebhook
|
|
135
|
+
};
|
|
@@ -62,7 +62,9 @@ function composeRelease(options = {}, dependencies = {}) {
|
|
|
62
62
|
const resolved = resolveComposePath(projectDir, file);
|
|
63
63
|
const original = fs.readFileSync(resolved.composePath, "utf8");
|
|
64
64
|
const updated = bumpComposeImageVersion(original, bump);
|
|
65
|
-
const
|
|
65
|
+
const commands = ["config", "build", ...(push ? ["push"] : [])]
|
|
66
|
+
.map((command) => ["compose", "-f", resolved.composePath, command]);
|
|
67
|
+
const result = { ...updated, ...resolved };
|
|
66
68
|
|
|
67
69
|
if (bump === "none") {
|
|
68
70
|
console.log(`Version fixed: ${updated.from}`);
|
|
@@ -71,24 +73,20 @@ function composeRelease(options = {}, dependencies = {}) {
|
|
|
71
73
|
}
|
|
72
74
|
|
|
73
75
|
if (dryRun) {
|
|
74
|
-
console.log(`[dry-run] docker ${
|
|
75
|
-
|
|
76
|
-
if (push) console.log(`[dry-run] docker ${[...baseArgs, "push"].join(" ")}`);
|
|
77
|
-
return { ...updated, projectDir: resolved.projectDir, composePath: resolved.composePath };
|
|
76
|
+
for (const args of commands) console.log(`[dry-run] docker ${args.join(" ")}`);
|
|
77
|
+
return result;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
const versionChanged = updated.content !== original;
|
|
81
81
|
if (versionChanged) fs.writeFileSync(resolved.composePath, updated.content, "utf8");
|
|
82
82
|
try {
|
|
83
|
-
runDocker(
|
|
84
|
-
runDocker([...baseArgs, "build"], { cwd: resolved.projectDir });
|
|
85
|
-
if (push) runDocker([...baseArgs, "push"], { cwd: resolved.projectDir });
|
|
83
|
+
for (const args of commands) runDocker(args, { cwd: resolved.projectDir });
|
|
86
84
|
} catch (error) {
|
|
87
85
|
if (versionChanged) fs.writeFileSync(resolved.composePath, original, "utf8");
|
|
88
86
|
throw error;
|
|
89
87
|
}
|
|
90
88
|
|
|
91
|
-
return
|
|
89
|
+
return result;
|
|
92
90
|
}
|
|
93
91
|
|
|
94
92
|
module.exports = {
|
|
@@ -24,5 +24,9 @@ for health_url in "${health_urls[@]}"; do
|
|
|
24
24
|
sleep "$health_interval"
|
|
25
25
|
done
|
|
26
26
|
[[ "$checked" == true ]] || { echo "健康检查失败: $health_url" >&2; exit 1; }
|
|
27
|
-
|
|
27
|
+
if [[ -t 1 && -z ${NO_COLOR+x} && ${FORCE_COLOR-} != 0 ]]; then
|
|
28
|
+
printf '\033[32m√ 健康检查通过: %s\033[0m\n' "$health_url"
|
|
29
|
+
else
|
|
30
|
+
printf '健康检查通过: %s\n' "$health_url"
|
|
31
|
+
fi
|
|
28
32
|
done
|
|
@@ -23,9 +23,44 @@ cleanup() {
|
|
|
23
23
|
}
|
|
24
24
|
trap cleanup EXIT
|
|
25
25
|
|
|
26
|
+
push_release_image() {
|
|
27
|
+
local help api_versions client_api server_api api platform_supported=true
|
|
28
|
+
help=$(docker push --help) || return 1
|
|
29
|
+
if [[ "$help" == *--platform* ]]; then
|
|
30
|
+
api_versions=$(docker version --format '{{.Client.APIVersion}} {{.Server.APIVersion}}') || return 1
|
|
31
|
+
read -r client_api server_api <<< "$api_versions"
|
|
32
|
+
for api in "$client_api" "$server_api" "${DOCKER_API_VERSION:-1.46}"; do
|
|
33
|
+
[[ "$api" =~ ^([0-9]+)\.([0-9]+)$ ]] || {
|
|
34
|
+
echo "$component 无法确定 Docker push API 兼容性" >&2
|
|
35
|
+
return 1
|
|
36
|
+
}
|
|
37
|
+
if ((10#${BASH_REMATCH[1]} < 1 || (10#${BASH_REMATCH[1]} == 1 && 10#${BASH_REMATCH[2]} < 46))); then
|
|
38
|
+
platform_supported=false
|
|
39
|
+
fi
|
|
40
|
+
done
|
|
41
|
+
else
|
|
42
|
+
platform_supported=false
|
|
43
|
+
fi
|
|
44
|
+
if [[ "$platform_supported" == true ]]; then
|
|
45
|
+
# containerd 拉取的镜像可能保留未完整下载的索引;正式标签只推送已核验镜像的平台清单。
|
|
46
|
+
# 平台来自镜像 ID,不能假定 Jenkins 主机或业务镜像使用 amd64。
|
|
47
|
+
echo "$component 按已核验平台晋升镜像: $source_platform"
|
|
48
|
+
docker push --platform "$source_platform" "$final_image"
|
|
49
|
+
else
|
|
50
|
+
# 旧 CLI/API 保留原有单镜像推送;真实推送失败立即停止,不再降级重试。
|
|
51
|
+
echo "$component Docker CLI/API 不支持指定平台推送,使用兼容的单镜像推送"
|
|
52
|
+
docker push "$final_image"
|
|
53
|
+
fi
|
|
54
|
+
}
|
|
55
|
+
|
|
26
56
|
docker manifest inspect "$source_image" >/dev/null
|
|
27
57
|
docker pull "$source_image"
|
|
28
|
-
|
|
58
|
+
source_id=$(docker image inspect --format '{{.Id}}' "$source_image")
|
|
59
|
+
[[ "$source_id" =~ ^sha256:[0-9a-f]{64}$ ]] || {
|
|
60
|
+
echo "$component qc 镜像 ID 无效" >&2
|
|
61
|
+
exit 1
|
|
62
|
+
}
|
|
63
|
+
actual_revision=$(docker image inspect --format '{{ index .Config.Labels "org.opencontainers.image.revision" }}' "$source_id")
|
|
29
64
|
if [[ "$actual_revision" != "$expected_revision" ]]; then
|
|
30
65
|
echo "$component qc 镜像完整 revision 校验失败: image=$source_image" >&2
|
|
31
66
|
exit 1
|
|
@@ -48,10 +83,45 @@ if ! grep -Eqi 'manifest unknown|no such manifest|manifest[^[:cntrl:]]*not found
|
|
|
48
83
|
exit 1
|
|
49
84
|
fi
|
|
50
85
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
86
|
+
if docker buildx version >/dev/null 2>&1; then
|
|
87
|
+
if [[ "$source_image" == *@* ]]; then
|
|
88
|
+
source_digest=${source_image##*@}
|
|
89
|
+
source_repository=${source_image%@*}
|
|
90
|
+
[[ -n "$source_repository" && "$source_repository" != *@* ]] || exit 1
|
|
91
|
+
else
|
|
92
|
+
# printf 前缀兼容 Buildx v0.10.5,避免其把以 {{.Manifest 开头的格式当成固定展示模式。
|
|
93
|
+
source_digest=$(docker buildx imagetools inspect "$source_image" --format '{{printf "%s" .Manifest.Digest}}')
|
|
94
|
+
source_repository=$source_image
|
|
95
|
+
# 只移除最后一个路径段的标签,保留 registry:port 与未显式指定标签的仓库名。
|
|
96
|
+
if [[ "${source_repository##*/}" == *:* ]]; then source_repository=${source_repository%:*}; fi
|
|
97
|
+
fi
|
|
98
|
+
[[ "$source_digest" =~ ^sha256:[0-9a-f]{64}$ ]] || {
|
|
99
|
+
echo "$component 无法冻结 qc 镜像 Registry digest" >&2
|
|
100
|
+
exit 1
|
|
101
|
+
}
|
|
102
|
+
frozen_source="$source_repository@$source_digest"
|
|
103
|
+
# 在冻结后重新拉取并校验;若 QC 标签在第一次校验后漂移,禁止复制另一个镜像。
|
|
104
|
+
docker pull "$frozen_source"
|
|
105
|
+
frozen_id=$(docker image inspect --format '{{.Id}}' "$frozen_source")
|
|
106
|
+
frozen_revision=$(docker image inspect --format '{{ index .Config.Labels "org.opencontainers.image.revision" }}' "$frozen_id")
|
|
107
|
+
if [[ "$frozen_id" != "$source_id" || "$frozen_revision" != "$expected_revision" ]]; then
|
|
108
|
+
echo "$component 冻结后的 qc 镜像与已核验镜像不一致,禁止晋升" >&2
|
|
109
|
+
exit 1
|
|
110
|
+
fi
|
|
111
|
+
echo "$component 从已冻结 Registry digest 晋升镜像: $frozen_source"
|
|
112
|
+
# 仓库端复制保留完整索引及附加证明,不依赖 Docker Engine 的 push --platform 能力。
|
|
113
|
+
# inspect/create 的真实错误立即停止,不切换到本地推送路径。
|
|
114
|
+
docker buildx imagetools create --tag "$final_image" "$frozen_source"
|
|
115
|
+
else
|
|
116
|
+
source_platform=$(docker image inspect --format '{{.Os}}/{{.Architecture}}{{if .Variant}}/{{.Variant}}{{end}}' "$source_id")
|
|
117
|
+
[[ "$source_platform" =~ ^[a-z0-9][a-z0-9_.-]*/[a-z0-9][a-z0-9_.-]*(/[a-z0-9][a-z0-9_.-]*)?$ ]] || {
|
|
118
|
+
echo "$component qc 镜像平台无效" >&2
|
|
119
|
+
exit 1
|
|
120
|
+
}
|
|
121
|
+
# 没有 Buildx 时以已核验的 ID 重标记,保留新旧 Docker CLI/API 兼容路径。
|
|
122
|
+
docker tag "$source_id" "$final_image"
|
|
123
|
+
push_release_image
|
|
124
|
+
fi
|
|
55
125
|
docker pull "$final_image"
|
|
56
126
|
final_revision=$(docker image inspect --format '{{ index .Config.Labels "org.opencontainers.image.revision" }}' "$final_image")
|
|
57
127
|
if [[ "$final_revision" != "$expected_revision" ]]; then
|
|
@@ -216,4 +216,8 @@ if ! "${compose[@]}" up -d --no-deps "${selected_services[@]}"; then rollback; e
|
|
|
216
216
|
if ! run_post_deploy "${selected_components[@]}"; then rollback; exit 1; fi
|
|
217
217
|
if ! bash "$bundle_dir/health-check.sh"; then rollback; exit 1; fi
|
|
218
218
|
release_complete=true
|
|
219
|
-
|
|
219
|
+
if [[ -t 1 && -z ${NO_COLOR+x} && ${FORCE_COLOR-} != 0 ]]; then
|
|
220
|
+
printf '\033[32m√ 发布成功: %s; 上一版本记录: %s\033[0m\n' "$target" "$project_dir/$previous_env"
|
|
221
|
+
else
|
|
222
|
+
printf '发布成功: %s; 上一版本记录: %s\n' "$target" "$project_dir/$previous_env"
|
|
223
|
+
fi
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
* [--health-check] [--health-timeout SECONDS] [--health-add-host HOST:IP]
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
|
+
const { logTerminal } = require("../../cli/terminal-style");
|
|
20
21
|
const { execSync, spawn } = require("child_process");
|
|
21
22
|
const fs = require("fs");
|
|
22
23
|
const path = require("path");
|
|
@@ -86,7 +87,7 @@ function formatCicdLink(value, deployParams) {
|
|
|
86
87
|
.replace(/%2F/gi, "/").replace(/%26/gi, "&").replace(/%3D/gi, "=").replace(/%3A/gi, ":");
|
|
87
88
|
const hyperlinkStart = `\x1b]8;;${parsedUrl.href}\x1b\\`;
|
|
88
89
|
const hyperlinkEnd = "\x1b]8;;\x1b\\";
|
|
89
|
-
return `发布系统:\n${hyperlinkStart}
|
|
90
|
+
return `发布系统:\n${hyperlinkStart}${displayHref}${hyperlinkEnd}`;
|
|
90
91
|
}
|
|
91
92
|
|
|
92
93
|
function detectMode({ standalone = false } = {}) {
|
|
@@ -256,13 +257,13 @@ function healthCheckImage(imageTag, options = {}) {
|
|
|
256
257
|
const addHostArg = healthAddHost ? ` --add-host ${healthAddHost}` : "";
|
|
257
258
|
|
|
258
259
|
console.log("");
|
|
259
|
-
console.log("
|
|
260
|
+
console.log("启动临时容器并等待镜像 HEALTHCHECK");
|
|
260
261
|
run(`docker run --detach --name ${containerName}${addHostArg} ${imageTag}`, { dryRun });
|
|
261
262
|
|
|
262
263
|
if (dryRun) {
|
|
263
264
|
run(`docker inspect --format health-status ${containerName}`, { dryRun });
|
|
264
265
|
run(`docker rm --force ${containerName}`, { dryRun });
|
|
265
|
-
|
|
266
|
+
logTerminal("success", "[DRY RUN] 镜像健康检查计划完成");
|
|
266
267
|
return;
|
|
267
268
|
}
|
|
268
269
|
|
|
@@ -276,7 +277,7 @@ function healthCheckImage(imageTag, options = {}) {
|
|
|
276
277
|
).trim();
|
|
277
278
|
|
|
278
279
|
if (lastStatus === "healthy") {
|
|
279
|
-
|
|
280
|
+
logTerminal("success", "镜像健康检查通过");
|
|
280
281
|
return;
|
|
281
282
|
}
|
|
282
283
|
if (lastStatus === "unhealthy" || lastStatus === "missing") {
|
|
@@ -285,20 +286,20 @@ function healthCheckImage(imageTag, options = {}) {
|
|
|
285
286
|
wait(1000);
|
|
286
287
|
}
|
|
287
288
|
|
|
288
|
-
|
|
289
|
+
logTerminal("error", `镜像健康检查失败,最终状态: ${lastStatus}`);
|
|
289
290
|
run(`docker logs ${containerName}`);
|
|
290
291
|
throw new Error(`镜像 ${imageTag} 未通过健康检查,已阻止推送`);
|
|
291
292
|
} finally {
|
|
292
293
|
try {
|
|
293
294
|
run(`docker rm --force ${containerName}`, { silent: true });
|
|
294
295
|
} catch (cleanupError) {
|
|
295
|
-
|
|
296
|
+
logTerminal("warning", `警告: 临时容器 ${containerName} 清理失败: ${cleanupError.message}`);
|
|
296
297
|
}
|
|
297
298
|
}
|
|
298
299
|
}
|
|
299
300
|
|
|
300
301
|
function startDockerDesktop() {
|
|
301
|
-
|
|
302
|
+
logTerminal("warning", "Docker Desktop 未运行,正在启动...");
|
|
302
303
|
try {
|
|
303
304
|
try {
|
|
304
305
|
execSync(
|
|
@@ -316,7 +317,7 @@ function startDockerDesktop() {
|
|
|
316
317
|
for (let i = 0; i < 30; i++) {
|
|
317
318
|
try {
|
|
318
319
|
execSync("docker info", { stdio: "ignore" });
|
|
319
|
-
|
|
320
|
+
logTerminal("success", "Docker 已就绪");
|
|
320
321
|
return true;
|
|
321
322
|
} catch {}
|
|
322
323
|
process.stdout.write(".");
|
|
@@ -351,10 +352,11 @@ async function dockerBuildPush(options = {}, dependencies = {}) {
|
|
|
351
352
|
// 1. 检测运行模式并定位关键路径
|
|
352
353
|
const cwd = path.resolve(process.cwd());
|
|
353
354
|
const { isStandalone, contextRoot } = detectMode({ standalone });
|
|
355
|
+
const mode = isStandalone ? "standalone" : "monorepo";
|
|
354
356
|
|
|
355
357
|
const appPkgPath = path.join(cwd, "package.json");
|
|
356
358
|
if (!fs.existsSync(appPkgPath)) {
|
|
357
|
-
|
|
359
|
+
logTerminal("error", "错误: 当前目录下缺少 package.json,请在 app 目录内执行");
|
|
358
360
|
process.exit(1);
|
|
359
361
|
}
|
|
360
362
|
|
|
@@ -364,7 +366,7 @@ async function dockerBuildPush(options = {}, dependencies = {}) {
|
|
|
364
366
|
isStandalone,
|
|
365
367
|
});
|
|
366
368
|
if (!fs.existsSync(path.join(buildContext, dockerfileArg))) {
|
|
367
|
-
|
|
369
|
+
logTerminal("error", `错误: 缺少 Dockerfile: ${dockerfileArg}`);
|
|
368
370
|
process.exit(1);
|
|
369
371
|
}
|
|
370
372
|
|
|
@@ -391,23 +393,23 @@ async function dockerBuildPush(options = {}, dependencies = {}) {
|
|
|
391
393
|
|
|
392
394
|
// 5. Docker Desktop 自动启动(仅本地模式)
|
|
393
395
|
if (local) {
|
|
394
|
-
console.log("
|
|
396
|
+
console.log("检查 Docker 运行状态");
|
|
395
397
|
try {
|
|
396
398
|
execSync("docker info", { stdio: "ignore" });
|
|
397
|
-
|
|
399
|
+
logTerminal("success", "Docker 已运行");
|
|
398
400
|
} catch {
|
|
399
401
|
const ok = startDockerDesktop();
|
|
400
402
|
if (!ok) {
|
|
401
|
-
|
|
403
|
+
logTerminal("error", "Docker Desktop 启动超时,请手动启动后重试");
|
|
402
404
|
process.exit(1);
|
|
403
405
|
}
|
|
404
406
|
}
|
|
405
407
|
}
|
|
406
408
|
|
|
407
409
|
// 6. 打印信息
|
|
408
|
-
console.log("
|
|
410
|
+
console.log("==========================================");
|
|
409
411
|
console.log("构建并推送 Docker 镜像");
|
|
410
|
-
console.log("
|
|
412
|
+
console.log("==========================================");
|
|
411
413
|
console.log(`运行模式: ${modeLabel}`);
|
|
412
414
|
console.log(`镜像名称: ${imageName}`);
|
|
413
415
|
console.log(`环境: ${env}`);
|
|
@@ -420,7 +422,7 @@ async function dockerBuildPush(options = {}, dependencies = {}) {
|
|
|
420
422
|
console.log("");
|
|
421
423
|
|
|
422
424
|
// 7. 构建
|
|
423
|
-
console.log("
|
|
425
|
+
console.log("开始构建 Docker 镜像");
|
|
424
426
|
let effectiveBuildContext = buildContext;
|
|
425
427
|
let preparedContext;
|
|
426
428
|
if (!isStandalone && !dryRun) {
|
|
@@ -435,7 +437,7 @@ async function dockerBuildPush(options = {}, dependencies = {}) {
|
|
|
435
437
|
tags.forEach((tag) => buildArgs.push("-t", tag));
|
|
436
438
|
buildArgs.push(
|
|
437
439
|
"--build-arg", `GIT_COMMIT=${fullCommit}`,
|
|
438
|
-
"--build-arg", `BUILD_MODE=${
|
|
440
|
+
"--build-arg", `BUILD_MODE=${mode}`,
|
|
439
441
|
"--build-arg", `PROJECT_DIR=${projectDirArg}`,
|
|
440
442
|
"--label", `org.opencontainers.image.revision=${fullCommit}`,
|
|
441
443
|
"--label", `org.opencontainers.image.version=${commitHash}`
|
|
@@ -457,7 +459,7 @@ async function dockerBuildPush(options = {}, dependencies = {}) {
|
|
|
457
459
|
} finally {
|
|
458
460
|
if (preparedContext) fs.rmSync(preparedContext.contextRoot, { recursive: true, force: true });
|
|
459
461
|
}
|
|
460
|
-
|
|
462
|
+
logTerminal("success", "镜像构建完成");
|
|
461
463
|
|
|
462
464
|
// 8. 可选镜像健康门禁(失败会抛错,任何标签都不会推送)
|
|
463
465
|
if (healthCheck) {
|
|
@@ -471,16 +473,14 @@ async function dockerBuildPush(options = {}, dependencies = {}) {
|
|
|
471
473
|
|
|
472
474
|
// 9. 推送
|
|
473
475
|
console.log("");
|
|
474
|
-
console.log(
|
|
476
|
+
console.log(`推送镜像到 ${registry}`);
|
|
475
477
|
for (const tag of tags) {
|
|
476
478
|
run(`docker push ${tag}`, { dryRun });
|
|
477
479
|
}
|
|
478
480
|
|
|
479
481
|
// 10. 结果摘要
|
|
480
482
|
console.log("");
|
|
481
|
-
|
|
482
|
-
console.log("✅ 完成");
|
|
483
|
-
console.log("==========================================\x1b[0m");
|
|
483
|
+
logTerminal("success", "Docker 镜像流程已结束");
|
|
484
484
|
console.log("可用标签:");
|
|
485
485
|
console.log(` ${tags[0]} (commit 精确版本)`);
|
|
486
486
|
if (!commitOnly) {
|
|
@@ -493,7 +493,7 @@ async function dockerBuildPush(options = {}, dependencies = {}) {
|
|
|
493
493
|
}
|
|
494
494
|
console.log("");
|
|
495
495
|
|
|
496
|
-
return { mode
|
|
496
|
+
return { mode, buildContext, imageName, commitHash, tags };
|
|
497
497
|
}
|
|
498
498
|
|
|
499
499
|
module.exports = {
|