Azure管道:我遇到了“致命错误:无法读取'https://github.com'的用户名:终端提示已禁用”的问题。

29

我在Azure构建管道中配置了PowerShell任务,用于将dev分支中的更改合并到我的GitHub公共存储库的master分支,并将更改推送到master。我遇到了以下错误:

fatal: could not read Username for 'https://github.com': terminal prompts disabled

注:

  • 我已经在gitconfig中设置了我的用户名和电子邮件地址。
  • 当我修改文件并进行提交和推送时,git push正常工作,但在合并和推送时会出现此错误。
  • 我有足够的权限将更改推送到该分支。

如有需要更多信息,请在此线程中发表评论。

这是实际代码段。

$branchName = $env:BRANCH_NAME;

Write-Host "Getting SHA for last commit in the latest release" -ForegroundColor Blue;
$latestReleaseCommitSHA = git rev-list --tags --max-count=1;

if([string]::IsNullOrEmpty($latestReleaseCommitSHA)) {
    Write-Host "Unable to get the SHA for last commit in latest release" -ForegroundColor Red;
    EXIT 1;
}

Write-Host "SHA for last commit in the latest release is '$($latestReleaseCommitSHA)'" -ForegroundColor Green;

Write-Host "Merging Changes till '$($latestReleaseCommitSHA)'" -ForegroundColor Blue;
git merge $latestReleaseCommitSHA

Write-Host "Checking Conflicted Files";
$conflictedFiles = git diff --name-only --diff-filter=U

if (-Not [string]::IsNullOrEmpty($conflictedFiles)) {
    Write-Host "Unable to Merge" -ForegroundColor Red;
    Write-Host "There are conflicts in below files:" -ForegroundColor Cyan;
    Write-Host -Object $conflictedFiles -ForegroundColor Cyan;
    EXIT 1;
}

Write-Host "Merged changes to '$($branchName)'" -ForegroundColor Green;

Write-Host "Pushing changes." -ForegroundColor Blue;
git push origin HEAD:$branchName

Write-Host "Pushed the changes to the $($branchName) branch." -ForegroundColor Green;

可能意味着在尝试推送时会提示输入某些内容,尝试在本地进行相同操作并找出如何修复它。 - 4c74356b41
尝试本地化是指在自己的计算机上进行尝试,而不是在 Azure 管道或本地代码库中进行。 - Muthurathinam
好的,考虑到这个错误,我非常确定它缺少推送的权限,否则为什么会提示输入用户名和密码。 - 4c74356b41
1
@Muthurathinam,请尝试我的下面的答案,我在合并时也遇到了这个错误,并且我已经解决了它。 - Shayki Abramczyk
1
嗨@Muthurathinam,希望你不介意我问一下:你真的需要编写合并和推送脚本吗?在Azure DevOps中,使用Pull Request通常可以更轻松地完成此类操作。 - Vince Bowdren
3个回答

47

内置的“服务帐户”实际上是一个名为Project Collection Build Service的PAT,不要与Project Collection Build Service Accounts 混淆。

来源: https://marcstan.net/blog/2018/08/31/Mirror-github-gitlab-and-VSTS-repositories/

小知识:如果您不知道"$env:SYSTEM_ACCESSTOKEN"是什么,那么它是由构建服务器自动生成的PAT(个人/私人访问令牌),默认情况下已禁用,并允许在构建和发布中对VSTS进行身份验证。要启用它,必须选择构建或发布定义中的“代理作业”,并在“其他选项”下选中“允许脚本访问OAuth令牌”复选框。

此过程分为两步:

第一步

要消除fatal: could not read username for...错误,我们需要允许脚本访问OAuth令牌。如果您正在使用最新的基于YAML的Azure Pipeline,则需要在UI中搜索“Allow scripts to access the OAuth token”选项。Microsoft的答案在此处。在您的YAML文件(azure-pipelines.yml)中添加:

steps:
- checkout: self
  persistCredentials: true

