使用Git获取最新代码并使用msbuild编译项目

因部署需要获取最新代码并编译项目,使用Git本身的命令即可

static void GitPull(string codePath)
        {
            if (System.IO.File.Exists(codePath))
            {
                FileInfo fi = new FileInfo(codePath);
                Process process = new Process();
                process.StartInfo.FileName = "cmd.exe";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.RedirectStandardInput = true;

                process.OutputDataReceived += Process_OutputDataReceived;
                process.ErrorDataReceived += Process_ErrorDataReceived;

                process.StartInfo.Arguments = fi.DirectoryName;
                process.Start();

                Console.WriteLine("Getting new codes form git at {0} ", process.StartTime);
                Console.WriteLine("");

                process.StandardInput.WriteLine(@"cd " + fi.DirectoryName);
                process.StandardInput.WriteLine(fi.DirectoryName.Substring(0, 2));
                process.StandardInput.WriteLine(@"git pull");
                process.StandardInput.Close();
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                process.WaitForExit();
                process.CancelOutputRead();
                process.CancelErrorRead();
                Console.WriteLine("finished update codes!");
            }
            else
            {
                Console.WriteLine("Invalid code path,please check the settings ,press any key to exist!");
                Console.Read();
                Environment.Exit(0);
            }
        }

编译项目代码

 static void BuildProject(string projectPath)
        {
            if (System.IO.File.Exists(projectPath))
            {
                string msbuildPath = ConfigurationManager.AppSettings["MSBuildPath"];

                DirectoryInfo di = new DirectoryInfo(msbuildPath);
                if (di.Exists == false)
                {
                    Console.WriteLine("msbuild path is invalid!");
                    Console.Read();
                    return;
                }
                FileInfo fi = new FileInfo(projectPath);
                Process process = new Process();
                process.StartInfo.FileName = "cmd.exe";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.RedirectStandardInput = true;

                process.OutputDataReceived += Process_OutputDataReceived;
                process.ErrorDataReceived += Process_ErrorDataReceived;

                process.StartInfo.Arguments = fi.DirectoryName;
                process.Start();

                Console.WriteLine("Getting new codes form git at {0} ", process.StartTime);
                Console.WriteLine("");

                process.StandardInput.WriteLine(@"cd " + msbuildPath);

                process.StandardInput.WriteLine(di.Root.Name.Replace("\\", ""));
                process.StandardInput.WriteLine(@"MSBuild.exe {0} -property:Configuration=Release -t:rebuild",projectPath);
                process.StandardInput.Close();
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                process.WaitForExit();
                process.CancelOutputRead();
                process.CancelErrorRead();
                Console.WriteLine("finished build project!");
            }
            else
            {
                Console.WriteLine("Invalid code path,please check the settings ,press any key to exist!");
                Console.Read();
                Environment.Exit(0);
            }
        }

 

posted on 2019-07-24 14:46  Tencent/Tim  阅读(309)  评论(0编辑  收藏  举报

导航