我想将文件从Azure App Service上的文件夹推送到Git存储库。

我已经将本地git repo复制到服务器上,并且正在使用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应用,您仍然可以捆绑嵌入式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:积分Executing Batch File in C#

另一个谈论类似问题的SO线程

Azure App Service, run a native EXE to convert a file

How to run a .EXE in an Azure App Service

Run .exe executable file in Azure Function

09-04 15:58
查看更多