This commit is contained in:
jiangzhongxiang 2019-09-27 08:56:11 +08:00
commit ac0b99e8e5
2 changed files with 92 additions and 89 deletions

View File

@ -147,6 +147,7 @@ public class GameController {
String evaluateStartTime = requestTime.toString(); String evaluateStartTime = requestTime.toString();
JSONObject cost = new JSONObject(); JSONObject cost = new JSONObject();
cost.put("evaluateStartTime", evaluateStartTime); cost.put("evaluateStartTime", evaluateStartTime);
JedisUtil.set("timeCost:" + tpiID + ":" + buildID, cost.toJSONString());
Boolean reuseFlag = reusePod == null ? Boolean.FALSE : reusePod; Boolean reuseFlag = reusePod == null ? Boolean.FALSE : reusePod;
needPortMapping = needPortMapping == null ? 0 : needPortMapping; needPortMapping = needPortMapping == null ? 0 : needPortMapping;

View File

@ -156,96 +156,98 @@ public class GameService {
* @param tpiGitURL * @param tpiGitURL
* @throws GameException * @throws GameException
*/ */
public void gitPull(String path, String tpiGitURL, Integer contentModified) throws Exception { public void gitPull(String path, String tpiGitURL, Integer contentModified) throws Exception {
logger.debug("git pull, path: {}, tpiGitURL: {}", path, tpiGitURL); logger.debug("git pull, path: {}, tpiGitURL: {}", path, tpiGitURL);
String tpiRepoName = GameHelper.getRepoName(tpiGitURL); String tpiRepoName = GameHelper.getRepoName(tpiGitURL);
String tpiRepoPath = path + File.separator + tpiRepoName; String tpiRepoPath = path + File.separator + tpiRepoName;
String tpiID = path.substring(path.lastIndexOf("_") + 1); String tpiID = path.substring(path.lastIndexOf("_") + 1);
// pull 操作之前如果tpi本地版本库不存在则克隆主机名为origin // pull 操作之前如果tpi本地版本库不存在则克隆主机名为origin
File file = FileUtils.getFile(tpiRepoPath); File file = FileUtils.getFile(tpiRepoPath);
if (!file.exists()) { if (!file.exists()) {
logger.warn("版本库不存在,先克隆! tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID); logger.warn("版本库不存在,先克隆! tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
gitClone(path, tpiGitURL, "origin", tpiRepoName, "--depth=1"); gitClone(path, tpiGitURL, "origin", tpiRepoName, "--depth=1");
} }
// pull之前先判断版本库的origin主机是否存在若不存在则先创建 // pull之前先判断版本库的origin主机是否存在若不存在则先创建
String cmd = "cd " + tpiRepoPath + " && git remote | grep ^origin$"; String cmd = "cd " + tpiRepoPath + " && git remote | grep ^origin$";
JSONObject remoteExist = ShellUtil.executeAndGetExitStatus(cmd); JSONObject remoteExist = ShellUtil.executeAndGetExitStatus(cmd);
if (remoteExist.getInteger("exitStatus") != 0) { if (remoteExist.getInteger("exitStatus") != 0) {
if (remoteExist.getString("out").contains("fatal")) { if (remoteExist.getString("out").contains("fatal")) {
logger.warn("版本库不完整先进行init操作, tpiID: {}, tpiGitURL: {}, 检查cmd: {}", tpiID, tpiGitURL, cmd); logger.warn("版本库不完整先进行init操作, tpiID: {}, tpiGitURL: {}, 检查cmd: {}", tpiID, tpiGitURL, cmd);
ShellUtil.execute("cd " + tpiRepoPath + " && git init"); ShellUtil.execute("cd " + tpiRepoPath + " && git init");
} }
logger.warn("origin主机名不存在先添加主机名及其git地址remoteName: {}, tpiGitURL: {}, tpiID: {}", "origin", tpiGitURL, logger.warn("origin主机名不存在先添加主机名及其git地址remoteName: {}, tpiGitURL: {}, tpiID: {}", "origin", tpiGitURL,
tpiID); tpiID);
cmd = "cd " + tpiRepoPath + " && git remote add origin " + tpiGitURL; cmd = "cd " + tpiRepoPath + " && git remote add origin " + tpiGitURL;
JSONObject addRemote = ShellUtil.executeAndGetExitStatus(cmd, 3); JSONObject addRemote = ShellUtil.executeAndGetExitStatus(cmd, 3);
if (addRemote.getInteger("exitStatus") != 0) { if (addRemote.getInteger("exitStatus") != 0) {
logger.error("origin主机名不存在,添加主机名失败, tpiGitURL: {}, tpiID: {}, cmd: {}", tpiGitURL, tpiID, cmd); logger.error("origin主机名不存在,添加主机名失败, tpiGitURL: {}, tpiID: {}, cmd: {}", tpiGitURL, tpiID, cmd);
throw new GameException("origin主机名不存在无法完成pull操作,tpiID:" + tpiID); throw new GameException("origin主机名不存在无法完成pull操作,tpiID:" + tpiID);
} else { } else {
logger.debug("origin主机名不存在,添加主机名成功, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID); logger.debug("origin主机名不存在,添加主机名成功, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
} }
} }
// 执行pull操作 // 执行pull操作
long currentPullStart = System.currentTimeMillis(); long currentPullStart = System.currentTimeMillis();
logger.debug("##----> gitpull 开始包括git config ; set remote当前时间为" + currentPullStart); logger.debug("##----> gitpull 开始包括git config ; set remote当前时间为" + currentPullStart);
String pullCommand = "cd " + tpiRepoPath + " && git config user.email educoder@163.com" String pullCommand = "cd " + tpiRepoPath + " && git config user.email educoder@163.com"
+ " && git config user.name educoder " + "&& git config core.fileMode false" + // 忽略文件权限 + " && git config user.name educoder " + "&& git config core.fileMode false" + // 忽略文件权限
"&& git remote set-url origin " + tpiGitURL + " && git pull origin master"; // 本地直接清除所有改动 "&& git remote set-url origin " + tpiGitURL + " && git pull origin master"; // 本地直接清除所有改动
JSONObject result = ShellUtil.executeAndGetExitStatus(pullCommand, 3); JSONObject result = ShellUtil.executeAndGetExitStatus(pullCommand, 3);
logger.debug("result: {}, out: {}", result.getInteger("exitStatus"), result.getString("out")); logger.debug("{} git pull result: {}, out: {}", tpiID, result.getInteger("exitStatus"), result.getString("out"));
if (result.getString("out").contains("commit your changes or stash them before you can merge")) { if (result.getString("out").contains("commit your changes or stash")
String stashCmd = "cd " + tpiRepoPath + " && git stash save " || result.getString("out").contains("would be overwritten by merge")) {
+ TimeHelper.getCurrentTime("yyyyMMddHHmmss"); String stashCmd = "cd " + tpiRepoPath + " && git stash save "
logger.info("tpi {} git pull与未commit代码冲突,先保存本地代码, cmd:{}", tpiID, stashCmd); + TimeHelper.getCurrentTime("yyyyMMddHHmmss");
result = ShellUtil.executeAndGetExitStatus(stashCmd, 3); logger.info("tpi {} git pull与未commit代码冲突,先保存本地代码, cmd:{}", tpiID, stashCmd);
if (result.getInteger("exitStatus") != 0) { result = ShellUtil.executeAndGetExitStatus(stashCmd, 3);
logger.error("tpi {} git pull与未commit代码冲突,保存本地代码失败, cmd:{}, error: {}", tpiID, stashCmd, result.getString("out")); if (result.getInteger("exitStatus") != 0) {
throw new GameException("git pull与未commit代码冲突,保存本地代码失败,tpiID:" + tpiID); logger.error("tpi {} git pull与未commit代码冲突,保存本地代码失败, cmd:{}, error: {}", tpiID, stashCmd, result.getString("out"));
} throw new GameException("git pull与未commit代码冲突,保存本地代码失败,tpiID:" + tpiID);
logger.info("tpi {} git pull与未commit代码冲突, 保存本地代码代码后, 再pull", tpiID); }
result = ShellUtil.executeAndGetExitStatus(pullCommand, 3); logger.info("tpi {} git pull与未commit代码冲突, 保存本地代码代码后, 再pull", tpiID);
} else if(result.getString("out").contains("Merge conflict") result = ShellUtil.executeAndGetExitStatus(pullCommand, 3);
|| result.getString("out").contains("Pull is not possible because you have unmerged files")) { }
String resetCmd = "cd " + tpiRepoPath + " && git reset --hard FETCH_HEAD "; if(result.getString("out").contains("Merge conflict")
logger.info("tpi {} git pull与本地commit冲突, 使用远程版本, cmd:{}", tpiID, resetCmd); || result.getString("out").contains("have unmerged files")) {
JSONObject resetResult = ShellUtil.executeAndGetExitStatus(resetCmd, 3); String resetCmd = "cd " + tpiRepoPath + " && git reset --hard FETCH_HEAD ";
if (resetResult.getInteger("exitStatus") != 0) { logger.info("tpi {} git pull与本地commit冲突, 使用远程版本, cmd:{}", tpiID, resetCmd);
logger.error("tpi {}git pull与本地commit冲突, 使用远程版本失败, cmd:{}, error: {}", tpiID, resetCmd, resetResult.getString("out")); JSONObject resetResult = ShellUtil.executeAndGetExitStatus(resetCmd, 3);
throw new GameException("git pull与本地commit冲突, 使用远程版本失败,tpiID:" + tpiID); if (resetResult.getInteger("exitStatus") != 0) {
} else { logger.error("tpi {}git pull与本地commit冲突, 使用远程版本失败, cmd:{}, error: {}", tpiID, resetCmd, resetResult.getString("out"));
return; throw new GameException("git pull与本地commit冲突, 使用远程版本失败,tpiID:" + tpiID);
} } else {
return;
}
}
long currentPullEnd = System.currentTimeMillis();
logger.debug("##----> gitpull 结束,总耗时间为:" + (currentPullEnd - currentPullStart));
if (result.getInteger("exitStatus") != 0) {
logger.info("pullCommand: {}, tpiID: {}", pullCommand, tpiID);
logger.info("output: {}, tpiID: {}", result.getString("out"), tpiID);
// 如果pull失败先check一下看是不是远端版本库HEAD丢失如果是进行修复
check(tpiID, tpiGitURL);
backupPullError(path, tpiRepoPath, tpiID);
// 重新clone
gitClone(path, tpiGitURL, "origin", tpiRepoName, "--depth=1");
logger.debug("gitPull冲突重新clone结束, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
} else {
// 判段是否成功拉取到更新
int rePullTimes = 10;
while (contentModified == 1 && rePullTimes > 0 && "Already up-to-date.".equals(result.getString("out"))) {
try {
Thread.sleep(200);
} catch (Exception e) {
}
result = ShellUtil.executeAndGetExitStatus(pullCommand, 1);
rePullTimes--;
}
if (rePullTimes == 0) {
logger.error("版本库有更新2s之内拉取版本库更新失败, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
} else {
logger.debug("pull成功, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
}
}
} }
long currentPullEnd = System.currentTimeMillis();
logger.debug("##----> gitpull 结束,总耗时间为:" + (currentPullEnd - currentPullStart));
if (result.getInteger("exitStatus") != 0) {
logger.info("pullCommand: {}, tpiID: {}", pullCommand, tpiID);
logger.info("output: {}, tpiID: {}", result.getString("out"), tpiID);
// 如果pull失败先check一下看是不是远端版本库HEAD丢失如果是进行修复
check(tpiID, tpiGitURL);
backupPullError(path, tpiRepoPath, tpiID);
// 重新clone
gitClone(path, tpiGitURL, "origin", tpiRepoName, "--depth=1");
logger.debug("gitPull冲突重新clone结束, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
} else {
// 判段是否成功拉取到更新
int rePullTimes = 10;
while (contentModified == 1 && rePullTimes > 0 && "Already up-to-date.".equals(result.getString("out"))) {
try {
Thread.sleep(200);
} catch (Exception e) {
}
result = ShellUtil.executeAndGetExitStatus(pullCommand, 1);
rePullTimes--;
}
if (rePullTimes == 0) {
logger.error("版本库有更新2s之内拉取版本库更新失败, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
} else {
logger.debug("pull成功, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
}
}
}
private void backupPullError(String path, String tpiRepoPath, String tpiID) { private void backupPullError(String path, String tpiRepoPath, String tpiID) {
try { try {