!195 新增git submodules支持

Merge pull request !195 from Croce/dev
This commit is contained in:
蒋小小 2023-08-29 07:52:27 +00:00 committed by Gitee
commit 1a2f032cf6
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 41 additions and 19 deletions

View File

@ -136,6 +136,7 @@ public class ReleaseManage {
this.init(); this.init();
this.resultFile = buildExtraModule.resultDirFile(this.buildNumberId); this.resultFile = buildExtraModule.resultDirFile(this.buildNumberId);
this.buildEnv.put("BUILD_RESULT_FILE", FileUtil.getAbsolutePath(this.resultFile)); this.buildEnv.put("BUILD_RESULT_FILE", FileUtil.getAbsolutePath(this.resultFile));
this.buildEnv.put("BUILD_RESULT_DIR_FILE", buildExtraModule.getResultDirFile());
// //
this.updateStatus(BuildStatus.PubIng, "开始发布中"); this.updateStatus(BuildStatus.PubIng, "开始发布中");
if (FileUtil.isEmpty(this.resultFile)) { if (FileUtil.isEmpty(this.resultFile)) {
@ -144,7 +145,7 @@ public class ReleaseManage {
return info; return info;
} }
long resultFileSize = FileUtil.size(this.resultFile); long resultFileSize = FileUtil.size(this.resultFile);
logRecorder.system("开始执行发布,需要发布的文件大小:{}", FileUtil.readableFileSize(resultFileSize)); logRecorder.system("开始执行发布需要发布的文件大小:{}", FileUtil.readableFileSize(resultFileSize));
Optional.ofNullable(consumer).ifPresent(consumer1 -> consumer1.accept(resultFileSize)); Optional.ofNullable(consumer).ifPresent(consumer1 -> consumer1.accept(resultFileSize));
// 先同步到文件管理中心 // 先同步到文件管理中心
Boolean syncFileStorage = this.buildExtraModule.getSyncFileStorage(); Boolean syncFileStorage = this.buildExtraModule.getSyncFileStorage();
@ -157,9 +158,9 @@ public class ReleaseManage {
// 默认的别名码为构建id // 默认的别名码为构建id
StrUtil.emptyToDefault(buildInfoModel.getAliasCode(), buildInfoModel.getId())); StrUtil.emptyToDefault(buildInfoModel.getAliasCode(), buildInfoModel.getId()));
if (successMd5 != null) { if (successMd5 != null) {
logRecorder.system("构建产物文件成功同步到文件管理中心,{}", successMd5); logRecorder.system("构建产物文件成功同步到文件管理中心{}", successMd5);
} else { } else {
logRecorder.systemWarning("构建产物文件同步到文件管理中心失败,当前文件已经存文件管理中心存在啦"); logRecorder.systemWarning("构建产物文件同步到文件管理中心失败当前文件已经存文件管理中心存在啦");
} }
} }
// //
@ -180,7 +181,7 @@ public class ReleaseManage {
} else if (releaseMethod == BuildReleaseMethod.No.getCode()) { } else if (releaseMethod == BuildReleaseMethod.No.getCode()) {
return null; return null;
} else { } else {
String format = StrUtil.format("没有实现的发布分发:{}", releaseMethod); String format = StrUtil.format("没有实现的发布分发{}", releaseMethod);
logRecorder.systemError(format); logRecorder.systemError(format);
return format; return format;
} }
@ -275,7 +276,7 @@ public class ReleaseManage {
if (pushToRepository != null && pushToRepository) { if (pushToRepository != null && pushToRepository) {
List<String> repositoryList = StrUtil.splitTrim(dockerTag, StrUtil.COMMA); List<String> repositoryList = StrUtil.splitTrim(dockerTag, StrUtil.COMMA);
for (String repositoryItem : repositoryList) { for (String repositoryItem : repositoryList) {
logRecorder.system("start push to repository in({}),{} {}", map.get("name"), StrUtil.emptyToDefault((String) map.get("registryUrl"), StrUtil.EMPTY), repositoryItem); logRecorder.system("start push to repository in({}),{} {}{}", map.get("name"), StrUtil.emptyToDefault((String) map.get("registryUrl"), StrUtil.EMPTY), repositoryItem, System.lineSeparator());
// //
map.put("repository", repositoryItem); map.put("repository", repositoryItem);
Consumer<String> logConsumer = s -> logRecorder.info(s); Consumer<String> logConsumer = s -> logRecorder.info(s);
@ -316,7 +317,7 @@ public class ReleaseManage {
} }
private void doDockerImage(DockerInfoModel dockerInfoModel, Map<String, String> envMap, File dockerfile, File baseDir, String dockerTag, BuildExtraModule extraModule) { private void doDockerImage(DockerInfoModel dockerInfoModel, Map<String, String> envMap, File dockerfile, File baseDir, String dockerTag, BuildExtraModule extraModule) {
logRecorder.system("{} start build image {}", dockerInfoModel.getName(), dockerTag); logRecorder.system("{} start build image {}{}", dockerInfoModel.getName(), dockerTag, System.lineSeparator());
Map<String, Object> map = machineDockerServer.dockerParameter(dockerInfoModel); Map<String, Object> map = machineDockerServer.dockerParameter(dockerInfoModel);
//.toParameter(); //.toParameter();
map.put("Dockerfile", dockerfile); map.put("Dockerfile", dockerfile);
@ -348,7 +349,7 @@ public class ReleaseManage {
logRecorder.systemError("没有需要执行的命令"); logRecorder.systemError("没有需要执行的命令");
return null; return null;
} }
logRecorder.system("{} start exec", DateUtil.now()); logRecorder.system("{} start exec{}", DateUtil.now(), System.lineSeparator());
File sourceFile = BuildUtil.getSourceById(this.buildExtraModule.getId()); File sourceFile = BuildUtil.getSourceById(this.buildExtraModule.getId());
Map<String, String> envFileMap = buildEnv.environment(); Map<String, String> envFileMap = buildEnv.environment();
@ -409,7 +410,7 @@ public class ReleaseManage {
if (StrUtil.isEmpty(releasePath)) { if (StrUtil.isEmpty(releasePath)) {
logRecorder.systemWarning("发布目录为空"); logRecorder.systemWarning("发布目录为空");
} else { } else {
logRecorder.system("{} {} start ftp upload", DateUtil.now(), item.getName()); logRecorder.system("{} {} start ftp upload{}", DateUtil.now(), item.getName(), System.lineSeparator());
MySftp.ProgressMonitor sftpProgressMonitor = sshService.createProgressMonitor(logRecorder); MySftp.ProgressMonitor sftpProgressMonitor = sshService.createProgressMonitor(logRecorder);
MySftp sftp = new MySftp(session, charset, timeout, sftpProgressMonitor); MySftp sftp = new MySftp(session, charset, timeout, sftpProgressMonitor);
channelSftp = sftp.getClient(); channelSftp = sftp.getClient();
@ -482,9 +483,9 @@ public class ReleaseManage {
int delSize = CollUtil.size(del); int delSize = CollUtil.size(del);
int diffSize = CollUtil.size(diff); int diffSize = CollUtil.size(diff);
if (clearOld) { if (clearOld) {
logRecorder.system("对比文件结果,产物文件 {} 个、需要上传 {} 个、需要删除 {} 个", CollUtil.size(collect), CollUtil.size(diff), delSize); logRecorder.system("对比文件结果产物文件 {} 个、需要上传 {} 个、需要删除 {} 个", CollUtil.size(collect), CollUtil.size(diff), delSize);
} else { } else {
logRecorder.system("对比文件结果,产物文件 {} 个、需要上传 {} 个", CollUtil.size(collect), CollUtil.size(diff)); logRecorder.system("对比文件结果产物文件 {} 个、需要上传 {} 个", CollUtil.size(collect), CollUtil.size(diff));
} }
// 清空发布才先执行删除 // 清空发布才先执行删除
if (delSize > 0 && clearOld) { if (delSize > 0 && clearOld) {
@ -510,7 +511,7 @@ public class ReleaseManage {
int progressRange = (int) Math.floor(progressPercentage / buildExtConfig.getLogReduceProgressRatio()); int progressRange = (int) Math.floor(progressPercentage / buildExtConfig.getLogReduceProgressRatio());
if (progressRangeList.add(progressRange)) { if (progressRangeList.add(progressRange)) {
// total, progressSize // total, progressSize
logRecorder.system("上传文件进度:{}[{}/{}] {}/{} {} ", file.getName(), logRecorder.system("上传文件进度{}[{}/{}] {}/{} {} ", file.getName(),
(finalI + 1), diffSize, (finalI + 1), diffSize,
FileUtil.readableFileSize(progressSize), FileUtil.readableFileSize(total), FileUtil.readableFileSize(progressSize), FileUtil.readableFileSize(total),
NumberUtil.formatPercent(((float) progressSize / total), 0) NumberUtil.formatPercent(((float) progressSize / total), 0)
@ -558,7 +559,7 @@ public class ReleaseManage {
double progressPercentage = Math.floor(((float) progressSize / total) * 100); double progressPercentage = Math.floor(((float) progressSize / total) * 100);
int progressRange = (int) Math.floor(progressPercentage / buildExtConfig.getLogReduceProgressRatio()); int progressRange = (int) Math.floor(progressPercentage / buildExtConfig.getLogReduceProgressRatio());
if (progressRangeList.add(progressRange)) { if (progressRangeList.add(progressRange)) {
logRecorder.system("上传文件进度:{} {}/{} {}", name, logRecorder.system("上传文件进度{} {}/{} {}", name,
FileUtil.readableFileSize(progressSize), FileUtil.readableFileSize(total), FileUtil.readableFileSize(progressSize), FileUtil.readableFileSize(total),
NumberUtil.formatPercent(((float) progressSize / total), 0)); NumberUtil.formatPercent(((float) progressSize / total), 0));
} }
@ -593,7 +594,7 @@ public class ReleaseManage {
return outGivingRunBuilder.build().startRun(selectProject); return outGivingRunBuilder.build().startRun(selectProject);
}); });
//OutGivingRun.startRun(releaseMethodDataId, zipFile, userModel, unZip, 0); //OutGivingRun.startRun(releaseMethodDataId, zipFile, userModel, unZip, 0);
logRecorder.system("开始执行分发包啦,请到分发中查看详情状态"); logRecorder.system("开始执行分发包啦请到分发中查看详情状态");
OutGivingModel.Status status = statusFuture.get(); OutGivingModel.Status status = statusFuture.get();
logRecorder.system("分发结果:{}", status.getDesc()); logRecorder.system("分发结果:{}", status.getDesc());
} }
@ -607,7 +608,7 @@ public class ReleaseManage {
try { try {
BaseServerController.resetInfo(userModel); BaseServerController.resetInfo(userModel);
this.init(); this.init();
logRecorder.system("开始回滚:{}", DateTime.now()); logRecorder.system("开始回滚{}", DateTime.now());
// //
String errorMsg = this.start(null, item); String errorMsg = this.start(null, item);
logRecorder.system("执行回滚结束:{}", StrUtil.emptyToDefault(errorMsg, "ok")); logRecorder.system("执行回滚结束:{}", StrUtil.emptyToDefault(errorMsg, "ok"));

View File

@ -44,6 +44,8 @@ import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.SubmoduleConfig.FetchRecurseSubmodulesMode;
import org.eclipse.jgit.merge.MergeStrategy;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.*; import org.eclipse.jgit.transport.*;
@ -133,7 +135,7 @@ public class JGitUtil {
*/ */
private static Git reClone(Map<String, Object> parameter, String branchName, String tagName, File file, PrintWriter printWriter) throws GitAPIException, IOException { private static Git reClone(Map<String, Object> parameter, String branchName, String tagName, File file, PrintWriter printWriter) throws GitAPIException, IOException {
println(printWriter, StrUtil.EMPTY); println(printWriter, StrUtil.EMPTY);
println(printWriter, "Automatically re-clones repositories"); println(printWriter, "JGit: Automatically re-clones repositories");
if (!FileUtil.clean(file)) { if (!FileUtil.clean(file)) {
FileUtil.del(file.toPath()); FileUtil.del(file.toPath());
} }
@ -151,7 +153,8 @@ public class JGitUtil {
} }
String url = (String) parameter.get("url"); String url = (String) parameter.get("url");
CloneCommand command = cloneCommand.setURI(url) CloneCommand command = cloneCommand.setURI(url)
.setDirectory(file); .setDirectory(file)
.setCloneSubmodules(true);
// 设置凭证 // 设置凭证
setCredentials(command, parameter); setCredentials(command, parameter);
return command.call(); return command.call();
@ -377,7 +380,8 @@ public class JGitUtil {
SmallTextProgressMonitor progressMonitor = new SmallTextProgressMonitor(printWriter, progressRatio); SmallTextProgressMonitor progressMonitor = new SmallTextProgressMonitor(printWriter, progressRatio);
// 放弃本地修改 // 放弃本地修改
git.checkout().setName(branchName).setForced(true).setProgressMonitor(progressMonitor).call(); git.checkout().setName(branchName).setForced(true).setProgressMonitor(progressMonitor).call();
//
println(printWriter, "\ngit pull");
PullCommand pull = git.pull(); PullCommand pull = git.pull();
// //
setCredentials(pull, parameter); setCredentials(pull, parameter);
@ -385,6 +389,7 @@ public class JGitUtil {
PullResult call = pull PullResult call = pull
.setRemoteBranchName(branchName) .setRemoteBranchName(branchName)
.setProgressMonitor(progressMonitor) .setProgressMonitor(progressMonitor)
.setRecurseSubmodules(FetchRecurseSubmodulesMode.YES)
.call(); .call();
// 输出拉取结果 // 输出拉取结果
if (call != null) { if (call != null) {
@ -405,6 +410,16 @@ public class JGitUtil {
// println(printWriter, "fetchResult {}", fetchResult); // println(printWriter, "fetchResult {}", fetchResult);
// } // }
} }
//
println(printWriter, "\ngit submodule update --init --remote --recursive");
SubmoduleUpdateCommand subUpdate = git.submoduleUpdate();
setCredentials(subUpdate, parameter);
Collection<String> rst = subUpdate
.setProgressMonitor(progressMonitor)
.setFetch(true)
.setStrategy(MergeStrategy.THEIRS)
.call();
println(printWriter, rst.stream().collect(Collectors.joining("\n")));
return call; return call;
} }

View File

@ -160,10 +160,16 @@ public class SystemGitProcess extends AbstractGitProcess {
File saveFile = getSaveFile(); File saveFile = getSaveFile();
{ {
// 更新 // 更新
printWriter.println("git pull");
CommandUtil.exec(saveFile, null, line -> { CommandUtil.exec(saveFile, null, line -> {
printWriter.println(line); printWriter.println(line);
printWriter.flush(); printWriter.flush();
}, "git", "pull"); }, "git", "pull");
printWriter.println("git submodule update --init --remote -f --recursive");
CommandUtil.exec(saveFile, null, line -> {
printWriter.println(line);
printWriter.flush();
}, "git", "submodule", "update", "--init", "--remote", "-f", "--recursive");
} }
// 获取提交日志 // 获取提交日志
String[] command = {"git", "log", "-1", branchOrTag}; String[] command = {"git", "log", "-1", branchOrTag};
@ -181,7 +187,7 @@ public class SystemGitProcess extends AbstractGitProcess {
} }
private void reClone(PrintWriter printWriter, String branchOrTag) throws IOException { private void reClone(PrintWriter printWriter, String branchOrTag) throws IOException {
printWriter.println("Automatically re-clones repositories"); printWriter.println("SystemGit: Automatically re-clones repositories");
// 先删除本地目录 // 先删除本地目录
File savePath = getSaveFile(); File savePath = getSaveFile();
if (!FileUtil.clean(savePath)) { if (!FileUtil.clean(savePath)) {
@ -205,7 +211,7 @@ public class SystemGitProcess extends AbstractGitProcess {
return null; return null;
}).ifPresent(integer -> env.put("GIT_HTTP_TIMEOUT", String.valueOf(integer))); }).ifPresent(integer -> env.put("GIT_HTTP_TIMEOUT", String.valueOf(integer)));
// //
String[] command = new String[]{"git", "clone", depthStr, "-b", branchOrTag, this.getCovertUrl(), savePath.getAbsolutePath()}; String[] command = new String[]{"git", "clone", "--recursive", depthStr, "-b", branchOrTag, this.getCovertUrl(), savePath.getAbsolutePath()};
FileUtil.mkdir(savePath); FileUtil.mkdir(savePath);
CommandUtil.exec(savePath, env, line -> { CommandUtil.exec(savePath, env, line -> {
printWriter.println(line); printWriter.println(line);