/// <summary> /// 等待JenkinsBuildTask并返回任务结果 /// </summary> /// <param name="appName">应用名称</param> /// <param name="environment">所属环境</param> /// <param name="logDirName">日志输出文件夹名</param> /// <returns>Tuple bool--是否编译成功, string--成功则为任务id 则是异常信息或空, string--编译的SVN的版本 </returns> private static Tuple <bool, string, string> WaitJkbtAndGetResult(string appName, string environment, string logDirName) { var jkbtId = ""; try { //第一步:检查Jenkins的任务队列是否有等待执行的任务,没人排队任务则继续第二步 var jenkinsQueueUrl = GetJenkinsConfig("JenkinsQueueApiUrl", ""); LogInfoWriter.GetInstance(logDirName).Info($"start check jenkins build task queue is empty, JenkinsQueueApiUrl:{jenkinsQueueUrl}"); var isJkQueueEmpty = ExecExtensions.RetryUntilTrueWithTimeout(() => { var queueInfo = HttpClientUtils.GetJson(jenkinsQueueUrl); var queueInfoJObj = JObject.Parse(queueInfo); Console.WriteLine("jenkins build task queue is not empty"); return(!queueInfoJObj["items"].Values <JObject>().Any()); }, ConfigManager.GetConfigObject("JenkinsQueueCheckTimeout", 5) * OneMinute); if (!isJkQueueEmpty) { throw new Exception("wait jenkins build task finish error; ErrorMsg:check wether jenkins build task queue is empty timout"); } LogInfoWriter.GetInstance(logDirName).Info("check jenkins build task queue is empty end"); //第二步:获取Jenkins的编译任务Id var getJkbtNumberUrl = GetJenkinsConfig("JenkinsGetBuildTaskIdApiUrl", ""); getJkbtNumberUrl = String.Format(getJkbtNumberUrl, appName, environment); LogInfoWriter.GetInstance(logDirName).Info($"start get jenkins build task id, JenkinsGetBuildTaskIdApiUrl:{getJkbtNumberUrl}"); var jkbtNumberInfo = HttpClientUtils.Get(getJkbtNumberUrl); jkbtId = GetJenkinsXmlValue(jkbtNumberInfo); if (String.IsNullOrWhiteSpace(jkbtId)) { throw new Exception("get jenkins build task id error,the jenkins build task id is null or empty!"); } LogInfoWriter.GetInstance(logDirName).Info($"get jenkins build task id:{jkbtId} end"); //第三步:根据Jenkins的编译任务Id获取其执行结果 var getJkbtResultUrl = GetJenkinsConfig("JenkinsGetBuildTaskResultApiUrl", ""); getJkbtResultUrl = String.Format(getJkbtResultUrl, appName, environment, jkbtId); LogInfoWriter.GetInstance(logDirName).Info($"start get jenkins build task:{jkbtId} result, JenkinsGetBuildTaskResultApiUrl:{getJkbtResultUrl}"); var isJkbtFinish = ExecExtensions.RetryUntilTrueWithTimeout(() => { if (String.Equals(JkbtResultSuccess, HttpClientUtils.Get(getJkbtResultUrl), StringComparison.OrdinalIgnoreCase)) { return(ExecExtensions.ResultType.Success); } if (String.Equals(JkbtResultFailure, HttpClientUtils.Get(getJkbtResultUrl), StringComparison.OrdinalIgnoreCase)) { return(ExecExtensions.ResultType.Failure); } return(ExecExtensions.ResultType.Continue); }, ConfigManager.GetConfigObject("JenkinsGetBuildTaskResultTimeout", 10) * OneMinute); LogInfoWriter.GetInstance(logDirName).Info($"get jenkins build task:{jkbtId} result end"); //第四步:根据Jenkins编译任务结果返回最终结果 switch (isJkbtFinish) { case ExecExtensions.ResultType.Success: var getJkbtSvnNumberUrl = GetJenkinsConfig("JenkinsGetBuildTaskSvnNumberUrl", ""); getJkbtSvnNumberUrl = String.Format(getJkbtSvnNumberUrl, appName, environment, jkbtId); LogInfoWriter.GetInstance(logDirName).Info($"start get jenkins build task:{jkbtId} svn number,JenkinsGetBuildTaskSvnNumberUrl:{getJkbtSvnNumberUrl}"); var jkbtSvnNumberInfo = HttpClientUtils.Get(getJkbtSvnNumberUrl); var jkbtSvnNumber = GetJenkinsXmlValue(jkbtSvnNumberInfo); if (String.IsNullOrWhiteSpace(jkbtSvnNumber)) { throw new Exception("$the jenkins build task:{jkbtId} svn number is null or empty!"); } return(Tuple.Create(true, jkbtId, jkbtSvnNumber)); case ExecExtensions.ResultType.Failure: return(Tuple.Create(false, jkbtId, "jenkins build task failure")); case ExecExtensions.ResultType.Timeout: return(Tuple.Create(false, jkbtId, "jenkins build task timeout")); } } catch (Exception ex) { return(Tuple.Create(false, jkbtId, ex.ToString())); } return(Tuple.Create(false, jkbtId, "unknow error")); }