C# 将文件从 Azure 应用服务推送到 Bitbucket 存储库?
C# Push Files to Bitbucket repository from an Azure App Service?
我想将文件从 Azure 应用服务上的文件夹推送到 Git 存储库。
我已将本地 git 存储库复制到服务器,我正在使用 LibGit2Sharp 提交并推送这些文件:
using (var repo = new Repository(@"D:\home\site\wwwroot\repo"))
{
// Stage the file
Commands.Stage(repo, "*");
// Create the committer's signature and commit
Signature author = new Signature("translator", "example.com", DateTime.Now);
Signature committer = author;
// Commit to the repository
Commit commit = repo.Commit($"Files updated {DateTime.Now}", author, committer);
Remote remote = repo.Network.Remotes["origin"];
var options = new PushOptions
{
CredentialsProvider = (_url, _user, _cred) =>
new UsernamePasswordCredentials
{
Username = _settings.UserName,
Password = _settings.Password
}
};
repo.Network.Push(remote, @"+refs/heads/master", options);
}
它有效,但似乎需要一段时间,而且这似乎有点笨拙。是否有更有效的方法通过代码或直接通过 Azure(配置或 Azure Functions)来实现?
如果是 Azure Apps,您仍然可以捆绑嵌入式 exe,下面有一个便携式 Git link
https://github.com/sheabunge/GitPortable
您应该将其与您的应用程序捆绑在一起并创建一个批处理文件。然后你应该使用 C# 代码启动它
static void ExecuteCommand(string command)
{
var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
var process = Process.Start(processInfo);
process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
Console.WriteLine("output>>" + e.Data);
process.BeginOutputReadLine();
process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
Console.WriteLine("error>>" + e.Data);
process.BeginErrorReadLine();
process.WaitForExit();
Console.WriteLine("ExitCode: {0}", process.ExitCode);
process.Close();
}
PS:积分 Executing Batch File in C#
另一个讨论类似内容的 SO 线程
我认为你应该使用 Azure 存储而不是应用服务的本地磁盘,因为稍后当你必须扩展你的服务时,在缩小时,一些内容可能会从 D:\home\site\wwwroot\repo 文件夹,如果你横向扩展,不同的实例将在这个文件夹中有不同的内容。
如果您检查应用服务控制台:
git preinstalled
您可以看到 git 已经预装,所以您实际上不需要任何库或可移植 git,您只需 运行 您的 git 命令和 System.Diagnostics.Process.Start() 方法。
我想将文件从 Azure 应用服务上的文件夹推送到 Git 存储库。
我已将本地 git 存储库复制到服务器,我正在使用 LibGit2Sharp 提交并推送这些文件:
using (var repo = new Repository(@"D:\home\site\wwwroot\repo"))
{
// Stage the file
Commands.Stage(repo, "*");
// Create the committer's signature and commit
Signature author = new Signature("translator", "example.com", DateTime.Now);
Signature committer = author;
// Commit to the repository
Commit commit = repo.Commit($"Files updated {DateTime.Now}", author, committer);
Remote remote = repo.Network.Remotes["origin"];
var options = new PushOptions
{
CredentialsProvider = (_url, _user, _cred) =>
new UsernamePasswordCredentials
{
Username = _settings.UserName,
Password = _settings.Password
}
};
repo.Network.Push(remote, @"+refs/heads/master", options);
}
它有效,但似乎需要一段时间,而且这似乎有点笨拙。是否有更有效的方法通过代码或直接通过 Azure(配置或 Azure Functions)来实现?
如果是 Azure Apps,您仍然可以捆绑嵌入式 exe,下面有一个便携式 Git link
https://github.com/sheabunge/GitPortable
您应该将其与您的应用程序捆绑在一起并创建一个批处理文件。然后你应该使用 C# 代码启动它
static void ExecuteCommand(string command)
{
var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
var process = Process.Start(processInfo);
process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
Console.WriteLine("output>>" + e.Data);
process.BeginOutputReadLine();
process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
Console.WriteLine("error>>" + e.Data);
process.BeginErrorReadLine();
process.WaitForExit();
Console.WriteLine("ExitCode: {0}", process.ExitCode);
process.Close();
}
PS:积分 Executing Batch File in C#
另一个讨论类似内容的 SO 线程
我认为你应该使用 Azure 存储而不是应用服务的本地磁盘,因为稍后当你必须扩展你的服务时,在缩小时,一些内容可能会从 D:\home\site\wwwroot\repo 文件夹,如果你横向扩展,不同的实例将在这个文件夹中有不同的内容。
如果您检查应用服务控制台: git preinstalled 您可以看到 git 已经预装,所以您实际上不需要任何库或可移植 git,您只需 运行 您的 git 命令和 System.Diagnostics.Process.Start() 方法。