问题描述
我正在尝试用蛋糕推动项目中的变化。这是我的代码
[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 和 GitCommit 工作正常,但在 GitPush 中出现此错误:
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,CakeReport report)
at Cake.Core.CakeEngine.RunTask(ICakeContext context,String target,CakeReport report)
at Cake.Core.CakeEngine.RunTargetAsync(ICakeContext context,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)
我猜这个错误是因为我在 Azure Repo 上进行身份验证的方式存在问题,但我找不到方法或添加到工作中使用 azureRepo 查找...
解决方法
您是否尝试过手动提交和推送? 我在执行您提供的示例时没有遇到任何问题。
注意:Code: Read&Write
已设置。
然后我使用了你上面指定的代码,只是稍作修改: (密码设置为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);
}
结果如下:
❯ 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