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

时间:2021-08-24 16:27:03

I want to push files from a folder on Azure App Service to a Git repository.

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

I have copied the local git repo up to the server and I'm using LibGit2Sharp to commit and push these files:

我已将本地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);
}

It works, but seems to take a while and and this seems a bit clunky. Is there a more efficient way to achieve this via code, or perhaps directly via Azure (config or Azure Functions)?

它工作,但似乎需要一段时间,这似乎有点笨重。有没有更有效的方法通过代码实现这一点,或者可能直接通过Azure(配置或Azure功能)?

2 个解决方案

#1


6  

In case of Azure Apps you can still bundle embedded exes, There a portable Git available on below link

对于Azure应用程序,您仍然可以捆绑嵌入式exes,在下面的链接上有一个便携式Git

https://github.com/sheabunge/GitPortable

https://github.com/sheabunge/GitPortable

You should bundle that with your app and create a batch file as well. And then you should launch it using C# code

您应该将其与您的应用捆绑在一起并创建批处理文件。然后你应该使用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: Credits Executing Batch File in C#

PS:在C#中执行批处理文件的积分

Another SO thread that talk about something similar

另一个谈论类似事情的SO线程

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

Azure App Service,运行本机EXE以转换文件

How to run a .EXE in an Azure App Service

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

Run .exe executable file in Azure Function

在Azure Function中运行.exe可执行文件

#2


1  

I think instead of the local disk of the App Service, you should use Azure storage, because later when you have to scale your service, on scale down, some content might get lost from the D:\home\site\wwwroot\repo folder, of if you scale out, the different instances will have different content in this folder.

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

And if you check your App Services console: git preinstalled You can see the git is already preinstalled, so you don't really need any lib or portable git, you can just run your git command with the System.Diagnostics.Process.Start() method.

如果您检查您的App Services控制台:git preinstalled您可以看到git已经预先安装,因此您不需要任何lib或便携式git,您可以使用System.Diagnostics.Process.Start运行您的git命令( ) 方法。

#1


6  

In case of Azure Apps you can still bundle embedded exes, There a portable Git available on below link

对于Azure应用程序,您仍然可以捆绑嵌入式exes,在下面的链接上有一个便携式Git

https://github.com/sheabunge/GitPortable

https://github.com/sheabunge/GitPortable

You should bundle that with your app and create a batch file as well. And then you should launch it using C# code

您应该将其与您的应用捆绑在一起并创建批处理文件。然后你应该使用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: Credits Executing Batch File in C#

PS:在C#中执行批处理文件的积分

Another SO thread that talk about something similar

另一个谈论类似事情的SO线程

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

Azure App Service,运行本机EXE以转换文件

How to run a .EXE in an Azure App Service

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

Run .exe executable file in Azure Function

在Azure Function中运行.exe可执行文件

#2


1  

I think instead of the local disk of the App Service, you should use Azure storage, because later when you have to scale your service, on scale down, some content might get lost from the D:\home\site\wwwroot\repo folder, of if you scale out, the different instances will have different content in this folder.

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

And if you check your App Services console: git preinstalled You can see the git is already preinstalled, so you don't really need any lib or portable git, you can just run your git command with the System.Diagnostics.Process.Start() method.

如果您检查您的App Services控制台:git preinstalled您可以看到git已经预先安装,因此您不需要任何lib或便携式git,您可以使用System.Diagnostics.Process.Start运行您的git命令( ) 方法。