0

Im trying pushing changes in a project with cake. This is my code

[TaskName("GitPush")]
public sealed class GitPushTask : FrostingTask<BuildContext>
{
    public override void Run(BuildContext context)
    {
        context.GitAddAll(context.WorkDirectoryPath);
        context.GitCommit(context.WorkDirectoryPath, "user surname", "usersurname@hellow.com", "Testing Commit");
        context.GitPush(context.WorkDirectoryPath, context.GitUsername, context.GitPassword);
    }
}

GitAddAll and GitCommit work fine, but in the GitPush I get this error:

An error occurred when executing task 'GitPush'.
Error: LibGit2Sharp.LibGit2SharpException: No error message has been provided by the native library
   at LibGit2Sharp.Core.Ensure.HandleError(Int32 result)
   at LibGit2Sharp.Core.Ensure.ZeroResult(Int32 result)
   at LibGit2Sharp.Core.Proxy.git_remote_push(RemoteHandle remote, IEnumerable`1 refSpecs, GitPushOptions opts)
   at LibGit2Sharp.Network.Push(Remote remote, IEnumerable`1 pushRefSpecs, PushOptions pushOptions)
   at LibGit2Sharp.Network.Push(Remote remote, String pushRefSpec, PushOptions pushOptions)
   at LibGit2Sharp.Network.Push(IEnumerable`1 branches, PushOptions pushOptions)
   at Cake.Git.GitAliases.<>c__DisplayClass32_0.<GitPush>b__0(Repository repository)
   at Cake.Git.Extensions.RepositoryExtensions.UseRepository(ICakeContext context, DirectoryPath repositoryPath, Action`1 repositoryAction)
   at Cake.Git.GitAliases.GitPush(ICakeContext context, DirectoryPath repositoryDirectoryPath, String username, String password)
   at GitPushTask.Run(BuildContext context) in C:\Users\jmedingr\Desktop\MonEES.CICD.Cake\cake\Program.cs:line 128
   at Cake.Frosting.FrostingTask`1.Cake.Frosting.IFrostingTask.RunAsync(ICakeContext context)
   at Cake.Core.CakeTask.Execute(ICakeContext context)
   at Cake.Core.DefaultExecutionStrategy.ExecuteAsync(CakeTask task, ICakeContext context)
   at Cake.Core.CakeEngine.ExecuteTaskAsync(ICakeContext context, IExecutionStrategy strategy, Stopwatch stopWatch, CakeTask task, CakeReport report)
   at Cake.Core.CakeEngine.ExecuteTaskAsync(ICakeContext context, IExecutionStrategy strategy, Stopwatch stopWatch, CakeTask task, CakeReport report)
   at Cake.Core.CakeEngine.ExecuteTaskAsync(ICakeContext context, IExecutionStrategy strategy, Stopwatch stopWatch, CakeTask task, CakeReport report)
   at Cake.Core.CakeEngine.RunTask(ICakeContext context, IExecutionStrategy strategy, CakeTask task, String target, Stopwatch stopWatch, CakeReport report)
   at Cake.Core.CakeEngine.RunTargetAsync(ICakeContext context, IExecutionStrategy strategy, ExecutionSettings settings)
   at Cake.Cli.BuildScriptHost`1.RunTargetAsync(String target)
   at Cake.Core.Scripting.ScriptHost.RunTarget(String target)
   at Cake.Frosting.Internal.FrostingEngine`1.Run(String target)
   at Cake.Frosting.Internal.DefaultCommand.Execute(CommandContext context, DefaultCommandSettings settings)

I guess the error is because problems in the way I authenticated on Azure Repo, but I cant find a methods or adding to work find with azureRepo...

1 Answers1

0

Have you tried committing and pushing manually? I had no problems executing the example you gave.

I created a PAT like this: enter image description here

notice: Code: Read&Write is set.

Then I used the code you specified above only sightly modified: (Username is not used when the password is set to a PAT)

public override void Run(BuildContext context)
{
    var pat = context.EnvironmentVariable("MYPAT");
    if (string.IsNullOrEmpty(pat))
    {
        throw new Exception("MYPAT must be set!");
    }
    
    context.GitAddAll(context.WorkDirectoryPath);
    context.GitCommit(context.WorkDirectoryPath, "Nils", "nils@hellow.com", "Testing Commit");
    context.GitPush(context.WorkDirectoryPath, "unused when password is a PAT", pat);
}

The result then looks like:

❯ git log
commit 5979345845c7d3d7c6387ea7aff74a1de3ae366c (HEAD -> main, origin/main, origin/HEAD)
Author: Nils <nils@hellow.com>
Date:   Mon May 24 22:03:12 2021 +0200

    Testing Commit

Nils
  • 9,682
  • 6
  • 46
  • 72
  • Ok, I put my PAT on the enviroment variables, and I check that have all the permissions needed. However, I still recive the same error... Y try to put the Pat encode on Base64 on the enviroment variables with the same result... I still trying to use the Pat manually to check if work, but I still dont find the way... A lot of thanks for your help Nils!!! – José Pablo Medina Grande May 25 '21 at 07:49
  • well, what happens if you simply `git push`? I get a dialog asking for username/password. If I enter the PAT (not encoded or anything) pushing works. You did use the `https` checkout not `ssh`, right? – Nils May 25 '21 at 07:59
  • If I set git push, the pushing works without dialog asking. And not, im not using ssh – José Pablo Medina Grande May 25 '21 at 14:22
  • 1
    Sounds like you have credential manager enabled. Not sure if it can be disabled on a per-checkout basis. You could have a look at https://stackoverflow.com/questions/37182847/how-do-i-disable-git-credential-manager-for-windows – Nils May 26 '21 at 07:27