C# Push Files to Bitbucket repository from an Azure App Service?
Asked Answered
B

2

12

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

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

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)?

Buckle answered 9/4, 2018 at 15:34 Comment(3)
Why don't you just setup up git config and use cli?Accuse
@JamesP, it seems it is possible to run a exe in Azure app service? #46338133, #43234415, #45348998, and you can embed the git portable with your code github.com/sheabunge/GitPortableTerraterrace
@JamesP, did you get a chance to look at those links?Terraterrace
T
5

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

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

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#

Another SO thread that talk about something similar

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

Terraterrace answered 23/4, 2018 at 13:45 Comment(0)
S
2

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.

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.

Stony answered 24/4, 2018 at 23:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.