【问题标题】:Push commit to Azure Repo using Cake Build (C# Make)使用 Cake Build (C# Make) 将提交推送到 Azure Repo
【发布时间】:2021-05-24 12:00:00
【问题描述】:

我正在尝试用蛋糕推动项目的变化。这是我的代码

[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, 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)

我猜这个错误是因为我在 Azure Repo 上进行身份验证的方式存在问题,但我找不到方法或添加到 azureRepo 的工作查找...

【问题讨论】:

  • 是否添加了提交?您是否使用正确密码的访问令牌?是远程 https 还是 ssh?
  • 好的,在 GitAddAll 和 GitCommit 上添加了提交。我正在使用用户名和密码,只是……我不确定在 Azure Repos 上配置访问令牌。
  • 您需要将 PAT 与 Azure DevOps 一起使用,它不会接受密码。
  • 谢谢回答,devlead...我如何使用 PAT?
  • 你用它代替密码

标签: cakebuild azure-repos


【解决方案1】:

您是否尝试过手动提交和推送? 我执行你给出的例子没有问题。

我创建了一个这样的 PAT:

注意:Code: Read&amp;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

【讨论】:

  • 好的,我将我的 PAT 放在环境变量上,并检查它是否具有所需的所有权限。但是,我仍然收到相同的错误...Y尝试将Pat编码放在Base64上的环境变量上,结果相同...我仍然尝试手动使用Pat来检查是否有效,但我仍然找不到方式...非常感谢尼尔斯的帮助!!!
  • 好吧,如果你只是 git push 会发生什么?我收到一个对话框,询问用户名/密码。如果我输入 PAT(未编码或其他任何内容),则推送有效。您确实使用了https 结帐而不是ssh,对吗?
  • 如果我设置了 git push,那么推送就可以在没有对话框询问的情况下进行。不是,我没有使用 ssh
  • 听起来您启用了凭据管理器。不确定是否可以在每次结帐时禁用它。你可以看看stackoverflow.com/questions/37182847/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-01
  • 2021-02-09
  • 2016-06-03
  • 1970-01-01
相关资源
最近更新 更多