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

  • Atlassian Bamboo: Handling git-Flow hotfix-branch
  • Publishing multiple packages from a Single Repository
  • Get rid of multiple bamboo build plans
  • Add validation checks for GitHub pull requests
  • In Bamboo, how do I pull a component library repository to a fixed location to avoid per-branch duplication?
  • Bamboo git checkout works too long
  • Error: unsupported URL protocol

    Thanks

  • Git error: inflate: data stream error (unknown compression method)
  • Can I send pull request from one repository to another without forking?
  • Recovering a deleted branch from a remote on Bitbucket (git)
  • Git commit and push a binary file, but don't keep history
  • Jenkins finds cannot find ssh key
  • Show both tags and branches in Bitbucket under commits view
  • 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.