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

  • TFS vs. JIRA/Bamboo/SVN
  • Bamboo build stuck on “Waiting to be built…” and Cloning into 'Common'
  • How can I check out only a part of a git repository during a bamboo build?
  • Bamboo git checkout works too long
  • Build Bamboo from Bitbucket, Deploy to Azure Cloud
  • Maven Release-plugin “tag already exists” for nonexistant tag
  • Error: unsupported URL protocol

    Thanks

  • Why can't I specify another repository for pull request in BitBucket?
  • GIT rebase requires to recommit changes
  • Connect Nitrous.io to Bitbucket Git SSH Public Key
  • jenkins hook not working - jenkins bitbucket
  • Merge-only Branch on BitBucket/GitLab/GitHub?
  • How do I manage users 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.