How to push Tag to Bitbucket Git Repository in Bamboo from Cake build task?

I’m using next code to push Tag to Git repository

#addin "Cake.Git"
using LibGit2Sharp;

var solutionFolder = "./";
var versionTag = "someTag";

Task("Default")
    .Does(() =>
    {
        var remoteRepository = EnvironmentVariable("bamboo_planRepository_repositoryUrl");
        var repositoryRevision = EnvironmentVariable("bamboo_planRepository_revision");

        var absolutePath = MakeAbsolute(Directory(solutionFolder));
        var repoName = "central";

        //LibGit2Sharp add remote  
        using (var repo = new Repository(absolutePath.FullPath))
        {
            repo.Network.Remotes.Add(repoName, remoteRepository);
        }

        GitTag(solutionFolder, versionTag, repositoryRevision);
        GitPushRef(solutionFolder, gitUser, gitPassword, repoName, versionTag); 
    }
});

Stuck into the next issue: because our bamboo configured to use SSH protocol, and Cake.Git(LibGit2Sharp) currently doesn’t support it receiving next error

  • Build Bamboo from Bitbucket, Deploy to Azure Cloud
  • How to get atlassian bamboo to trigger a build automatically from a post commit hook
  • Putting version file into EAR or WAR using Maven
  • TFS vs. JIRA/Bamboo/SVN
  • Reverting the commit , if build fail in jenkins or bamboo
  • Git commit user is different than git push user
  • Error: unsupported URL protocol

    Thanks

  • Cloning a Git repo into a new repo - without commit history
  • git can't remember password
  • Bitbucket SSH Key loading but not working
  • Git command posts my entire desktop to repo
  • How to delete an old commit completely on bitbucket
  • How to create pull request for wiki of a repository on Bitbucket?
  • 2 Solutions collect form web for “How to push Tag to Bitbucket Git Repository in Bamboo from Cake build task?”

    A little bit hacky but it works, will update answer when will find better approach.

    Done based on the How to tag a git repo in a bamboo build.

    Cake.Git currently doesn’t support adding repository but under the hood using LibGit2Sharp so just added LibGit2Sharp namespace to the code.

    Core issue is that Cake.Git(LibGit2Sharp) doesn’t support SSH yet (Issue on GitHub Is it possible to use Cake.Git with SSH), as workaraound calling git push through cmd How to execute cmd
    command

    #addin "Cake.Git"
    using LibGit2Sharp;
    
    var solutionFolder = "./";
    var versionTag = "someTag";
    var remoteRepository = EnvironmentVariable("bamboo_planRepository_repositoryUrl");
    var repositoryRevision = EnvironmentVariable("bamboo_planRepository_revision");
    
    Task("Default")
        .Does(() =>
        {
            var absolutePath = MakeAbsolute(Directory(solutionFolder));
            var repoName = "central";
    
            //LibGit2Sharp add remote  
            using (var repo = new Repository(absolutePath.FullPath))
            {
                repo.Network.Remotes.Add(repoName, remoteRepository);
            }
    
            GitTag(solutionFolder, versionTag, repositoryRevision);
            ExecuteCommand($"git push {repoName} {versionTag}");
        }
    });
    
     void ExecuteCommand(string command)
     {
         Information(command);
         int exitCode;
         System.Diagnostics.ProcessStartInfo processInfo;
         System.Diagnostics.Process process;
    
         processInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe", "/c " + command);
         processInfo.CreateNoWindow = true;
         processInfo.UseShellExecute = false;
         // *** Redirect the output ***
         processInfo.RedirectStandardError = true;
         processInfo.RedirectStandardOutput = true;
    
         process = System.Diagnostics.Process.Start(processInfo);
         process.OutputDataReceived += (object sender, System.Diagnostics.DataReceivedEventArgs e) =>
                Information("output>>" + e.Data);
         process.BeginOutputReadLine();
    
         process.ErrorDataReceived += (object sender, System.Diagnostics.DataReceivedEventArgs e) =>
                Information("error>>" + e.Data);
         process.BeginErrorReadLine();
         process.WaitForExit();
    
         Information($"ExitCode: {process.ExitCode}");
         process.Close();
      }
    

    I would suspect the issue is to due using shallow clones, which are enabled by default.

    Shallow clones allows Bamboo to perform clones with i.e. history truncated to a specified number of revisions.
    This should increase the speed of the initial code checkouts, however if your build depends on the full repository history, we recommend that you do not use this option.

    GIT operations in general need the full repo to work reliably.

    Git Baby is a git and github fan, let's start git clone.