/// <summary> /// build 源代码 /// </summary> /// <param name="_sourceCodeRootDir"></param> /// <param name="_projectTempDir"></param> private static void BuildSource(LogEngin _logEngin, BuildTaskBean _buildBean, SourceCodeBean _sourceBean, string _projectTempDir) { _logEngin.Info("开始build代码"); //找到该目录下面的所有".sln"后缀的文件 //TODO 可能有点耗时,排除.git 目录 ArrayList _slnFiles = FileUtils.GetFiles(_sourceBean.DestPath, "*.sln"); for (int i = 0; i < _slnFiles.Count; i++) { string _slnFile = Path.GetDirectoryName((string)_slnFiles[i]); string msbulidBatPath = _projectTempDir + Path.DirectorySeparatorChar + "msbuild.bat"; StringBuilder _sb = new StringBuilder(); string changeDir = "cd /d " + _slnFile; //string changeDir = "cd /d " + _destPath + Path.DirectorySeparatorChar + "UnitA"; _sb.Append(changeDir + "\n"); _sb.Append("\"" + _configBean.Msbuildpath + "\""); IOUtils.WriteString(msbulidBatPath, _sb.ToString()); if (IOUtils.FileExists(msbulidBatPath)) { _logEngin.Info(msbulidBatPath + " 文件创建成功!"); int _code = CMDUtils.Execute(_logEngin, msbulidBatPath); if (_code == 0) { //删除bat 文件 FileUtils.DeleteFile(msbulidBatPath); _logEngin.Debug("build " + _slnFile + " Success"); } else { _logEngin.Debug("build " + _slnFile + " Fail"); } } } //build 完成 if (_slnFiles != null && _slnFiles.Count > 0) { _sourceCodeRootDirs.Add(_buildBean.TaskId, _sourceBean.DestPath); } else { _logEngin.IsSuccess = false; } }
public int DownloadSourceCode(LogEngin _logEngin, SourceCodeBean _sourceCodeBean, BuildTaskBean _buildTaskBean) { string branch = _buildTaskBean.BranchName; string url = _sourceCodeBean.Url; string dest = _sourceCodeBean.DestPath; string command = "git clone "; if (branch != null && branch.Length > 0) { command = command + "-b " + branch + " "; } command = command + "--progress -v " + url + " " + dest; int _code = CMDUtils.Execute(_logEngin, command); return(_code); }
//public async Task<IHttpActionResult> Get() { // return Ok("成功"); //} public async Task <IHttpActionResult> Post([FromBody] N_BuildPostVm model) { if (!ModelState.IsValid) { throw new Exception("请求参数不正确"); } Dictionary <string, object> _dictData = new Dictionary <string, object>(); _dictData.Add("TimeStamp", model.TimeStamp); _dictData.Add("Url", model.Url); _dictData.Add("ProjectId", model.ProjectId); _dictData.Add("Unit", model.Unit); var _sign = SignData.Md5SignDict("1234567890", _dictData); if (_sign != model.Sign) { throw new Exception("签名不正确"); } //验证通过,下载Qiniu上面的zip文件 string _savePath = Path.Combine(Constants.Temp, Path.GetFileName(model.Url)); WebClient myWebClient = new WebClient(); myWebClient.DownloadFile(model.Url, _savePath); //判断文件是否存在 if (!IOUtils.FileExists(_savePath)) { //表示文件下载失败 throw new Exception(model.Url + " 下载失败"); } //解压文件 ZipFile.ExtractToDirectory(_savePath, Constants.Temp); //删除zip文件 FileUtils.DeleteFile(_savePath); string _sourcePath = Path.Combine(Constants.Temp, model.ProjectId, model.Unit); string _targetPath = Path.Combine(Constants.RootPath, model.ProjectId, model.Unit); if (!IOUtils.DirExists(_targetPath)) { IOUtils.CreateDir(_targetPath); } FileUtils.CopyDir(_sourcePath, _targetPath); FileUtils.DeleteDir(Path.Combine(Constants.Temp, model.ProjectId)); //判断是否存在build.sh脚本文件,如果有就执行 string shellFilePath = Path.Combine(_targetPath, Constants.ScripteFile); if (IOUtils.FileExists(shellFilePath)) { int _code = CMDUtils.Execute(shellFilePath); if (_code == 0) { //_logEngin.Debug("build " + _slnFile + " Success"); } else { //_logEngin.Debug("build " + _slnFile + " Fail"); } } return(Ok()); }