1. fix 在线构建仓库文件存在.env文件时带来构建结果异常问题

2. feat 在线构建新增附加环境变量配置 来实现定义构建运行环境变量
This commit is contained in:
bwcx_jzy 2022-12-02 15:08:37 +08:00
parent 18f232b3bd
commit c7ec9848a4
No known key found for this signature in database
GPG Key ID: 5E48E9372088B9E5
6 changed files with 70 additions and 42 deletions

View File

@ -1,5 +1,21 @@
# 🚀 版本日志
## 2.9.19
### 🐣 新增功能
1. 【server】新增 在线构建新增附加环境变量配置 来实现定义构建运行环境变量
### 🐞 解决BUG、优化功能
1. 【server】修复 在线构建仓库文件存在`.env`文件时带来构建结果异常问题(感谢@蹉跎岁月)
### ❌ 不兼容功能
1. 【server】在线构建本地构建、本地发布、docker 镜像取消默认读取 `.env`文件,需要手动配置
------
## 2.9.18 (2022-12-01)
### 🐣 新增功能

View File

@ -199,27 +199,37 @@ public class FileUtils {
/**
* 读取环境变量文件
*
* @param envFile 文件
* @param baseFile 基础文件夹
* @param attachEnv 要读取的文件列表
* @return map
*/
public static Map<String, String> readEnvFile(File envFile) {
public static Map<String, String> readEnvFile(File baseFile, String attachEnv) {
HashMap<String, String> map = MapUtil.newHashMap(10);
if (FileUtil.isFile(envFile)) {
List<String> list = FileUtil.readLines(envFile, CharsetUtil.CHARSET_UTF_8);
List<Tuple> collect = list.stream()
.map(StrUtil::trim)
.filter(s -> !StrUtil.isEmpty(s) && !StrUtil.startWith(s, "#"))
.map(s -> {
List<String> 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<String, String> 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<String> list2 = StrUtil.splitTrim(attachEnv, StrUtil.COMMA);
for (String itemEnv : list2) {
File envFile = FileUtil.file(baseFile, itemEnv);
if (FileUtil.isFile(envFile)) {
List<String> list = FileUtil.readLines(envFile, CharsetUtil.CHARSET_UTF_8);
List<Tuple> collect = list.stream()
.map(StrUtil::trim)
.filter(s -> !StrUtil.isEmpty(s) && !StrUtil.startWith(s, "#"))
.map(s -> {
List<String> 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<String, String> envMap = CollStreamUtil.toMap(collect, objects -> objects.get(0), objects -> objects.get(1));
// java.lang.UnsupportedOperationException
map.putAll(envMap);
}
}
return map;
}

View File

@ -807,8 +807,7 @@ public class BuildExecuteService {
Map<String, String> environment = processBuilder.environment();
environment.putAll(taskData.env);
// env file
File envFile = FileUtil.file(this.gitFile, ".env");
Map<String, String> envFileMap = FileUtils.readEnvFile(envFile);
Map<String, String> envFileMap = FileUtils.readEnvFile(this.gitFile, this.buildExtraModule.getAttachEnv());
environment.putAll(envFileMap);
environment.putAll(buildEnv);
//

View File

@ -129,6 +129,11 @@ public class BuildExtraModule extends BaseModel {
*/
private Boolean dockerTagIncrement;
/**
* 附加环境变量,比如常见的 .env 文件
*/
private String attachEnv;
public String getResultDirFile() {
if (resultDirFile == null) {
return null;

View File

@ -205,8 +205,8 @@ public class ReleaseManage implements Runnable {
*/
private Map<String, String> formatCommand(String[] commands) {
File sourceFile = BuildUtil.getSourceById(this.buildExtraModule.getId());
File envFile = FileUtil.file(sourceFile, ".env");
Map<String, String> envFileMap = FileUtils.readEnvFile(envFile);
// File envFile = FileUtil.file(sourceFile, ".env");
Map<String, String> 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<String> 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<String, String> 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

View File

@ -614,6 +614,22 @@
<a-select-option v-for="item2 in scriptList" :key="item2.id">{{ item2.name }}</a-select-option>
</a-select>
</a-form-model-item>
<a-form-model-item prop="attachEnv">
<template slot="label">
附加环境变量
<a-tooltip v-show="!temp.id">
<template slot="title">
<ul>
<li>附加环境变量是指读取仓库指定环境变量文件来添加到执行构建运行时</li>
<li>比如常见的 .env 文件</li>
<li>文件内容格式要求env_name=xxxxx 不满足格式的行将自动忽略</li>
</ul>
</template>
<a-icon type="question-circle" theme="filled" />
</a-tooltip>
</template>
<a-input v-model="tempExtraData.attachEnv" placeholder="附加环境变量 .env 添加多个使用逗号分隔" />
</a-form-model-item>
</a-collapse-panel>
</a-collapse>
</a-form-model>