C#从Azure应用服务将文件推送到Bitbucket存储库?

时间:2018-04-09 15:34:31

标签: git azure bitbucket libgit2sharp

我想将文件从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功能)?

2 个答案:

答案 0 :(得分:6)

对于Azure应用程序,您仍然可以捆绑嵌入式exes,在下面的链接上可以使用便携式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

答案 1 :(得分:1)

我认为代替App Service的本地磁盘,您应该使用Azure存储,因为稍后当您必须扩展服务时,按比例缩小,某些内容可能会从D:\ home \ site \ wwwroot丢失\ repo文件夹,如果您向外扩展,不同的实例将在此文件夹中具有不同的内容。

如果您检查App Services控制台: git preinstalled 你可以看到git已经预装了,所以你真的不需要任何lib或者可移植的git,你可以使用System.Diagnostics.Process.Start()方法运行你的git命令。