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

左心房为你撑大大i 提交于 2020-01-03 02:58:05

问题


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 });
    }
}


来源:https://stackoverflow.com/questions/44185169/how-to-push-tag-to-bitbucket-git-repository-in-bamboo-from-cake-build-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!