第二步

在解决了原帖作者的错误后,我无法提交,收到以下错误信息:

remote: 001f# service=git-receive-pack
remote: 0000000000aaTF401027: You need the Git 'GenericContribute' permission to perform this action. Details: identity 'Build\c21ba3ac-5ad4-de50-bc1a-12ee21de21f0', scope 'repository'.
remote: TF401027: You need the Git 'GenericContribute' permission to perform this action. Details: identity 'Build\c21ba3ac-5ad4-de50-bc1a-12ee21de21f0', scope 'repository'.
fatal: unable to access 'https://[username].visualstudio.com/[repo]/_git/[repo]/': The requested URL returned error: 403

我们还需要授予权限。从上述页面中,将用户Project Collection Build Service添加到您的代码库。

enter image description here

注意:是用户(1)而不是组(2)。

授予:

Contribute: Allow
Create Branch: Allow
Create Tag: Allow (Inherited)
Read: Allow (Inherited)

HTH


1
在此答案的基础上,如果您仍然使用旧的URL格式,则可以在此处找到相同的页面: https://[organization].visualstudio.com/DefaultCollection/[project]/_settings/repositories?repoGroup=true&_a=security - Doug
令人沮丧的是,在执行cake build脚本时,我也遇到了“您需要Git的'GenericContribute'权限”错误 - 即使我只推送了一个标签(例如git push origin refs/tags/1.0.0.0),并且允许创建标签权限!我本来希望不必给予贡献权限,但似乎这仍然是必要的。 - killercowuk
1
这是非常有用的答案。 - aDisplayName

31

虽然不完全相同,但这是唯一一个与我的情况相似的帖子,所以我认为在这里添加我的解决方案是值得的。我在托管的 Ubuntu Azure Pipeline 中遇到了这个错误,运行一个 shell 命令任务来检出、编辑和推送到 git 时发生了错误。

当尝试使用命令进行推送时,我收到了该错误:

git push

我通过更改命令来修复它:

git -c http.extraheader="AUTHORIZATION: bearer $(System.AccessToken)" push

$(System.AccessToken)是Azure Pipelines中预定义的变量: https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&viewFallbackFrom=vsts&tabs=yaml


6
除了使用System.AccessToken命令之外,还需要在作业的“附加选项”部分启用“允许脚本访问OAuth令牌”,这一点在您提供的链接中有提到。请注意不要改变原意。 - Jordan
嗯,这很奇怪。我没有使用checkout步骤,也没有设置任何关于“允许脚本访问OAuth令牌”的设置...但是我可以访问System.AccessToken变量。所以他们更新了行为?我的步骤是:
  1. UseDotNet
  2. GitVersion:设置
  3. GitVersion:执行
  4. 脚本:git tag + git -c http.extraheader... push
- Nikita Kalimov
1
@NikitaKalimov 我也可以无限制地访问 $(System.AccessToken),但是我必须进入项目设置/流水线/设置,然后禁用“保护 YAML 流水线中的存储库访问权限”,才能使此访问令牌按照描述的方式工作。请注意,根据保护 YAML 流水线中的存储库访问权限 的说明,如果您使用uses属性明确提到要使用的存储库,则可以保留此设置。 - Roland Sarrazin

4

我不知道为什么,在merge之后尝试push时,git会要求输入用户名和密码。

Azure DevOps默认禁用了提示输入凭据,导致出现错误。

您可以通过设置环境变量GIT_TERMINAL_PROMPT1来启用提示,但在构建过程中无法输入值,构建将挂起。

要解决此错误,只需在git push命令中添加用户名和密码或个人访问令牌(PAT)即可:

git push https://username:password(or PAT)@github.com/username/reponame.git 

https://...取代了origin


使用个人访问令牌代替用户名和密码可以正常工作。 - Muthurathinam
不要将密码粘贴到命令行中,可以使用系统变量来代替。请参见Darren Rogers在下面的答案 - Igor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接