-->

How to push Tag to Bitbucket Git Repository in Bam

2019-06-07 19:13发布

问题:

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

Error: unsupported URL protocol

Thanks

回答1:

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.



回答2:

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);
            Cmd($"git push {repoName} {versionTag}");
        }
    });

private void Cmd(params object[] parameters)
{
    if (parameters.Any())
    {
        var args =  new ProcessArgumentBuilder()
            .Append(@"/c");

        foreach (var param in parameters)
            args.Append($"{param}");

        StartProcess("cmd", new ProcessSettings { Arguments = args });
    }
}