@infly/libs 2.6.0 → 2.7.1
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 +21 -1
- 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 +52 -67
- 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/deployment/health-check.sh +5 -1
- package/build/docker/deployment/release.sh +25 -2
- package/build/docker/docker-build-push.js +20 -21
- package/build/docker/release-docker.js +5 -4
- package/build/docker/release-paths.js +30 -30
- package/build/docker/release-validation.js +51 -7
- package/build/docker/target-image.js +44 -6
- package/build/docker/target-release.js +540 -527
- 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/package.json +3 -3
- 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 +862 -828
- package/script/git-automation/submodule-utils.js +16 -16
- package/workspace/config.js +144 -144
|
@@ -19,6 +19,44 @@ const SAFE_DOCKER_PLATFORM = /^[a-z0-9]+\/[a-z0-9]+(?:\/[a-z0-9._-]+)?$/;
|
|
|
19
19
|
const SAFE_VERSION_REQUIREMENT = /^>=(\d+)(?:\.(\d+))?(?:\.(\d+))?$/;
|
|
20
20
|
const PRE_BUILD_MODES = new Set(["always", "monorepo", "standalone"]);
|
|
21
21
|
const BUILD_STRATEGIES = new Set(["artifact", "source"]);
|
|
22
|
+
const ENV_NAME = /^[A-Z_][A-Z0-9_]*$/;
|
|
23
|
+
const DOCKER_NAME = /^[A-Za-z0-9_][A-Za-z0-9_.-]*$/;
|
|
24
|
+
|
|
25
|
+
function validateRepository(repository, name) {
|
|
26
|
+
if (typeof repository !== "string" || !/^[a-z0-9][a-z0-9._:/-]*$/.test(repository)) {
|
|
27
|
+
throw new Error(`${name} 必须配置有效的镜像 repository(不含标签)`);
|
|
28
|
+
}
|
|
29
|
+
if (repository.split("/").at(-1).includes(":")) throw new Error(`${name} repository 不得包含标签`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function validateBuildInputs(component, name) {
|
|
33
|
+
const { buildSecrets, noCacheFilter, buildTimestampArg, buildArgs } = component;
|
|
34
|
+
if (buildSecrets != null) {
|
|
35
|
+
if (!Array.isArray(buildSecrets) || !buildSecrets.length || buildSecrets.some((secret) =>
|
|
36
|
+
!secret || typeof secret !== "object" || Object.keys(secret).some((key) => !["id", "fileEnv", "defaultFile"].includes(key))
|
|
37
|
+
|| typeof secret.id !== "string" || !DOCKER_NAME.test(secret.id)
|
|
38
|
+
|| typeof secret.fileEnv !== "string" || !ENV_NAME.test(secret.fileEnv)
|
|
39
|
+
) || new Set(buildSecrets.map(({ id }) => id)).size !== buildSecrets.length) {
|
|
40
|
+
throw new Error(`${name} buildSecrets 需声明唯一 id 及保存文件路径的 fileEnv`);
|
|
41
|
+
}
|
|
42
|
+
for (const { defaultFile } of buildSecrets) {
|
|
43
|
+
if (defaultFile == null) continue;
|
|
44
|
+
if (typeof defaultFile !== "string" || !defaultFile.trim() || /[,\r\n\0"]/.test(defaultFile)) {
|
|
45
|
+
throw new Error(`${name} buildSecrets.defaultFile 必须是部署仓内的相对文件路径`);
|
|
46
|
+
}
|
|
47
|
+
inside("/config", defaultFile);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (noCacheFilter != null && (!Array.isArray(noCacheFilter) || !noCacheFilter.length
|
|
51
|
+
|| noCacheFilter.some((stage) => typeof stage !== "string" || !DOCKER_NAME.test(stage)))) {
|
|
52
|
+
throw new Error(`${name} noCacheFilter 必须是合法 Docker stage 列表`);
|
|
53
|
+
}
|
|
54
|
+
if (buildTimestampArg != null && (typeof buildTimestampArg !== "string"
|
|
55
|
+
|| !ENV_NAME.test(buildTimestampArg) || Object.hasOwn(buildArgs || {}, buildTimestampArg)
|
|
56
|
+
|| ["BUILD_MODE", "PROJECT_DIR", "REFRESH_INTERNAL"].includes(buildTimestampArg))) {
|
|
57
|
+
throw new Error(`${name} buildTimestampArg 必须是未被占用的构建参数名`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
22
60
|
|
|
23
61
|
function normalizePreBuildCommand(value, target) {
|
|
24
62
|
const entry = Array.isArray(value) ? { command: value, when: "always" } : value;
|
|
@@ -82,10 +120,14 @@ function validateReleaseProfile(profile, target) {
|
|
|
82
120
|
}
|
|
83
121
|
|
|
84
122
|
function validateComponent(component, name, guardedByProfile = false) {
|
|
85
|
-
if (
|
|
86
|
-
|
|
123
|
+
if (component?.repositoryEnv != null) {
|
|
124
|
+
if (typeof component.repositoryEnv !== "string" || !ENV_NAME.test(component.repositoryEnv) || component.repository != null) {
|
|
125
|
+
throw new Error(`${name} repositoryEnv 必须是环境变量名,且不能同时配置 repository`);
|
|
126
|
+
}
|
|
127
|
+
} else {
|
|
128
|
+
validateRepository(component?.repository, name);
|
|
87
129
|
}
|
|
88
|
-
|
|
130
|
+
validateBuildInputs(component, name);
|
|
89
131
|
if (!component.expectedRef && !component.source?.branch && !guardedByProfile) {
|
|
90
132
|
throw new Error(`${name} 必须配置 expectedRef、source.branch 或 target pushRef`);
|
|
91
133
|
}
|
|
@@ -182,11 +224,12 @@ function validateComponent(component, name, guardedByProfile = false) {
|
|
|
182
224
|
for (const file of component.files) { inside("/config", file.from); inside("/config", file.to); }
|
|
183
225
|
}
|
|
184
226
|
if (component.check) {
|
|
185
|
-
const { entrypoint, args, addHosts = [] } = component.check;
|
|
227
|
+
const { entrypoint, args, addHosts = [], network } = component.check;
|
|
186
228
|
if (typeof entrypoint !== "string" || !entrypoint || !Array.isArray(args)
|
|
187
229
|
|| !args.every((arg) => typeof arg === "string") || !Array.isArray(addHosts)
|
|
188
|
-
|| !addHosts.every((host) => typeof host === "string" && host.includes(":"))
|
|
189
|
-
|
|
230
|
+
|| !addHosts.every((host) => typeof host === "string" && host.includes(":"))
|
|
231
|
+
|| (network != null && network !== "none")) {
|
|
232
|
+
throw new Error(`${name} check 必须配置 entrypoint、args 数组及可选 addHosts 或 network: none`);
|
|
190
233
|
}
|
|
191
234
|
}
|
|
192
235
|
if (component.health != null) {
|
|
@@ -201,4 +244,5 @@ function validateComponent(component, name, guardedByProfile = false) {
|
|
|
201
244
|
}
|
|
202
245
|
}
|
|
203
246
|
|
|
204
|
-
module.exports = { compareVersions, normalizePreBuildCommand, resolveSourceDirectories, validateComponent,
|
|
247
|
+
module.exports = { compareVersions, normalizePreBuildCommand, resolveSourceDirectories, validateComponent,
|
|
248
|
+
validateReleaseProfile, validateRepository };
|
|
@@ -1,13 +1,37 @@
|
|
|
1
|
+
const { formatTerminalMessage } = require("../../cli/terminal-style");
|
|
1
2
|
const os = require("node:os");
|
|
2
3
|
const path = require("node:path");
|
|
3
4
|
const { prepareMinimalMonorepoContext } = require("./minimal-context");
|
|
4
5
|
const { prepareProjectInstallContext, prepareStandaloneInstallContext } = require("./project-install");
|
|
5
6
|
const { createReleaseId, createBuildProvenance } = require("./release-identity");
|
|
6
7
|
const { confinedPath, inside } = require("./release-paths");
|
|
8
|
+
const { createBuildTimestampPlugin } = require("../vite");
|
|
9
|
+
|
|
10
|
+
function resolveBuildSecrets(secrets = [], { env, projectDir, io }) {
|
|
11
|
+
return secrets.map(({ id, fileEnv, defaultFile }) => {
|
|
12
|
+
const override = env[fileEnv];
|
|
13
|
+
const source = override || defaultFile;
|
|
14
|
+
if (typeof source !== "string" || !source.trim() || /[,\r\n\0"]/.test(source)) {
|
|
15
|
+
throw new Error(`请通过 ${fileEnv} 提供 ${id} 的构建密钥文件路径`);
|
|
16
|
+
}
|
|
17
|
+
const file = path.resolve(projectDir, source);
|
|
18
|
+
try {
|
|
19
|
+
const stat = io.statSync(file);
|
|
20
|
+
if (!stat.isFile() || stat.size === 0) throw new Error();
|
|
21
|
+
io.accessSync(file, io.constants.R_OK);
|
|
22
|
+
} catch {
|
|
23
|
+
if (!override && defaultFile) {
|
|
24
|
+
throw new Error(`缺少可读的 ${id} 构建配置。请将文件放到部署仓的 ${defaultFile},或通过 ${fileEnv} 指定路径`);
|
|
25
|
+
}
|
|
26
|
+
throw new Error(`无法读取 ${fileEnv} 指定的 ${id} 构建密钥文件`);
|
|
27
|
+
}
|
|
28
|
+
return `type=file,id=${id},src=${file}`;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
7
31
|
|
|
8
32
|
function buildTargetImage(item, {
|
|
9
33
|
target, configuration, projectDir, publicSnapshots, resources, verifyFrozen,
|
|
10
|
-
io, run, capture, log, dependencies, buildExecutableSpawn,
|
|
34
|
+
io, run, capture, log, dependencies, buildExecutableSpawn, secrets = [],
|
|
11
35
|
}) {
|
|
12
36
|
const { sourceDir, commit } = item.frozen;
|
|
13
37
|
let tempRoot;
|
|
@@ -27,7 +51,7 @@ function buildTargetImage(item, {
|
|
|
27
51
|
}
|
|
28
52
|
if (item.source) {
|
|
29
53
|
const sourceLabel = item.name === "backend" ? "后端" : (item.label || item.name);
|
|
30
|
-
log(
|
|
54
|
+
log(formatTerminalMessage("success", `${sourceLabel}构建分支:${item.source.branch}`));
|
|
31
55
|
}
|
|
32
56
|
const buildStrategy = item.buildStrategy || "artifact";
|
|
33
57
|
const componentReleaseContext = item.releaseContext;
|
|
@@ -104,6 +128,10 @@ function buildTargetImage(item, {
|
|
|
104
128
|
...(buildStrategy === "source" ? { BUILD_MODE: componentReleaseContext.mode, PROJECT_DIR: buildProjectDir } : {}),
|
|
105
129
|
...(item.projectInstall && componentReleaseContext.mode === "standalone" ? { REFRESH_INTERNAL: true } : {}),
|
|
106
130
|
};
|
|
131
|
+
if (item.buildTimestampArg) {
|
|
132
|
+
const timestamp = createBuildTimestampPlugin({ timestamp: dependencies.now?.() });
|
|
133
|
+
effectiveBuildArgs[item.buildTimestampArg] = timestamp.transformIndexHtml()[0].attrs.content;
|
|
134
|
+
}
|
|
107
135
|
let provenance;
|
|
108
136
|
const releaseId = item.releaseIdentity ? createReleaseId(commit, dependencies.releaseToken?.()) : commit;
|
|
109
137
|
if (item.releaseIdentity) {
|
|
@@ -146,10 +174,12 @@ function buildTargetImage(item, {
|
|
|
146
174
|
const buildOptions = ["buildx", "build", "--load", "--file", dockerfile];
|
|
147
175
|
if (item.platform) buildOptions.push("--platform", item.platform);
|
|
148
176
|
if (item.target) buildOptions.push("--target", item.target);
|
|
177
|
+
if (item.noCacheFilter) buildOptions.push("--no-cache-filter", item.noCacheFilter.join(","));
|
|
178
|
+
for (const secret of secrets) buildOptions.push("--secret", secret);
|
|
149
179
|
for (const [key, value] of Object.entries(effectiveBuildArgs)) {
|
|
150
180
|
buildOptions.push("--build-arg", `${key}=${value}`);
|
|
151
181
|
}
|
|
152
|
-
|
|
182
|
+
const dockerArgs = [...buildOptions,
|
|
153
183
|
"--label", `org.opencontainers.image.revision=${commit}`, "--label", `org.opencontainers.image.version=${releaseId}`,
|
|
154
184
|
...(sourceAuthor ? ["--label", `com.infly.source.author=${sourceAuthor}`] : []),
|
|
155
185
|
...(sourceMessage ? ["--label", `com.infly.source.message=${sourceMessage}`] : []),
|
|
@@ -157,13 +187,21 @@ function buildTargetImage(item, {
|
|
|
157
187
|
...(workspaceRevision ? ["--label", `com.infly.workspace.revision=${workspaceRevision}`] : []),
|
|
158
188
|
...(configuration ? ["--label", `com.infly.deployment.revision=${configuration.commit}`] : []),
|
|
159
189
|
"--label", `com.infly.platform=${target}`, "--tag", image,
|
|
160
|
-
...(latestImage ? ["--tag", latestImage] : []), context]
|
|
190
|
+
...(latestImage ? ["--tag", latestImage] : []), context];
|
|
191
|
+
try {
|
|
192
|
+
run("docker", dockerArgs, { cwd: projectDir });
|
|
193
|
+
} catch (error) {
|
|
194
|
+
// 子进程异常包含完整 argv;错误摘要不透出密钥文件路径。
|
|
195
|
+
if (secrets.length) throw new Error(`${item.name} Docker 构建失败,请检查上方 Buildx 输出`);
|
|
196
|
+
throw error;
|
|
197
|
+
}
|
|
161
198
|
if (capture("docker", ["image", "inspect", "--format", '{{ index .Config.Labels "org.opencontainers.image.revision" }}', image]) !== commit) {
|
|
162
199
|
throw new Error(`${item.name} 镜像 revision 校验失败`);
|
|
163
200
|
}
|
|
164
201
|
if (item.check) {
|
|
165
202
|
const hosts = (item.check.addHosts || []).flatMap((host) => ["--add-host", host]);
|
|
166
|
-
|
|
203
|
+
const network = item.check.network ? ["--network", item.check.network] : [];
|
|
204
|
+
run("docker", ["run", "--rm", ...network, ...hosts, "--entrypoint", item.check.entrypoint, image, ...item.check.args], { cwd: projectDir });
|
|
167
205
|
}
|
|
168
206
|
return { commit, image, latestImage, releaseId, provenance };
|
|
169
207
|
}
|
|
@@ -186,4 +224,4 @@ function finishTargetImage(item, { commit, image, latestImage, releaseId, proven
|
|
|
186
224
|
return { component: item.name, commit, image, latestImage, ...(channelImage ? { channelImage } : {}), pushed: false, ...identity };
|
|
187
225
|
}
|
|
188
226
|
|
|
189
|
-
module.exports = { buildTargetImage, finishTargetImage };
|
|
227
|
+
module.exports = { buildTargetImage, finishTargetImage, resolveBuildSecrets };
|