Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 806
Paolo B Avatar asked Apr 09 '18 15:04

Paolo B


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

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.

like image 176
hujtomi Avatar answered Oct 16 '22 19:10

hujtomi


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

like image 24
Tarun Lalwani Avatar answered Oct 16 '22 18:10

Tarun Lalwani