Skip to content

Instantly share code, notes, and snippets.

@seayxu
Last active April 14, 2016 06:38
Show Gist options
  • Save seayxu/4a35cb8f42a2f8a296a7783ee971be3c to your computer and use it in GitHub Desktop.
Save seayxu/4a35cb8f42a2f8a296a7783ee971be3c to your computer and use it in GitHub Desktop.
git webhook asp.net sample
if (!IsPostBack)
{
RetInfo info = new RetInfo();
string ret = null;
try
{
using (Stream input = HttpContext.Current.Request.InputStream)
{
if (input == null || input.Length <= 0)
{
info.Code = 0;
info.Message = "InputStream is null";
ret = JsonHelper.SerializeObject(info);
HttpContext.Current.Response.Write(ret);
HttpContext.Current.Response.End();
}
byte[] bytes = new byte[input.Length];
input.Read(bytes, 0, (int)input.Length);
var data = Encoding.UTF8.GetString(bytes);
if (string.IsNullOrEmpty(data))
{
info.Code = 0;
info.Message = "data is null";
ret = JsonHelper.SerializeObject(info);
HttpContext.Current.Response.Write(ret);
HttpContext.Current.Response.End();
}
else
{
string path = AppDomain.CurrentDomain.BaseDirectory;
string file = DateTime.Now.ToString("yyyyMMddHHmmss");
if (!Directory.Exists(path+"data"))
{
Directory.CreateDirectory(path + "data");
}
System.IO.File.WriteAllText(path + "data\\" + file + ".json", data, Encoding.UTF8);
Dictionary<string, object> dict = JsonHelper.DeserializeObject<Dictionary<string, object>>(data);
if (dict == null || dict.Count <= 0)
{
info.Code = 0;
info.Message = "Json.Deserialize is null";
info.Data = data;
}
else
{
info.Data = dict;
if (dict["ref"].ToString().ToLower().Equals("refs/heads/master"))
{
info.Code = 200;
info.Message = "ok";
Process prc = new Process();
prc.StartInfo.FileName = "cmd.exe";
prc.StartInfo.UseShellExecute = false;
prc.StartInfo.RedirectStandardInput = true;
prc.StartInfo.RedirectStandardOutput = true;
prc.StartInfo.RedirectStandardError = true;
prc.StartInfo.CreateNoWindow = false;
prc.Start();
//string disk = path.Substring(0, 2);
//prc.StandardInput.WriteLine(disk);
//prc.StandardInput.WriteLine("cd " + path);
prc.StandardInput.WriteLine("\"" + ConfigurationManager.AppSettings["cmd"].ToString() + "\" \"" + path + ConfigurationManager.AppSettings["shell"].ToString() + "\"");
prc.StandardInput.Close();
string msg = prc.StandardOutput.ReadToEnd();
File.WriteAllText(path + "data\\" + file + ".deploy", msg, Encoding.UTF8);
}
else
{
info.Code = 100;
info.Message = "not master branch";
}
}
}
}
}
catch (Exception ex)
{
info.Code = 0;
info.Message = ex.Message;
info.Data = ex;
}
ret = JsonHelper.SerializeObject(info);
HttpContext.Current.Response.Write(ret);
HttpContext.Current.Response.End();
}
public class RetInfo
{
public int Code { get; set; }
public string Message { get; set; }
public object Data { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment