fix 构建支持单个配置保留天数和保留个数 #I7FOG2

This commit is contained in:
bwcx_jzy 2023-06-26 17:02:23 +08:00
parent 2066aca1e0
commit e05c67a497
No known key found for this signature in database
GPG Key ID: E187D6E9DDDE8C53
10 changed files with 149 additions and 22 deletions

View File

@ -15,6 +15,8 @@
5. 【server】优化 工作空间配置页面中新增节点分发白名单配置入口(感谢 [@陈旭](https://gitee.com/chenxu8989) [Gitee issues I7F0W0](https://gitee.com/dromara/Jpom/issues/I7F0W0)
6. 【server】优化 构建附加环境变量支持解析 URL 参数格式
(感谢 [@爱琳琳真是太好了](https://gitee.com/qiqi513_admin) [Gitee issues I7FROG](https://gitee.com/dromara/Jpom/issues/I7FROG)
7. 【server】优化 构建支持单个配置保留天数和保留个数
(感谢 [@阿超](https://gitee.com/VampireAchao) [Gitee issues I7FOG2](https://gitee.com/dromara/Jpom/issues/I7FOG2)
------

View File

@ -191,6 +191,11 @@ public class BuildExtraModule extends BaseModel {
*/
private Integer cloneDepth;
/**
* 构建历史保留个数
*/
private Integer resultKeepCount;
public boolean strictlyEnforce() {
return strictlyEnforce != null && strictlyEnforce;
}

View File

@ -198,6 +198,7 @@ public class BuildInfoController extends BaseServerController {
String extraData, String group,
@ValidatorItem(value = ValidatorRule.POSITIVE_INTEGER, msg = "构建方式不正确") int buildMode,
String aliasCode,
@ValidatorItem(value = ValidatorRule.NUMBERS, msg = "请填写正确的保留天数") Integer resultKeepDay,
HttpServletRequest request) {
// 根据 repositoryId 查询仓库信息
RepositoryModel repositoryModel = repositoryService.getByKey(repositoryId, request);
@ -246,6 +247,7 @@ public class BuildInfoController extends BaseServerController {
buildInfoModel.setResultDirFile(resultDirFile);
buildInfoModel.setScript(script);
buildInfoModel.setGroup(group);
buildInfoModel.setResultKeepDay(resultKeepDay);
buildInfoModel.setBuildMode(buildMode);
// 发布方式
BuildReleaseMethod releaseMethod1 = BaseEnum.getEnum(BuildReleaseMethod.class, releaseMethod);

View File

@ -23,6 +23,7 @@
package org.dromara.jpom.model.data;
import cn.hutool.core.annotation.PropIgnore;
import cn.hutool.core.util.ObjectUtil;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -140,6 +141,14 @@ public class BuildInfoModel extends BaseGroupModel {
* 别名码
*/
private String aliasCode;
/**
* 产物保留天数
*/
private Integer resultKeepDay;
public Integer getResultKeepDay() {
return ObjectUtil.defaultIfNull(this.resultKeepDay, 0);
}
@Tolerate
public BuildInfoModel() {

View File

@ -109,6 +109,8 @@ public class BuildHistoryLog extends BaseWorkspaceModel {
private String buildRemark;
/**
* 构建其他信息
*
* @see BuildExtraModule
*/
private String extraData;
/**

View File

@ -156,6 +156,13 @@ public class BuildInfoService extends BaseGroupService<BuildInfoModel> implement
return getTableName();
}
public List<BuildInfoModel> hasResultKeep() {
//
String sql = "select * from " + super.getTableName() + " where resultKeepDay>0";
return super.queryList(sql);
}
private static class CronTask implements Task {
private final String buildId;

View File

@ -23,11 +23,22 @@
package org.dromara.jpom.service.dblog;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.Page;
import cn.hutool.db.sql.Direction;
import cn.hutool.db.sql.Order;
import cn.keepbx.jpom.event.ISystemTask;
import lombok.extern.slf4j.Slf4j;
import org.dromara.jpom.build.BuildExtraModule;
import org.dromara.jpom.build.BuildUtil;
import org.dromara.jpom.common.JsonMessage;
import org.dromara.jpom.model.BaseDbModel;
import org.dromara.jpom.model.PageResultDto;
import org.dromara.jpom.model.data.BuildInfoModel;
import org.dromara.jpom.model.enums.BuildStatus;
import org.dromara.jpom.model.log.BuildHistoryLog;
@ -36,7 +47,10 @@ import org.dromara.jpom.system.extconf.BuildExtConfig;
import org.springframework.stereotype.Service;
import java.io.File;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* 构建历史db
@ -46,7 +60,7 @@ import java.util.Optional;
*/
@Service
@Slf4j
public class DbBuildHistoryLogService extends BaseWorkspaceService<BuildHistoryLog> {
public class DbBuildHistoryLogService extends BaseWorkspaceService<BuildHistoryLog> implements ISystemTask {
private final BuildInfoService buildService;
private final BuildExtConfig buildExtConfig;
@ -99,7 +113,7 @@ public class DbBuildHistoryLogService extends BaseWorkspaceService<BuildHistoryL
}
}
int count = this.delByKey(buildHistoryLog.getId());
return new JsonMessage<>(200, "删除成功", count + "");
return new JsonMessage<>(200, "删除成功", String.valueOf(count));
}
@Override
@ -116,28 +130,45 @@ public class DbBuildHistoryLogService extends BaseWorkspaceService<BuildHistoryL
public int insert(BuildHistoryLog buildHistoryLog) {
int count = super.insert(buildHistoryLog);
// 清理单个
BuildExtraModule build = BuildExtraModule.build(buildHistoryLog);
int resultKeepCount = ObjectUtil.defaultIfNull(build.getResultKeepCount(), 0);
int buildItemMaxHistoryCount = buildExtConfig.getItemMaxHistoryCount();
super.autoLoopClear("startTime", buildItemMaxHistoryCount,
entity -> {
entity.set("buildDataId", buildHistoryLog.getBuildDataId());
// 清理单项构建历史保留个数只判断构建结束发布中发布失败发布失败有效构建状态避免无法保留有效构建历史
entity.set("status", CollUtil.newArrayList(
BuildStatus.Success.getCode(),
BuildStatus.PubIng.getCode(),
BuildStatus.PubSuccess.getCode(),
BuildStatus.PubError.getCode()));
},
buildHistoryLog1 -> {
JsonMessage<String> jsonMessage = this.deleteLogAndFile(buildHistoryLog1);
if (!jsonMessage.success()) {
log.warn("{} {} {}", buildHistoryLog1.getBuildName(), buildHistoryLog1.getBuildNumberId(), jsonMessage);
return false;
}
return true;
});
if (resultKeepCount > 0 || buildItemMaxHistoryCount > 0) {
// 至少有一个配置
int useCount;
if (resultKeepCount > 0 && buildItemMaxHistoryCount > 0) {
// 都配置过使用最小值
useCount = Math.min(resultKeepCount, buildItemMaxHistoryCount);
} else {
// 只配置了一处使用最大值
useCount = Math.max(resultKeepCount, buildItemMaxHistoryCount);
}
super.autoLoopClear("startTime", useCount, entity -> this.fillClearWhere(entity, buildHistoryLog.getBuildDataId()), this.predicate());
}
return count;
}
private Predicate<BuildHistoryLog> predicate() {
return buildHistoryLog1 -> {
JsonMessage<String> jsonMessage = this.deleteLogAndFile(buildHistoryLog1);
if (!jsonMessage.success()) {
log.warn("{} {} {}", buildHistoryLog1.getBuildName(), buildHistoryLog1.getBuildNumberId(), jsonMessage);
return false;
}
return true;
};
}
private void fillClearWhere(Entity entity, String buildDataId) {
entity.set("buildDataId", buildDataId);
// 清理单项构建历史保留个数只判断构建结束发布中发布失败发布失败有效构建状态避免无法保留有效构建历史
entity.set("status", CollUtil.newArrayList(
BuildStatus.Success.getCode(),
BuildStatus.PubIng.getCode(),
BuildStatus.PubSuccess.getCode(),
BuildStatus.PubError.getCode()));
}
@Override
protected void executeClearImpl(int count) {
// 清理总数据
@ -163,4 +194,40 @@ public class DbBuildHistoryLogService extends BaseWorkspaceService<BuildHistoryL
protected String[] clearTimeColumns() {
return super.clearTimeColumns();
}
@Override
public void executeTask() {
List<BuildInfoModel> buildInfoModels = buildService.hasResultKeep();
if (CollUtil.isEmpty(buildInfoModels)) {
return;
}
for (BuildInfoModel buildInfoModel : buildInfoModels) {
Integer resultKeepDay = buildInfoModel.getResultKeepDay();
if (resultKeepDay == null || resultKeepDay <= 0) {
continue;
}
log.debug("自动删除过期的构建历史相关文件:{} {}", buildInfoModel.getName(), resultKeepDay);
Entity entity = Entity.create();
this.fillClearWhere(entity, buildInfoModel.getId());
DateTime date = DateTime.now();
date = DateUtil.offsetDay(date, -resultKeepDay);
date = DateUtil.beginOfDay(date);
entity.set("startTime", "< " + date.getTime());
while (true) {
Page page = new Page(1, 50);
page.addOrder(new Order("startTime", Direction.DESC));
PageResultDto<BuildHistoryLog> pageResult = this.listPage(entity, page);
if (pageResult.isEmpty()) {
break;
}
List<String> ids = pageResult.getResult()
.stream()
.filter(this.predicate())
.map(BaseDbModel::getId)
.collect(Collectors.toList());
//
this.delByKey(ids, null);
}
}
}
}

View File

@ -60,3 +60,4 @@ ADD,WORKSPACE,group,String,50,,分组,false
ADD,MACHINE_SSH_INFO,dockerInfo,String,255,,服务器中的 docker 信息,false
ADD,MACHINE_DOCKER_INFO,enableSsh,tinyint,0,,是否开启SSH连接,false
ADD,MACHINE_DOCKER_INFO,machineSshId,String,255,,SSH连接信息,false
ADD,BUILD_INFO,resultKeepDay,Integer,,,产物保留天数,false

1 alterType,tableName,name,type,len,defaultValue,comment,notNull
60 ADD,MACHINE_SSH_INFO,dockerInfo,String,255,,服务器中的 docker 信息,false
61 ADD,MACHINE_DOCKER_INFO,enableSsh,tinyint,0,,是否开启SSH连接,false
62 ADD,MACHINE_DOCKER_INFO,machineSshId,String,255,,SSH连接信息,false
63 ADD,BUILD_INFO,resultKeepDay,Integer,,,产物保留天数,false

View File

@ -93,6 +93,7 @@ export function editBuild(params) {
autoBuildCron: params.autoBuildCron,
buildMode: params.buildMode,
aliasCode: params.aliasCode,
resultKeepDay: params.resultKeepDay,
};
return axios({
url: "/build/edit",

View File

@ -681,7 +681,7 @@
<a-col :span="4">
<a-switch v-model="tempExtraData.syncFileStorage" checked-children="同步" un-checked-children="不同步" />
</a-col>
<a-col :span="7" style="text-align: right">
<a-col :span="6" style="text-align: right">
<a-space>
<a-tooltip>
发布隐藏文件
@ -692,6 +692,33 @@
<a-switch v-model="tempExtraData.releaseHideFile" checked-children="" un-checked-children="" />
</a-space>
</a-col>
<a-col :span="7" style="text-align: right">
<a-space>
<a-tooltip>
保留天数
<template slot="title">
构建产物保留天数小于等于 0 为跟随全局保留配置注意自动清理仅会清理记录状态为构建结束发布中发布失败发布失败的数据避免一些异常构建影响保留个数
</template>
<a-icon v-if="!temp.id" type="question-circle" theme="filled" />
</a-tooltip>
<a-input-number v-model="temp.resultKeepDay" :min="0" />
</a-space>
</a-col>
<a-col :span="7" style="text-align: right">
<a-space>
<a-tooltip>
保留个数
<template slot="title">
构建产物保留个数小于等于 0 为跟随全局保留配置如果数值大于 0
将和全局配置对比最小值来参考注意自动清理仅会清理记录状态为构建结束发布中发布失败发布失败的数据避免一些异常构建影响保留个数 将在创建新的构建记录时候检查保留个数
</template>
<a-icon v-if="!temp.id" type="question-circle" theme="filled" />
</a-tooltip>
<a-input-number v-model="tempExtraData.resultKeepCount" :min="0" />
</a-space>
</a-col>
</a-row>
</a-form-model-item>
<a-form-model-item label="别名码" prop="aliasCode" help="如果产物同步到文件中心,当前值会共享">
@ -1070,7 +1097,7 @@ export default {
},
//
handleAdd() {
this.temp = {};
this.temp = { resultKeepDay: 0 };
this.branchList = [];
// this.tempRepository = {};
// this.loadRepositoryList();
@ -1083,6 +1110,7 @@ export default {
this.tempExtraData = {
cacheBuild: true,
saveBuildFile: true,
resultKeepCount: 0,
};
this.$refs["editBuildForm"]?.resetFields();
},
@ -1102,6 +1130,9 @@ export default {
if (this.tempExtraData.saveBuildFile === undefined) {
this.tempExtraData.saveBuildFile = true;
}
if (this.tempExtraData.resultKeepCount === undefined) {
this.tempExtraData.resultKeepCount = 0;
}
//
if (this.tempExtraData.releaseMethodDataId) {