diff --git a/CHANGELOG.md b/CHANGELOG.md index 7960cf3a0..d7686640f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,21 @@ # 🚀 版本日志 +## 2.9.19 + +### 🐣 新增功能 + +1. 【server】新增 在线构建新增附加环境变量配置 来实现定义构建运行环境变量 + +### 🐞 解决BUG、优化功能 + +1. 【server】修复 在线构建仓库文件存在`.env`文件时带来构建结果异常问题(感谢@蹉跎岁月) + +### ❌ 不兼容功能 + +1. 【server】在线构建本地构建、本地发布、docker 镜像取消默认读取 `.env`文件,需要手动配置 + +------ + ## 2.9.18 (2022-12-01) ### 🐣 新增功能 diff --git a/modules/common/src/main/java/io/jpom/util/FileUtils.java b/modules/common/src/main/java/io/jpom/util/FileUtils.java index 28f1491f3..c1cf73ece 100644 --- a/modules/common/src/main/java/io/jpom/util/FileUtils.java +++ b/modules/common/src/main/java/io/jpom/util/FileUtils.java @@ -199,27 +199,37 @@ public class FileUtils { /** * 读取环境变量文件 * - * @param envFile 文件 + * @param baseFile 基础文件夹 + * @param attachEnv 要读取的文件列表 * @return map */ - public static Map readEnvFile(File envFile) { + public static Map readEnvFile(File baseFile, String attachEnv) { HashMap map = MapUtil.newHashMap(10); - if (FileUtil.isFile(envFile)) { - List list = FileUtil.readLines(envFile, CharsetUtil.CHARSET_UTF_8); - List collect = list.stream() - .map(StrUtil::trim) - .filter(s -> !StrUtil.isEmpty(s) && !StrUtil.startWith(s, "#")) - .map(s -> { - List list1 = StrUtil.splitTrim(s, "="); - if (CollUtil.size(list1) != 2) { - return null; - } - return new Tuple(list1.get(0), list1.get(1)); - }).filter(Objects::nonNull).collect(Collectors.toList()); - Map envMap = CollStreamUtil.toMap(collect, objects -> objects.get(0), objects -> objects.get(1)); - // java.lang.UnsupportedOperationException - map.putAll(envMap); + if (StrUtil.isEmpty(attachEnv)) { + return map; } + List list2 = StrUtil.splitTrim(attachEnv, StrUtil.COMMA); + for (String itemEnv : list2) { + File envFile = FileUtil.file(baseFile, itemEnv); + + if (FileUtil.isFile(envFile)) { + List list = FileUtil.readLines(envFile, CharsetUtil.CHARSET_UTF_8); + List collect = list.stream() + .map(StrUtil::trim) + .filter(s -> !StrUtil.isEmpty(s) && !StrUtil.startWith(s, "#")) + .map(s -> { + List list1 = StrUtil.splitTrim(s, "="); + if (CollUtil.size(list1) != 2) { + return null; + } + return new Tuple(list1.get(0), list1.get(1)); + }).filter(Objects::nonNull).collect(Collectors.toList()); + Map envMap = CollStreamUtil.toMap(collect, objects -> objects.get(0), objects -> objects.get(1)); + // java.lang.UnsupportedOperationException + map.putAll(envMap); + } + } + return map; } diff --git a/modules/server/src/main/java/io/jpom/build/BuildExecuteService.java b/modules/server/src/main/java/io/jpom/build/BuildExecuteService.java index 26d058375..d8a105b48 100644 --- a/modules/server/src/main/java/io/jpom/build/BuildExecuteService.java +++ b/modules/server/src/main/java/io/jpom/build/BuildExecuteService.java @@ -807,8 +807,7 @@ public class BuildExecuteService { Map environment = processBuilder.environment(); environment.putAll(taskData.env); // env file - File envFile = FileUtil.file(this.gitFile, ".env"); - Map envFileMap = FileUtils.readEnvFile(envFile); + Map envFileMap = FileUtils.readEnvFile(this.gitFile, this.buildExtraModule.getAttachEnv()); environment.putAll(envFileMap); environment.putAll(buildEnv); // diff --git a/modules/server/src/main/java/io/jpom/build/BuildExtraModule.java b/modules/server/src/main/java/io/jpom/build/BuildExtraModule.java index 281ed1fb1..229a3a100 100644 --- a/modules/server/src/main/java/io/jpom/build/BuildExtraModule.java +++ b/modules/server/src/main/java/io/jpom/build/BuildExtraModule.java @@ -129,6 +129,11 @@ public class BuildExtraModule extends BaseModel { */ private Boolean dockerTagIncrement; + /** + * 附加环境变量,比如常见的 .env 文件 + */ + private String attachEnv; + public String getResultDirFile() { if (resultDirFile == null) { return null; diff --git a/modules/server/src/main/java/io/jpom/build/ReleaseManage.java b/modules/server/src/main/java/io/jpom/build/ReleaseManage.java index 4f974483d..428ae808a 100644 --- a/modules/server/src/main/java/io/jpom/build/ReleaseManage.java +++ b/modules/server/src/main/java/io/jpom/build/ReleaseManage.java @@ -205,8 +205,8 @@ public class ReleaseManage implements Runnable { */ private Map formatCommand(String[] commands) { File sourceFile = BuildUtil.getSourceById(this.buildExtraModule.getId()); - File envFile = FileUtil.file(sourceFile, ".env"); - Map envFileMap = FileUtils.readEnvFile(envFile); + // File envFile = FileUtil.file(sourceFile, ".env"); + Map envFileMap = FileUtils.readEnvFile(sourceFile, this.buildExtraModule.getAttachEnv()); // envFileMap.putAll(buildEnv); // @@ -219,25 +219,6 @@ public class ReleaseManage implements Runnable { return envFileMap; } - private String parseDockerTag(File envFile, String tag) { - if (!FileUtil.isFile(envFile)) { - return tag; - } - final String[] newTag = {tag}; - FileUtil.readLines(envFile, StandardCharsets.UTF_8, (LineHandler) line -> { - line = StrUtil.trim(line); - if (StrUtil.startWith(line, "#")) { - return; - } - List list = StrUtil.splitTrim(line, "="); - if (CollUtil.size(list) != 2) { - return; - } - newTag[0] = StrUtil.replace(newTag[0], "${" + list.get(0) + "}", list.get(1)); - }); - return newTag[0]; - } - /** * 版本号递增 * @@ -289,9 +270,10 @@ public class ReleaseManage implements Runnable { File historyPackageFile = BuildUtil.getHistoryPackageFile(buildExtraModule.getId(), this.buildNumberId, StrUtil.SLASH); FileUtil.copyContent(historyPackageFile, tempPath, true); // env file - File envFile = FileUtil.file(tempPath, ".env"); + Map envMap = FileUtils.readEnvFile(sourceFile, this.buildExtraModule.getAttachEnv()); + //File envFile = FileUtil.file(tempPath, ".env"); String dockerTag = this.buildExtraModule.getDockerTag(); - dockerTag = this.parseDockerTag(envFile, dockerTag); + dockerTag = StringUtil.formatStrByMap(dockerTag, envMap); // dockerTag = this.dockerTagIncrement(this.buildExtraModule.getDockerTagIncrement(), dockerTag); // docker file diff --git a/web-vue/src/pages/build/list-info.vue b/web-vue/src/pages/build/list-info.vue index c62f237b4..12feb60a7 100644 --- a/web-vue/src/pages/build/list-info.vue +++ b/web-vue/src/pages/build/list-info.vue @@ -614,6 +614,22 @@ {{ item2.name }} + + + +