【问题标题】:How to authenticate git push to GitHub within an Azure Pipeline Bash task如何在 Azure Pipeline Bash 任务中验证 git push 到 GitHub
【发布时间】:2019-07-01 22:31:07
【问题描述】:

在我们的一个 Azure Pipelines 中,我尝试将标签推送到 GitHub,但根据我的方法收到两个错误之一。

没有认证,只是一个简单的git push

- bash: 'git push origin $(tag)'
git push origin 2019.07.01.1
fatal: could not read Username for 'https://github.com': terminal prompts disabled

AUTHORIZATION: Bearer *** 作为额外的标头传递

- bash: 'git -c http.extraheader="AUTHORIZATION: Bearer $(System.AccessToken)" push origin $(tag)'
git -c http.extraheader="AUTHORIZATION: Bearer ***" push origin 2019.07.01.2
fatal: unable to access 'https://github.com/respondentinc/website/': The requested URL returned error: 400

使用环境变量将AUTHORIZATION: Bearer *** 作为额外的标头传递

发件人:https://github.com/microsoft/azure-pipelines-agent/issues/1582#issuecomment-392235276

- bash: 'git -c http.extraheader="AUTHORIZATION: Bearer $env:token" push origin $(tag)'
  env:
      token: $(System.AccessToken)
git -c http.extraheader="AUTHORIZATION: Bearer ***" push origin 2019.07.01.3
fatal: unable to access 'https://github.com/respondentinc/website/': The requested URL returned error: 400

将 Bash 脚本传递给凭证助手

基于bk2204的回答

- bash: 'git -c credential.helper=''!f() { echo "username=token"; echo "password=$(System.AccessToken)"; }; f'' push origin $(tag)'
git -c credential.helper='!f() { echo "username=token"; echo "password=***"; }; f' push origin 2019.07.02.9
fatal: unable to access 'https://github.com/respondentinc/website/': The requested URL returned error: 400

使用环境变量将 Bash 脚本传递给凭证助手

来自bk2204的回答

- bash: 'git -c credential.helper=''!f() { echo "username=token"; echo "password=$env:token"; }; f'' push origin $(tag)'
  env:
      token: $(System.AccessToken)
git -c credential.helper='!f() { echo "username=token"; echo "password=$env:token"; }; f' push origin 2019.07.02.9
fatal: unable to access 'https://github.com/respondentinc/website/': The requested URL returned error: 400

AUTHORIZATION: Basic *** 作为额外的标头传递

试图模仿默认管道“结帐”任务的方式。

- bash: 'git -c http.extraheader="AUTHORIZATION: basic $(System.AccessToken)" push origin $(tag)'
git -c http.extraheader="AUTHORIZATION: basic ***" push origin 2019.07.02.10
fatal: unable to access 'https://github.com/respondentinc/website/': The requested URL returned error: 400

我在上述主题上尝试了一些变体,但都没有成功。

我错过了什么?

更新:2019-07-02

上述所有机制都是有效的身份验证方式。问题是 System.AccessToken 对 GitHub 无效。 documentation 表示它仅用于与 Azure Pipeline 服务交互

我找不到Azure Pipeline GitHub App 提供的令牌的变量。我认为这并不重要,因为它是installation token。我尝试将此令牌与 GitHubRelease 任务一起使用,但由于缺少写入权限,它仍然失败。

为了完成这项工作,我执行了 GitHubRelease 任务,并且必须使用具有写入权限的新 OAuth 令牌创建新服务连接。

我仍然很好奇如何从 Bash 任务中访问与服务连接关联的令牌。

【问题讨论】:

  • 我也有同样的问题。你找到解决办法了吗?
  • @Maxim 我使用了 GitHubRelease 任务并专门为此工作创建了一个 OAuth 令牌(在 GitHub 内)。不理想。我计划很快迁移到 GitHub Actions。
  • 我已添加我的解决方案作为答案。

标签: git azure-pipelines


【解决方案1】:

我的解决办法是:

  1. 在 GitHub 上创建个人访问令牌 (PAT)

  2. 在您的管道中创建将保存 PAT 的秘密变量

  3. 现在您可以在管道 YAML 配置中为 git push 使用 PAT:

    git push https://$(GitHubPAT)@github.com/<your_org_and_project>.git

您似乎还需要在push 之前执行这些步骤(我正在使用 PowerShell 脚本步骤):

Add-Content "$HOME\.git-credentials" "https://$(GitHubPAT):x-oauth-basic@github.com"
git config --global user.email "<email>"
git config --global user.name "<name>"
git config --global --add url."git@github.com:".insteadOf "https://github.com/"

但我不确定这些说明中的哪些可以省略。但是按照这些步骤,我可以从我的管道中执行git push

【讨论】:

【解决方案2】:

这里的问题是您正在尝试使用 Bearer 身份验证。这对于 Azure DevOps 托管是正确的,但对于使用基本身份验证的 GitHub 则不正确。

处理此问题的最佳方法是将环境变量TOKEN 设置为具有您想要的令牌(如您的尝试),然后按如下方式运行命令:

git -c credential.helper='!f() { echo "username=token"; echo "password=$TOKEN"; };f' push $(tag)

如果您使用的是令牌,任何用户名都是可以接受的;我只是使用令牌,因为您需要 一些 用户名,这是一个显而易见的选择。

通过使用 shell 函数作为凭据帮助器并使用内置 shell 的 echo,您的凭据将不会出现在日志或进程输出中。

【讨论】:

  • 我试了一下,但我仍然收到fatal: unable to access 'https://github.com/respondentinc/website/': The requested URL returned error: 400。让我相信这不是身份验证问题,而是某种范围/环境问题。此外,我已经更新了我的问题以包含此方法。
猜你喜欢
  • 2022-01-02
  • 1970-01-01
  • 2019-03-30
  • 2020-07-27
  • 2010-10-31
  • 1970-01-01
  • 2020-09-18
  • 2020-06-11
  • 2021-11-04
相关资源
最近更新 更多