@infly/libs 2.2.0 → 2.2.2
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 +49 -11
- package/adapters/webpack/auto-export.js +52 -52
- package/bin/cli.js +10 -0
- package/build/build-utils/command.js +57 -57
- package/build/build-utils/git.js +254 -254
- package/build/build-utils/preview.js +78 -78
- package/build/build-utils/webhook-single.js +136 -136
- package/build/docker/compose-command.js +6 -8
- package/build/docker/compose-release.js +79 -79
- package/build/docker/config-loader.js +127 -0
- package/build/docker/deployment/release.sh +9 -4
- package/build/docker/deployment-bundle.js +3 -4
- package/build/docker/docker-build-push.js +14 -7
- package/build/docker/release-docker.js +2 -5
- package/build/docker/target-release.js +93 -21
- package/build/project-files.js +47 -47
- package/core/async/concurrency.js +29 -29
- package/package.json +1 -1
- package/script/git-automation/index.js +703 -703
- package/script/git-automation/submodule-utils.js +16 -16
- package/workspace/config.js +144 -144
|
@@ -1,136 +1,136 @@
|
|
|
1
|
-
function shouldSkipWebhook() {
|
|
2
|
-
const args = process.argv || [];
|
|
3
|
-
const hasNoWebhookArg = args.some((arg = "") =>
|
|
4
|
-
["--nowebhook", "--noWebhook", "--no-webhook"].includes(arg)
|
|
5
|
-
);
|
|
6
|
-
|
|
7
|
-
const env = process.env || {};
|
|
8
|
-
const envValues = [
|
|
9
|
-
env.npm_config_nowebhook,
|
|
10
|
-
env.npm_config_noWebhook,
|
|
11
|
-
env.NO_WEBHOOK,
|
|
12
|
-
env.DISABLE_WEBHOOK
|
|
13
|
-
];
|
|
14
|
-
const hasNoWebhookEnv = envValues.some((value) => {
|
|
15
|
-
const normalized = String(value || "").toLowerCase();
|
|
16
|
-
return ["1", "true", "yes", "on"].includes(normalized);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
return hasNoWebhookArg || hasNoWebhookEnv;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function getWebhookMessage() {
|
|
23
|
-
const args = process.argv || [];
|
|
24
|
-
const env = process.env || {};
|
|
25
|
-
const messageEnv =
|
|
26
|
-
env.npm_config_m ||
|
|
27
|
-
env.npm_config_message ||
|
|
28
|
-
env.WEBHOOK_MESSAGE ||
|
|
29
|
-
env.BUILD_MESSAGE ||
|
|
30
|
-
"";
|
|
31
|
-
|
|
32
|
-
if (messageEnv) {
|
|
33
|
-
return String(messageEnv).trim();
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
for (let index = 0; index < args.length; index += 1) {
|
|
37
|
-
const arg = args[index] || "";
|
|
38
|
-
|
|
39
|
-
if (arg === "-m" || arg === "--message") {
|
|
40
|
-
return String(args[index + 1] || "").trim();
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (arg.startsWith("-m=")) {
|
|
44
|
-
return arg.slice(3).trim();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (arg.startsWith("--message=")) {
|
|
48
|
-
return arg.slice(10).trim();
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return "";
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function postVersionFileAndMsg(publishText, extraParams = {}) {
|
|
56
|
-
const {
|
|
57
|
-
projectName,
|
|
58
|
-
newFileName,
|
|
59
|
-
gitInfo,
|
|
60
|
-
webhookUrl = "", // 启用企业微信推送
|
|
61
|
-
webhookAtUser = [],
|
|
62
|
-
webhookExtraText = "",
|
|
63
|
-
gitAutoPushRepos,
|
|
64
|
-
gitAutoPushReposBranchMap,
|
|
65
|
-
repos,
|
|
66
|
-
deployUrl = ""
|
|
67
|
-
} = extraParams || {};
|
|
68
|
-
const { branch, commitMsg, author } = gitInfo || {};
|
|
69
|
-
const webhookMessage = getWebhookMessage();
|
|
70
|
-
const GITPATH = `https://gitee.com/gdinfly_1/${projectName}/blob/${branch}/${newFileName}`;
|
|
71
|
-
const UPLOADURL = `https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=&type=file`;
|
|
72
|
-
/* const data = {
|
|
73
|
-
msgtype: "markdown",
|
|
74
|
-
markdown: {
|
|
75
|
-
content: `${publishText}\n构建文件:[点击下载](${GITPATH})`,
|
|
76
|
-
},
|
|
77
|
-
};
|
|
78
|
-
if (Array.isArray(webhookAtUser) && webhookAtUser.length > 0) {
|
|
79
|
-
webhookAtUser.forEach((item, index) => {
|
|
80
|
-
data.markdown.content = `${data.markdown.content}\n<@${item}>`;
|
|
81
|
-
});
|
|
82
|
-
} */
|
|
83
|
-
let content = publishText;
|
|
84
|
-
if (gitAutoPushReposBranchMap) {
|
|
85
|
-
content = [
|
|
86
|
-
publishText,
|
|
87
|
-
`构建文件:已推送对应仓库【${repos}】- 分支【${gitAutoPushReposBranchMap[branch]}】`,
|
|
88
|
-
`构建人:${author}`,
|
|
89
|
-
deployUrl ? `部署系统:${deployUrl}` : ""
|
|
90
|
-
].filter(Boolean).join("\n");
|
|
91
|
-
} else {
|
|
92
|
-
content = `${publishText}\n构建文件下载地址:${GITPATH}\n${webhookExtraText}\n`;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (webhookMessage) {
|
|
96
|
-
content = `${content}\n${webhookMessage}`;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// 防御性过滤:防止运行时数据中的 %s 等格式化占位符泄露到推送内容
|
|
100
|
-
content = (content || "").replace(/%s/g, "");
|
|
101
|
-
|
|
102
|
-
const data = {
|
|
103
|
-
msgtype: "text",
|
|
104
|
-
text: {
|
|
105
|
-
content,
|
|
106
|
-
mentioned_mobile_list: Array.isArray(webhookAtUser) ? webhookAtUser : webhookAtUser[branch] // 支持按分支配置不同的提醒人
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
if (shouldSkipWebhook()) {
|
|
111
|
-
console.log(global.logColor?.info || "%s", "🔕 已跳过企业微信机器人推送(nowebhook)");
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
try {
|
|
116
|
-
const axios = require("axios");
|
|
117
|
-
|
|
118
|
-
if (axios) {
|
|
119
|
-
axios
|
|
120
|
-
.post(webhookUrl, data, {
|
|
121
|
-
headers: {
|
|
122
|
-
"Content-Type": "application/json"
|
|
123
|
-
}
|
|
124
|
-
})
|
|
125
|
-
.catch((e) => {
|
|
126
|
-
console.error("请求出错:", e);
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
} catch (error) {
|
|
130
|
-
console.error("axios is not available, please install it.");
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
module.exports = {
|
|
135
|
-
postVersionFileAndMsg
|
|
136
|
-
};
|
|
1
|
+
function shouldSkipWebhook() {
|
|
2
|
+
const args = process.argv || [];
|
|
3
|
+
const hasNoWebhookArg = args.some((arg = "") =>
|
|
4
|
+
["--nowebhook", "--noWebhook", "--no-webhook"].includes(arg)
|
|
5
|
+
);
|
|
6
|
+
|
|
7
|
+
const env = process.env || {};
|
|
8
|
+
const envValues = [
|
|
9
|
+
env.npm_config_nowebhook,
|
|
10
|
+
env.npm_config_noWebhook,
|
|
11
|
+
env.NO_WEBHOOK,
|
|
12
|
+
env.DISABLE_WEBHOOK
|
|
13
|
+
];
|
|
14
|
+
const hasNoWebhookEnv = envValues.some((value) => {
|
|
15
|
+
const normalized = String(value || "").toLowerCase();
|
|
16
|
+
return ["1", "true", "yes", "on"].includes(normalized);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
return hasNoWebhookArg || hasNoWebhookEnv;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function getWebhookMessage() {
|
|
23
|
+
const args = process.argv || [];
|
|
24
|
+
const env = process.env || {};
|
|
25
|
+
const messageEnv =
|
|
26
|
+
env.npm_config_m ||
|
|
27
|
+
env.npm_config_message ||
|
|
28
|
+
env.WEBHOOK_MESSAGE ||
|
|
29
|
+
env.BUILD_MESSAGE ||
|
|
30
|
+
"";
|
|
31
|
+
|
|
32
|
+
if (messageEnv) {
|
|
33
|
+
return String(messageEnv).trim();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
37
|
+
const arg = args[index] || "";
|
|
38
|
+
|
|
39
|
+
if (arg === "-m" || arg === "--message") {
|
|
40
|
+
return String(args[index + 1] || "").trim();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (arg.startsWith("-m=")) {
|
|
44
|
+
return arg.slice(3).trim();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (arg.startsWith("--message=")) {
|
|
48
|
+
return arg.slice(10).trim();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return "";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function postVersionFileAndMsg(publishText, extraParams = {}) {
|
|
56
|
+
const {
|
|
57
|
+
projectName,
|
|
58
|
+
newFileName,
|
|
59
|
+
gitInfo,
|
|
60
|
+
webhookUrl = "", // 启用企业微信推送
|
|
61
|
+
webhookAtUser = [],
|
|
62
|
+
webhookExtraText = "",
|
|
63
|
+
gitAutoPushRepos,
|
|
64
|
+
gitAutoPushReposBranchMap,
|
|
65
|
+
repos,
|
|
66
|
+
deployUrl = ""
|
|
67
|
+
} = extraParams || {};
|
|
68
|
+
const { branch, commitMsg, author } = gitInfo || {};
|
|
69
|
+
const webhookMessage = getWebhookMessage();
|
|
70
|
+
const GITPATH = `https://gitee.com/gdinfly_1/${projectName}/blob/${branch}/${newFileName}`;
|
|
71
|
+
const UPLOADURL = `https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=&type=file`;
|
|
72
|
+
/* const data = {
|
|
73
|
+
msgtype: "markdown",
|
|
74
|
+
markdown: {
|
|
75
|
+
content: `${publishText}\n构建文件:[点击下载](${GITPATH})`,
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
if (Array.isArray(webhookAtUser) && webhookAtUser.length > 0) {
|
|
79
|
+
webhookAtUser.forEach((item, index) => {
|
|
80
|
+
data.markdown.content = `${data.markdown.content}\n<@${item}>`;
|
|
81
|
+
});
|
|
82
|
+
} */
|
|
83
|
+
let content = publishText;
|
|
84
|
+
if (gitAutoPushReposBranchMap) {
|
|
85
|
+
content = [
|
|
86
|
+
publishText,
|
|
87
|
+
`构建文件:已推送对应仓库【${repos}】- 分支【${gitAutoPushReposBranchMap[branch]}】`,
|
|
88
|
+
`构建人:${author}`,
|
|
89
|
+
deployUrl ? `部署系统:${deployUrl}` : ""
|
|
90
|
+
].filter(Boolean).join("\n");
|
|
91
|
+
} else {
|
|
92
|
+
content = `${publishText}\n构建文件下载地址:${GITPATH}\n${webhookExtraText}\n`;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (webhookMessage) {
|
|
96
|
+
content = `${content}\n${webhookMessage}`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 防御性过滤:防止运行时数据中的 %s 等格式化占位符泄露到推送内容
|
|
100
|
+
content = (content || "").replace(/%s/g, "");
|
|
101
|
+
|
|
102
|
+
const data = {
|
|
103
|
+
msgtype: "text",
|
|
104
|
+
text: {
|
|
105
|
+
content,
|
|
106
|
+
mentioned_mobile_list: Array.isArray(webhookAtUser) ? webhookAtUser : webhookAtUser[branch] // 支持按分支配置不同的提醒人
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
if (shouldSkipWebhook()) {
|
|
111
|
+
console.log(global.logColor?.info || "%s", "🔕 已跳过企业微信机器人推送(nowebhook)");
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
const axios = require("axios");
|
|
117
|
+
|
|
118
|
+
if (axios) {
|
|
119
|
+
axios
|
|
120
|
+
.post(webhookUrl, data, {
|
|
121
|
+
headers: {
|
|
122
|
+
"Content-Type": "application/json"
|
|
123
|
+
}
|
|
124
|
+
})
|
|
125
|
+
.catch((e) => {
|
|
126
|
+
console.error("请求出错:", e);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
} catch (error) {
|
|
130
|
+
console.error("axios is not available, please install it.");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
module.exports = {
|
|
135
|
+
postVersionFileAndMsg
|
|
136
|
+
};
|
|
@@ -6,13 +6,12 @@ const {
|
|
|
6
6
|
selectOption: defaultSelectOption,
|
|
7
7
|
} = require("../../cli/select-option");
|
|
8
8
|
const { composeRelease: defaultComposeRelease } = require("./compose-release");
|
|
9
|
-
const {
|
|
9
|
+
const { loadDockerConfig } = require("./config-loader");
|
|
10
10
|
|
|
11
11
|
function loadTargetConfig(configPath) {
|
|
12
12
|
const resolvedPath = path.resolve(configPath);
|
|
13
|
-
delete require.cache[require.resolve(resolvedPath)];
|
|
14
13
|
return {
|
|
15
|
-
config:
|
|
14
|
+
config: loadDockerConfig({ projectDir: path.dirname(resolvedPath), configPath: path.basename(resolvedPath) }),
|
|
16
15
|
configDir: path.dirname(resolvedPath),
|
|
17
16
|
};
|
|
18
17
|
}
|
|
@@ -22,10 +21,8 @@ function loadPackageTargetConfig(cwd = process.cwd()) {
|
|
|
22
21
|
if (!fs.existsSync(packagePath)) {
|
|
23
22
|
throw new Error(`Package config not found: ${packagePath}`);
|
|
24
23
|
}
|
|
25
|
-
delete require.cache[require.resolve(packagePath)];
|
|
26
|
-
const packageJson = require(packagePath);
|
|
27
24
|
return {
|
|
28
|
-
config:
|
|
25
|
+
config: loadDockerConfig({ projectDir: path.dirname(packagePath) }),
|
|
29
26
|
configDir: path.dirname(packagePath),
|
|
30
27
|
};
|
|
31
28
|
}
|
|
@@ -40,9 +37,10 @@ async function runComposeCommand(options = {}, dependencies = {}) {
|
|
|
40
37
|
const loaded = options.config
|
|
41
38
|
? loadTargetConfig(options.config)
|
|
42
39
|
: options.configObject
|
|
43
|
-
? { config:
|
|
40
|
+
? { config: loadDockerConfig({ configObject: options.configObject,
|
|
41
|
+
configDir: options.configDir || process.cwd() }), configDir: options.configDir || process.cwd() }
|
|
44
42
|
: loadPackageTargetConfig(options.cwd || process.cwd());
|
|
45
|
-
const config =
|
|
43
|
+
const config = loaded.config;
|
|
46
44
|
const targets = config?.targets;
|
|
47
45
|
if (!targets || Object.keys(targets).length === 0) {
|
|
48
46
|
throw new Error("Docker target config must define at least one target.");
|
|
@@ -1,98 +1,98 @@
|
|
|
1
|
-
const { execFileSync } = require("node:child_process");
|
|
2
|
-
const fs = require("node:fs");
|
|
3
|
-
const path = require("node:path");
|
|
4
|
-
|
|
1
|
+
const { execFileSync } = require("node:child_process");
|
|
2
|
+
const fs = require("node:fs");
|
|
3
|
+
const path = require("node:path");
|
|
4
|
+
|
|
5
5
|
function bumpComposeImageVersion(content, bump = "patch") {
|
|
6
6
|
if (!["none", "patch"].includes(bump)) {
|
|
7
7
|
throw new Error(`Unsupported version bump: ${bump}. Supported values: none, patch.`);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const imageRegex = /^(\s*image:\s*)([^\s#]+):(\d+)\.(\d+)\.(\d+)(\s*(?:#.*)?)$/gm;
|
|
11
|
-
const matches = [...content.matchAll(imageRegex)];
|
|
12
|
-
|
|
13
|
-
if (matches.length !== 1) {
|
|
14
|
-
throw new Error(`Compose file must contain exactly one versioned image; found ${matches.length}.`);
|
|
15
|
-
}
|
|
16
|
-
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const imageRegex = /^(\s*image:\s*)([^\s#]+):(\d+)\.(\d+)\.(\d+)(\s*(?:#.*)?)$/gm;
|
|
11
|
+
const matches = [...content.matchAll(imageRegex)];
|
|
12
|
+
|
|
13
|
+
if (matches.length !== 1) {
|
|
14
|
+
throw new Error(`Compose file must contain exactly one versioned image; found ${matches.length}.`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
17
|
const [, prefix, image, major, minor, patchVersion, suffix] = matches[0];
|
|
18
18
|
const from = `${image}:${major}.${minor}.${patchVersion}`;
|
|
19
19
|
const to = bump === "none"
|
|
20
20
|
? from
|
|
21
21
|
: `${image}:${major}.${minor}.${Number(patchVersion) + 1}`;
|
|
22
22
|
const replacement = `${prefix}${to}${suffix}`;
|
|
23
|
-
|
|
24
|
-
return {
|
|
25
|
-
content: content.replace(imageRegex, replacement),
|
|
26
|
-
from,
|
|
27
|
-
to,
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function resolveComposePath(projectDir, file) {
|
|
32
|
-
if (!projectDir) throw new Error("--project-dir is required.");
|
|
33
|
-
if (!file) throw new Error("--file is required.");
|
|
34
|
-
|
|
35
|
-
const resolvedProjectDir = path.resolve(projectDir);
|
|
36
|
-
const composePath = path.resolve(resolvedProjectDir, file);
|
|
37
|
-
const relative = path.relative(resolvedProjectDir, composePath);
|
|
38
|
-
|
|
39
|
-
if (relative.startsWith("..") || path.isAbsolute(relative)) {
|
|
40
|
-
throw new Error("Compose file must be inside project-dir.");
|
|
41
|
-
}
|
|
42
|
-
if (!fs.existsSync(composePath)) {
|
|
43
|
-
throw new Error(`Compose file not found: ${composePath}`);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return { projectDir: resolvedProjectDir, composePath };
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function defaultRunDocker(args, options) {
|
|
50
|
-
execFileSync("docker", args, { cwd: options.cwd, stdio: "inherit" });
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function composeRelease(options = {}, dependencies = {}) {
|
|
54
|
-
const {
|
|
55
|
-
projectDir,
|
|
56
|
-
file,
|
|
57
|
-
bump = "patch",
|
|
58
|
-
push = false,
|
|
59
|
-
dryRun = false,
|
|
60
|
-
} = options;
|
|
61
|
-
const runDocker = dependencies.runDocker || defaultRunDocker;
|
|
62
|
-
const resolved = resolveComposePath(projectDir, file);
|
|
63
|
-
const original = fs.readFileSync(resolved.composePath, "utf8");
|
|
64
|
-
const updated = bumpComposeImageVersion(original, bump);
|
|
65
|
-
const baseArgs = ["compose", "-f", resolved.composePath];
|
|
66
|
-
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
content: content.replace(imageRegex, replacement),
|
|
26
|
+
from,
|
|
27
|
+
to,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function resolveComposePath(projectDir, file) {
|
|
32
|
+
if (!projectDir) throw new Error("--project-dir is required.");
|
|
33
|
+
if (!file) throw new Error("--file is required.");
|
|
34
|
+
|
|
35
|
+
const resolvedProjectDir = path.resolve(projectDir);
|
|
36
|
+
const composePath = path.resolve(resolvedProjectDir, file);
|
|
37
|
+
const relative = path.relative(resolvedProjectDir, composePath);
|
|
38
|
+
|
|
39
|
+
if (relative.startsWith("..") || path.isAbsolute(relative)) {
|
|
40
|
+
throw new Error("Compose file must be inside project-dir.");
|
|
41
|
+
}
|
|
42
|
+
if (!fs.existsSync(composePath)) {
|
|
43
|
+
throw new Error(`Compose file not found: ${composePath}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return { projectDir: resolvedProjectDir, composePath };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function defaultRunDocker(args, options) {
|
|
50
|
+
execFileSync("docker", args, { cwd: options.cwd, stdio: "inherit" });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function composeRelease(options = {}, dependencies = {}) {
|
|
54
|
+
const {
|
|
55
|
+
projectDir,
|
|
56
|
+
file,
|
|
57
|
+
bump = "patch",
|
|
58
|
+
push = false,
|
|
59
|
+
dryRun = false,
|
|
60
|
+
} = options;
|
|
61
|
+
const runDocker = dependencies.runDocker || defaultRunDocker;
|
|
62
|
+
const resolved = resolveComposePath(projectDir, file);
|
|
63
|
+
const original = fs.readFileSync(resolved.composePath, "utf8");
|
|
64
|
+
const updated = bumpComposeImageVersion(original, bump);
|
|
65
|
+
const baseArgs = ["compose", "-f", resolved.composePath];
|
|
66
|
+
|
|
67
67
|
if (bump === "none") {
|
|
68
68
|
console.log(`Version fixed: ${updated.from}`);
|
|
69
69
|
} else {
|
|
70
70
|
console.log(`Version update: ${updated.from} -> ${updated.to}`);
|
|
71
71
|
}
|
|
72
|
-
|
|
73
|
-
if (dryRun) {
|
|
74
|
-
console.log(`[dry-run] docker ${[...baseArgs, "config"].join(" ")}`);
|
|
75
|
-
console.log(`[dry-run] docker ${[...baseArgs, "build"].join(" ")}`);
|
|
76
|
-
if (push) console.log(`[dry-run] docker ${[...baseArgs, "push"].join(" ")}`);
|
|
77
|
-
return { ...updated, projectDir: resolved.projectDir, composePath: resolved.composePath };
|
|
78
|
-
}
|
|
79
|
-
|
|
72
|
+
|
|
73
|
+
if (dryRun) {
|
|
74
|
+
console.log(`[dry-run] docker ${[...baseArgs, "config"].join(" ")}`);
|
|
75
|
+
console.log(`[dry-run] docker ${[...baseArgs, "build"].join(" ")}`);
|
|
76
|
+
if (push) console.log(`[dry-run] docker ${[...baseArgs, "push"].join(" ")}`);
|
|
77
|
+
return { ...updated, projectDir: resolved.projectDir, composePath: resolved.composePath };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
80
|
const versionChanged = updated.content !== original;
|
|
81
81
|
if (versionChanged) fs.writeFileSync(resolved.composePath, updated.content, "utf8");
|
|
82
82
|
try {
|
|
83
|
-
runDocker([...baseArgs, "config"], { cwd: resolved.projectDir });
|
|
84
|
-
runDocker([...baseArgs, "build"], { cwd: resolved.projectDir });
|
|
85
|
-
if (push) runDocker([...baseArgs, "push"], { cwd: resolved.projectDir });
|
|
83
|
+
runDocker([...baseArgs, "config"], { cwd: resolved.projectDir });
|
|
84
|
+
runDocker([...baseArgs, "build"], { cwd: resolved.projectDir });
|
|
85
|
+
if (push) runDocker([...baseArgs, "push"], { cwd: resolved.projectDir });
|
|
86
86
|
} catch (error) {
|
|
87
87
|
if (versionChanged) fs.writeFileSync(resolved.composePath, original, "utf8");
|
|
88
|
-
throw error;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return { ...updated, projectDir: resolved.projectDir, composePath: resolved.composePath };
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
module.exports = {
|
|
95
|
-
bumpComposeImageVersion,
|
|
96
|
-
composeRelease,
|
|
97
|
-
resolveComposePath,
|
|
98
|
-
};
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return { ...updated, projectDir: resolved.projectDir, composePath: resolved.composePath };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
module.exports = {
|
|
95
|
+
bumpComposeImageVersion,
|
|
96
|
+
composeRelease,
|
|
97
|
+
resolveComposePath,
|
|
98
|
+
};
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
const fs = require("node:fs");
|
|
2
|
+
const path = require("node:path");
|
|
3
|
+
const YAML = require("yaml");
|
|
4
|
+
const { mergeTargetConfig, resolveDockerConfig } = require("./target-config");
|
|
5
|
+
|
|
6
|
+
function readConfigFile(file) {
|
|
7
|
+
const resolved = path.resolve(file);
|
|
8
|
+
const extension = path.extname(resolved).toLowerCase();
|
|
9
|
+
if (extension === ".js" || extension === ".cjs") {
|
|
10
|
+
delete require.cache[require.resolve(resolved)];
|
|
11
|
+
return require(resolved);
|
|
12
|
+
}
|
|
13
|
+
const content = fs.readFileSync(resolved, "utf8");
|
|
14
|
+
if (extension === ".yaml" || extension === ".yml") return YAML.parse(content);
|
|
15
|
+
if (extension === ".json" || path.basename(resolved) === "package.json") return JSON.parse(content);
|
|
16
|
+
throw new Error(`不支持的 Docker 配置格式: ${resolved}`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function expandConfigReference(config, configDir, chain = []) {
|
|
20
|
+
if (!config || typeof config !== "object" || Array.isArray(config)) return config;
|
|
21
|
+
if (config.config == null) return config;
|
|
22
|
+
if (typeof config.config !== "string" || !config.config.trim()) {
|
|
23
|
+
throw new Error("infly.docker.config 必须是非空 JSON/YAML 配置路径");
|
|
24
|
+
}
|
|
25
|
+
const file = path.resolve(configDir, config.config);
|
|
26
|
+
if (chain.includes(file)) throw new Error(`Docker 配置循环引用: ${[...chain, file].join(" -> ")}`);
|
|
27
|
+
const external = expandConfigReference(readConfigFile(file), path.dirname(file), [...chain, file]);
|
|
28
|
+
const inline = { ...config };
|
|
29
|
+
delete inline.config;
|
|
30
|
+
return mergeTargetConfig(external, inline);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function imageRepository(image, name) {
|
|
34
|
+
if (typeof image !== "string" || !image.trim()) throw new Error(`${name} Compose service 缺少 image`);
|
|
35
|
+
const value = image.trim();
|
|
36
|
+
const slash = value.lastIndexOf("/");
|
|
37
|
+
const colon = value.lastIndexOf(":");
|
|
38
|
+
return colon > slash ? value.slice(0, colon) : value;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function relativeToProject(projectDir, value, name) {
|
|
42
|
+
const relative = path.relative(projectDir, value);
|
|
43
|
+
if (!relative || relative === ".") return ".";
|
|
44
|
+
if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
|
|
45
|
+
throw new Error(`${name} 必须位于项目目录内`);
|
|
46
|
+
}
|
|
47
|
+
return relative.split(path.sep).join("/");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function composeComponent(projectDir, reference, name) {
|
|
51
|
+
if (!reference || typeof reference.file !== "string" || typeof reference.service !== "string") {
|
|
52
|
+
throw new Error(`${name}.compose 必须配置 file 和 service`);
|
|
53
|
+
}
|
|
54
|
+
const file = path.resolve(projectDir, reference.file);
|
|
55
|
+
relativeToProject(projectDir, file, `${name} compose.file`);
|
|
56
|
+
const compose = readConfigFile(file);
|
|
57
|
+
const service = compose?.services?.[reference.service];
|
|
58
|
+
if (!service) throw new Error(`${name} Compose 中不存在服务: ${reference.service}`);
|
|
59
|
+
const build = service.build;
|
|
60
|
+
if (!build || typeof build !== "object" || Array.isArray(build) || typeof build.context !== "string") {
|
|
61
|
+
throw new Error(`${name} Compose service 必须配置对象形式的 build.context`);
|
|
62
|
+
}
|
|
63
|
+
const context = path.resolve(path.dirname(file), build.context);
|
|
64
|
+
const dockerfile = path.resolve(context, build.dockerfile || "Dockerfile");
|
|
65
|
+
const buildArgs = {};
|
|
66
|
+
for (const [key, raw] of Object.entries(build.args || {})) {
|
|
67
|
+
const match = typeof raw === "string" ? raw.match(/^[$][{][A-Za-z_][A-Za-z0-9_]*:-([^}]*)[}]$/) : null;
|
|
68
|
+
buildArgs[key] = match ? match[1] : raw;
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
repository: imageRepository(service.image, name),
|
|
72
|
+
context: relativeToProject(projectDir, context, `${name} build.context`),
|
|
73
|
+
dockerfile: relativeToProject(projectDir, dockerfile, `${name} build.dockerfile`),
|
|
74
|
+
...(build.target ? { target: build.target } : {}),
|
|
75
|
+
...(Object.keys(buildArgs).length ? { buildArgs } : {}),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function hydrateComposeComponents(config, projectDir) {
|
|
80
|
+
for (const [targetName, target] of Object.entries(config?.targets || {})) {
|
|
81
|
+
for (const [componentName, component] of Object.entries(target.components || {})) {
|
|
82
|
+
if (!component.compose) continue;
|
|
83
|
+
target.components[componentName] = mergeTargetConfig(
|
|
84
|
+
composeComponent(projectDir, component.compose, `${targetName}.${componentName}`),
|
|
85
|
+
component,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return config;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function inferReleaseScripts(scripts) {
|
|
93
|
+
const result = {};
|
|
94
|
+
for (const name of Object.keys(scripts || {})) {
|
|
95
|
+
const match = name.match(/^release:([a-z][a-z0-9_-]*):docker$/);
|
|
96
|
+
if (match) result[match[1]] = name;
|
|
97
|
+
}
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function loadDockerConfig(options = {}) {
|
|
102
|
+
const projectDir = path.resolve(options.projectDir || process.cwd());
|
|
103
|
+
let rawConfig;
|
|
104
|
+
let configDir = projectDir;
|
|
105
|
+
let manifest;
|
|
106
|
+
if (options.configObject) {
|
|
107
|
+
rawConfig = options.configObject;
|
|
108
|
+
configDir = path.resolve(options.configDir || projectDir);
|
|
109
|
+
} else if (options.configPath) {
|
|
110
|
+
const file = path.resolve(projectDir, options.configPath);
|
|
111
|
+
rawConfig = readConfigFile(file);
|
|
112
|
+
configDir = path.dirname(file);
|
|
113
|
+
const manifestFile = path.join(projectDir, "package.json");
|
|
114
|
+
if (fs.existsSync(manifestFile)) manifest = readConfigFile(manifestFile);
|
|
115
|
+
} else {
|
|
116
|
+
manifest = readConfigFile(path.join(projectDir, "package.json"));
|
|
117
|
+
rawConfig = manifest.infly?.docker;
|
|
118
|
+
}
|
|
119
|
+
const expanded = expandConfigReference(rawConfig, configDir);
|
|
120
|
+
const inferred = inferReleaseScripts(manifest?.scripts);
|
|
121
|
+
const withScripts = Object.keys(inferred).length
|
|
122
|
+
? mergeTargetConfig({ releaseScripts: inferred }, expanded)
|
|
123
|
+
: expanded;
|
|
124
|
+
return hydrateComposeComponents(resolveDockerConfig(withScripts), projectDir);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
module.exports = { composeComponent, expandConfigReference, inferReleaseScripts, loadDockerConfig, readConfigFile };
|
|
@@ -46,7 +46,7 @@ for i in "${!component_names[@]}"; do
|
|
|
46
46
|
[[ "$(grep -c "^${key}=" "$release_env_file" || true)" == 1 ]] || { echo "基线必须包含唯一 $key" >&2; exit 1; }
|
|
47
47
|
image=$(grep "^${key}=" "$release_env_file"); image=${image#*=}
|
|
48
48
|
suffix=${image#"${repositories[$i]}"}
|
|
49
|
-
[[ "$image" != "$suffix" && ( "$suffix" =~ ^:[0-9a-f]{12}$ || "$suffix" =~ ^@sha256:[0-9a-f]{64}$ ) ]] || {
|
|
49
|
+
[[ "$image" != "$suffix" && ( "$suffix" =~ ^:([0-9a-f]{12}|[0-9a-f]{40})$ || "$suffix" =~ ^@sha256:[0-9a-f]{64}$ ) ]] || {
|
|
50
50
|
echo "当前基线不是配置仓库的不可变镜像: $key" >&2; exit 1;
|
|
51
51
|
}
|
|
52
52
|
current_images+=("$image"); next_images+=("$image")
|
|
@@ -59,7 +59,7 @@ for pair in "$@"; do
|
|
|
59
59
|
found=true
|
|
60
60
|
for selected in "${selected_indexes[@]}"; do [[ "$selected" != "$i" ]] || { echo '组件重复' >&2; exit 2; }; done
|
|
61
61
|
suffix=${image#"${repositories[$i]}:"}
|
|
62
|
-
[[ "$suffix" != "$image" && "$suffix" =~ ^[0-9a-f]{12}$ ]] || { echo "只接受本组件正式 commit
|
|
62
|
+
[[ "$suffix" != "$image" && "$suffix" =~ ^([0-9a-f]{12}|[0-9a-f]{40})$ ]] || { echo "只接受本组件正式 commit 镜像(40 位;兼容旧 12 位): $name" >&2; exit 1; }
|
|
63
63
|
next_images[$i]=$image
|
|
64
64
|
selected_indexes+=("$i")
|
|
65
65
|
selected_components+=("$name")
|
|
@@ -128,9 +128,14 @@ compose=(docker compose --env-file "$secret_env_file" --env-file "$next_env" -f
|
|
|
128
128
|
"${compose[@]}" config --quiet
|
|
129
129
|
"${compose[@]}" pull "${selected_services[@]}"
|
|
130
130
|
for i in "${selected_indexes[@]}"; do
|
|
131
|
-
image=${next_images[$i]};
|
|
131
|
+
image=${next_images[$i]}; tag=${image##*:}
|
|
132
132
|
full=$(docker image inspect --format '{{ index .Config.Labels "org.opencontainers.image.revision" }}' "$image")
|
|
133
|
-
[[ "$full" =~ ^[0-9a-f]{40}$
|
|
133
|
+
[[ "$full" =~ ^[0-9a-f]{40}$ ]] || { echo "镜像 revision 校验失败: $image" >&2; exit 1; }
|
|
134
|
+
if [[ ${#tag} -eq 40 ]]; then
|
|
135
|
+
[[ "$full" == "$tag" ]] || { echo "镜像 revision 与 40 位标签不一致: $image" >&2; exit 1; }
|
|
136
|
+
else
|
|
137
|
+
[[ "${full:0:12}" == "$tag" ]] || { echo "旧 12 位镜像 revision 前缀不一致: $image" >&2; exit 1; }
|
|
138
|
+
fi
|
|
134
139
|
done
|
|
135
140
|
mv -f -- "$next_env" "$release_env_file"
|
|
136
141
|
next_env=""
|