ASP.NET Azure C# MVC 5

Using libgit2sharp to commit to Git repositories in C#

I helped design an interesting deployment strategy for an Azure application. For one of our projects, we have an existing upgrade solution that will distribute new versions of our ASP.NET application to our clients. One of our clients runs in the app in Azure. To support this automated distribution, we use Azure’s Git Repository Deploy feature to push up new code to a staging environment. We can then manually swap the staging environment with our production environment after a smoke test, completing the deployment.

One step to this process is commiting the web app to the Azure Git repository. To do this, I took advantage of the Nuget package LibGit2Sharp.

Install-Package LibGit2Sharp -Version 0.21.0.176

I abstracted some of the code used to do this Git repository push into the following GitRepositoryManager class.

using System;
using System.IO;
using System.Linq;
using LibGit2Sharp;

namespace GitDeploy
{
    public class GitRepositoryManager
    {
        private readonly string _repoSource;
        private readonly UsernamePasswordCredentials _credentials;
        private readonly DirectoryInfo _localFolder;

        /// <summary>
        /// Initializes a new instance of the <see cref="GitRepositoryManager" /> class.
        /// </summary>
        /// <param name="username">The Git credentials username.</param>
        /// <param name="password">The Git credentials password.</param>
        /// <param name="gitRepoUrl">The Git repo URL.</param>
        /// <param name="localFolder">The full path to local folder.</param>
        public GitRepositoryManager(string username, string password, string gitRepoUrl, string localFolder)
        {
            var folder = new DirectoryInfo(localFolder);

            if (!folder.Exists)
            {
                throw new Exception(string.Format("Source folder '{0}' does not exist.", _localFolder));
            }

            _localFolder = folder;

            _credentials = new UsernamePasswordCredentials
            {
                Username = username,
                Password = password
            };

            _repoSource = gitRepoUrl;
        }

        /// <summary>
        /// Commits all changes.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <exception cref="System.Exception"></exception>
        public void CommitAllChanges(string message)
        {
            using (var repo = new Repository(_localFolder.FullName))
            {
                var files = _localFolder.GetFiles("*", SearchOption.AllDirectories).Select(f => f.FullName);
                repo.Stage(files);

                repo.Commit(message);
            }
        }

        /// <summary>
        /// Pushes all commits.
        /// </summary>
        /// <param name="remoteName">Name of the remote server.</param>
        /// <param name="branchName">Name of the remote branch.</param>
        /// <exception cref="System.Exception"></exception>
        public void PushCommits(string remoteName, string branchName)
        {
            using (var repo = new Repository(_localFolder.FullName))
            {
                var remote = repo.Network.Remotes.FirstOrDefault(r => r.Name == remoteName);
                if (remote == null)
                {
                    repo.Network.Remotes.Add(remoteName, _repoSource);
                    remote = repo.Network.Remotes.FirstOrDefault(r => r.Name == remoteName);
                }

                var options = new PushOptions
                {
                    CredentialsProvider = (url, usernameFromUrl, types) => _credentials
                };

                repo.Network.Push(remote, branchName, options);
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *