如何在 Azure Pipeline Bash 任务中验证 git push 到 GitHub

Exi*_*ide 6 git azure-pipelines

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

无需身份验证,只是一个简单的 git push

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

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

- bash: 'git -c http.extraheader="AUTHORIZATION: Bearer $(System.AccessToken)" push origin $(tag)'
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)

将 Bash 脚本传递给凭证助手

基于bk2204的回答

- bash: 'git -c credential.helper=''!f() { echo "username=token"; echo "password=$(System.AccessToken)"; }; f'' push origin $(tag)'
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)

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

来自bk2204的回答

- bash: 'git -c credential.helper=''!f() { echo "username=token"; echo "password=$env:token"; }; f'' push origin $(tag)'
  env:
      token: $(System.AccessToken)
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)

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

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

- bash: 'git -c http.extraheader="AUTHORIZATION: basic $(System.AccessToken)" push origin $(tag)'
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)

我尝试了上述主题的一些变体,但没有运气。

我错过了什么?

更新:2019-07-02

以上所有机制都是有效的认证方式。问题是System.AccessToken对 GitHub 无效。该文件表明它是与Azure的管道服务的交互

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

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

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

bk2*_*204 0

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

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

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

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

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