【C#】使用SVN钩子自动更新服务器

1 篇文章 0 订阅

博客重建工作一切从0开始,万事开头难,期间很多想法想来简单,但付诸实践却很是棘手。

服务器是Win2012 R2 + IIS7

我所使用的SVN服务商是免费的SVNBucket,它提供了一个很给力的功能,SVN钩子,意思就是每次Commit后,SVNBucker那边自动访问这个url,你的web服务器就可以接收到,然后触发你自己写的功能,比如更新SVN或者其他东西,达到服务器自动同步的目的,也就不用再沙雕的在服务器上每1分钟自动更新,浪费本就可怜的内存。

目录

 

失败的方案:

成功的方案:


失败的方案:

最开始的想法是写个batch,然后控制器触发CMD调用。

批处理内容:

svn update D:\Web1
svn update D:\Web2

控制器内容: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Diagnostics;
using System.IO;

namespace Web.Controllers
{
    public class TestController : Controller
    {
        public String Update()
        {
            WindowsTools t = new WindowsTools();
            var str = t.RunCmd2(@"D:\SVNupdate.bat").ToString();
            return str;
        }
    }

    public class WindowsTools
    {
        /// <summary>
        /// 运行cmd命令
        /// 不显示命令窗口
        /// </summary>
        /// <param name="cmdExe">指定应用程序的完整路径</param>
        public string RunCmd2(string cmdExe)
        {
            try
            {
                using (Process myPro = new Process())
                {
                    myPro.StartInfo.FileName = "cmd.exe";
                    myPro.StartInfo.UseShellExecute = false;
                    myPro.StartInfo.RedirectStandardInput = true;
                    myPro.StartInfo.RedirectStandardOutput = true;
                    myPro.StartInfo.RedirectStandardError = true;
                    myPro.StartInfo.CreateNoWindow = true;
                    myPro.Start();
                    myPro.StandardInput.WriteLine(cmdExe);
                    myPro.StandardInput.AutoFlush = true;
                    myPro.StandardInput.WriteLine("exit");
                    string outStr = myPro.StandardOutput.ReadToEnd();
                    myPro.Close();
                    return outStr;
                }
            }catch(Exception e)
            {
                return e.Message;
            }
            
        }

    }
}

看起来一切都那么美好,但是,线上环境的cmd调用始终有问题,就是无法触发。

控制器返回结果:

Microsoft Windows [版本 10.0.17763.775]
(c) 2018 Microsoft Corporation。保留所有权利。

C:\\windows\\sytstem32\\inetsrv>D:\\SVNupdate.bat.bat

C:\\windows\\sytstem32\\inetsrv>svn update D:\\Web1

C:\\windows\\sytstem32\\inetsrv>svn update D:\\Web2

C:\\windows\\sytstem32\\inetsrv>exit

执行完【svn update】后竟是一片空白。。。。。

无奈各种资料查阅,猜测原因是权限问题。

于是查阅到了一篇关于解决权限的IIS设置解决方案:https://www.cnblogs.com/yuyuko/p/10697856.html 

设置后再加服务器重启,还是不行,包括文件上给足权限,cmd给足权限,快捷方式扔windows32目录, 各种折腾后,我放弃了。。。

实在是太无奈了,为了体验下SVN钩子神奇的功能,只能逼我大招了。。。

我想到了另一个解决方法。进程间通信。。。


成功的方案:

【终极方案】

首先,另写一个控制台exe程序,24小时运行在服务器上,并把这个小程序的快捷方式扔进【C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp】这个目录(开机启动项目录),然后最小化运行就行了。

using System;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Threading;

namespace updatesvn
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread receiveDataThread = new Thread(ReceiveDataFromClient);
            receiveDataThread.IsBackground = true;
            receiveDataThread.Start();
            receiveDataThread.Join();
        }

        private static NamedPipeServerStream _pipeServer;
        private static void ReceiveDataFromClient()
        {
            while (true)
            {
                try
                {
                    _pipeServer = new NamedPipeServerStream("svnPipe", PipeDirection.InOut, 2);
                    _pipeServer.WaitForConnection(); //Waiting  
                    StreamReader sr = new StreamReader(_pipeServer);
                    string recData = sr.ReadLine();
                    if (recData == "update"){
                        System.Diagnostics.Process.Start(@"D:\SVNupdate.bat");
                    }else if(recData == "Exit"){
                        Console.WriteLine("Pipe Exit.");
                        Process.GetCurrentProcess().Kill();
                    }

                    Console.WriteLine(recData + DateTime.Now.ToString());
                    Thread.Sleep(1000);
                    sr.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

    }
}

C#.net 控制器代码:

using System;
using System.Web.Mvc;
using Web.Tools;

namespace Web.Controllers
{
    public class SVNUpdateController : Controller
    {
        public String Update()
        {
            var b = WindowsTools.SendUpdate().ToString();
            return b+DateTime.Now.ToString();
        }
    }

}

 另附一个C#.net 管道通信工具类

using System.IO;
using System.IO.Pipes;
using System.Security.Principal;

namespace Web.Tools
{
    public class WindowsTools
    {
        private static NamedPipeClientStream _pipeClient;
        public static bool SendData(string cmd)
        {
            try
            {
                _pipeClient = null;
                _pipeClient = new NamedPipeClientStream(".", "svnPipe", PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation);
                _pipeClient.Connect();
                StreamWriter sw = new StreamWriter(_pipeClient);
                sw.WriteLine(cmd);
                sw.Flush();
                sw.Close();
                return true;
            }
            catch {
                return false;
            }
        }
        public static bool SendUpdate() {
            return SendData("update");
        }
    }
}

大功告成。SVN钩子测试成功。如果以后有更好的方案,我再回来补充上。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用Python可以通过调用svn命令行来自动更新svn工作目录。可以使用subprocess模块来执行svn命令行,并适用于Windows和Mac OS。以下是一个示例代码片段: ```python import subprocess def update_svn(workingspacepath): subprocess.call(['svn', 'update', workingspacepath]) # 使用方法 workingspacepath = '/path/to/workingspace' update_svn(workingspacepath) ``` 该代码片段中,我们通过subprocess调用了svn update命令来更新svn工作目录。其中,`workingspacepath`是svn工作目录的路径。 请注意,这只是一个简化的示例代码片段,你可能需要根据你的实际需求进行适当的修改。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Python处理svn状态脚本](https://download.csdn.net/download/burnrock/9332463)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Python写自动化之SVN更新](https://blog.csdn.net/sogouauto/article/details/44151273)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值