コード例 #1
0
        /// <summary>
        /// 下载源代码
        /// </summary>
        /// <param name="_buildBean"></param>
        /// <returns></returns>
        private static SourceCodeBean DownloadSourceCode(LogEngin _logEngin, BuildTaskBean _buildBean)
        {
            IDownloadSourceCode _dsc = null;
            //根据projectName 可以读取project目录下面的Source.config 文件
            SourceCodeBean _sourceCodeBean = null;
            string         _projectPath    = Path.Combine(Constants.CurrentConfigProjects, _buildBean.ProjectName);

            if (!IOUtils.DirExists(_projectPath))
            { //表示文件目录不存在 配置有问题
                throw new Exception("项目" + _buildBean.ProjectName + "不存在,配置有问题");
            }

            string _sourceConfigFile = Path.Combine(_projectPath, "Source.config");

            if (!IOUtils.FileExists(_sourceConfigFile))
            { //表示文件不存在 配置有问题
                throw new Exception("项目" + _buildBean.ProjectName + " Source.config 不存在,配置有问题");
            }

            string _sourceConfigContent = IOUtils.GetUTF8String(_sourceConfigFile);
            string _sourceType          = "git";

            try
            {
                JObject _sourceConfigObj = JObject.Parse(_sourceConfigContent);
                _sourceCodeBean          = new SourceCodeBean();
                _sourceCodeBean.SourceId = _sourceConfigObj.GetValue("SourceId").ToString();
                _sourceCodeBean.Url      = _sourceConfigObj.GetValue("Url").ToString();
                _sourceCodeBean.Port     = _sourceConfigObj.GetValue("Port").ToString();
                _sourceCodeBean.Account  = _sourceConfigObj.GetValue("Account").ToString();
                _sourceCodeBean.Password = _sourceConfigObj.GetValue("Password").ToString();
                _sourceType = _sourceConfigObj.GetValue("SourceType").ToString();
            }
            catch (Exception _ex)
            {
                throw new Exception("Source.config 配置内容有误! \n" + _ex);
            }

            if ("git".Equals(_sourceType))
            {
                _dsc = new GitDownloadSourceCode();
            }

            _sourceCodeBean.DestPath = Path.Combine(Constants.SourceFile, _sourceCodeBean.SourceId, _buildBean.ProjectName, _buildBean.BranchName);
            FileUtils.CreateDir(_sourceCodeBean.DestPath);
            _logEngin.Info("创建 " + _sourceCodeBean.DestPath + "目录");

            _logEngin.Info("去远程仓库下载代码,下载代码到指定目录: " + _sourceCodeBean.DestPath);
            //根据sourceConfig里面的配置去远程仓库下载代码
            int _code = _dsc.DownloadSourceCode(_logEngin, _sourceCodeBean, _buildBean);

            if (_code != 0 && _code != 128)
            {
                throw new Exception("源代码下载失败!请检查 " + _buildBean.ProjectName + " 项目下的 Source.config文件");
            }

            return(_sourceCodeBean);
        }
コード例 #2
0
        static void BuildThreadChild()
        {
            while (true)
            {
                //请求http获取打包任务
                long _cDate = DateTime.Now.ToFileTime();
                Dictionary <string, object> _dictData = new Dictionary <string, object>();
                _dictData.Add("TimeStamp", _cDate);
                var    _sign        = SignData.Md5SignDict(_configBean.SecurityKey, _dictData);
                string _postDataStr = "TimeStamp=" + _cDate + "&Sign=" + _sign;
                string _result      = HttpUtils.HttpGet(Constants.GET_BUILD_TASK, _postDataStr);

                //如果取到任务
                if (_result != null && _result.Length > 0 && !"null".Equals(_result))
                {
                    BuildTaskBean _buildBean = new BuildTaskBean();
                    try
                    {
                        JObject _buildConfigObj = JObject.Parse(_result);
                        _buildBean.TaskId      = _buildConfigObj.GetValue("Id").ToString();
                        _buildBean.ProjectId   = _buildConfigObj.GetValue("ProjectId").ToString();
                        _buildBean.ProjectName = _buildConfigObj.GetValue("ProjectName").ToString();
                        _buildBean.BranchName  = _buildConfigObj.GetValue("BranchName").ToString();
                    }
                    catch (Exception ex)
                    {
                        LogUtils.Error(null, new Exception("解析打包任务数据请求失败 " + _result));
                        LogUtils.Error(null, ex);
                        throw;
                    }

                    LogEngin _logEngin = new LogEngin(_buildBean.ProjectName + " 项目打包", _buildBean.TaskId);

                    //根据TaskID创建一个临时目录
                    string _projectTempDir = Path.Combine(Constants.Temp, _buildBean.TaskId, _buildBean.ProjectName);
                    FileUtils.CreateDir(_projectTempDir);
                    _logEngin.Info("创建 " + _projectTempDir + " 临时目录");

                    //////////////////下载源代码
                    try
                    {
                        SourceCodeBean _sourceCodeBean = DownloadSourceCode(_logEngin, _buildBean);
                        ////////////////build源代码
                        BuildSource(_logEngin, _buildBean, _sourceCodeBean, _projectTempDir);
                    }
                    catch (Exception ex)
                    {
                        ////////build失败
                        _logEngin.Error(ex);
                        _logEngin.IsSuccess = false;
                    }

                    //上传日志文件
                    string _log     = _logEngin.ToHtml();
                    string _logPath = Path.Combine(Constants.Temp, _buildBean.TaskId + ".html");
                    IOUtils.WriteUTF8String(_logPath, _log);
                    string _logUrl = UploadLogFile(_logEngin, _logPath);


                    //反馈打包结果
                    _cDate = DateTime.Now.ToFileTime();
                    _dictData.Clear();

                    int _state = (_logEngin.IsSuccess ? 2 : 1);

                    _dictData.Add("TimeStamp", _cDate);
                    _dictData.Add("Id", _buildBean.TaskId);
                    _dictData.Add("State", _state);
                    _dictData.Add("LogUrl", _logUrl);

                    _sign = SignData.Md5SignDict(_configBean.SecurityKey, _dictData);

                    JObject _postDataJson = new JObject();
                    _postDataJson.Add("Id", _buildBean.TaskId);
                    _postDataJson.Add("State", _state);
                    _postDataJson.Add("LogUrl", _logUrl);
                    _postDataJson.Add("TimeStamp", _cDate);
                    _postDataJson.Add("Sign", _sign);

                    HttpUtils.HttpPut(Constants.GET_BUILD_TASK, _postDataJson.ToString());

                    Console.WriteLine("Build完成");
                }
                else //没有取到任务,隔段时间再去取
                {
                    Thread.Sleep(_configBean.GetBuildTaskInterval * 1000);
                }
            }
        }
コード例 #3
0
        /// <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;
            }
        }