如何从Azure应用服务将文件推送到Bitbucket仓库?(C#)

12

我想要将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函数),是否有更有效的方法来实现这个目标?


你为什么不设置git配置并使用命令行呢? - Varun Garg
@JamesP,看起来在Azure应用服务中运行exe是可能的?https://stackoverflow.com/questions/46337633/azure-app-service-run-a-native-exe-to-convert-a-file,https://dev59.com/6aDia4cB1Zd3GeqPHKUB,https://dev59.com/KVcO5IYBdhLWcg3wtz5g,并且您可以将Git Portable与您的代码一起嵌入https://github.com/sheabunge/GitPortable。 - Tarun Lalwani
@JamesP,你有机会看那些链接了吗? - Tarun Lalwani
2个回答

5
在Azure应用中,您仍然可以捆绑嵌入式exe文件,下面链接提供了可移植的Git: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: 致谢 在C#中执行批处理文件

另一个与此类似的SO线程

Azure应用服务,运行本地EXE文件以转换文件

如何在Azure应用服务中运行.EXE文件

在Azure函数中运行.EXE可执行文件


2

我认为,在扩展服务规模时,应该使用 Azure 存储而不是 App Service 的本地磁盘。因为在缩减规模时,D:\home\site\wwwroot\repo 文件夹中的某些内容可能会丢失;在扩展规模时,则不同的实例将在此文件夹中具有不同的内容。

如果您检查 App Service 控制台: 预装了 Git 您可以看到 Git 已经预先安装,所以您不需要任何库或便携式 Git,只需使用 System.Diagnostics.Process.Start() 方法运行您的 Git 命令即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接