在蔚蓝的DevOps发布管道中执行“ git pull”会导致错误->致命:无法读取

时间:2020-09-11 09:48:59

标签: git deployment azure-devops azure-pipelines-release-pipeline

问题

我试图通过执行Powershell脚本通过Azure Devops发布管道部署代码,该脚本签出master分支并拉入我们的生产服务器:

cd "C:\..."
git checkout master
git pull 

这在我尝试执行git pull时产生错误:

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

当我从服务器本身执行脚本时,脚本可以正常工作。只有当我使用发布管道时,它才会失败。

问题排查

•该错误消息使我相信执行git pull时未设置用户名,但是如果我在代码中添加以下两行

git config user.name 
git config user.email

我看到正确的用户名和电子邮件地址被打印。

•另一个猜测是,它试图从错误的来源中提取信息,因为“ https://acino.visualstudio.com”不是我的存储库的URL。所以我也尝试过将存储库名称附加到git pull

git pull https://URL to my repository
with no luck.

因此,我不太真正地了解问题所在。欢迎任何输入,评论,建议。

仅供参考:我知道在发布管道中执行git pull可能不是部署代码的标准方法。我还构建了其他更有效的常规部署管道。但是,由于使用git pull的目录/项目结构是我所看到的最佳解决方案。

亲切的问候, Rok Bohinc

2 个答案:

答案 0 :(得分:1)

您可以使用以下格式的git pull命令:

git pull {URL} {remote branch}:{local branch}

并将PAT插入URL将提供解决登录问题的凭据。您可以手动创建新的PAT或使用System.AccessToken(推荐),我这边的最终格式为:

git pull https://$env:SYSTEM_ACCESSTOKEN@dev.azure.com/{OrgName}/{ProjectName}/_git/{RepoName} master:master

git pull https://{PAT}@dev.azure.com/{OrgName}/{ProjectName}/_git/{RepoName} master:master

看起来您的项目仍然是旧格式xxx.visualstudio.com/...,因此您的最终格式将类似于:

git pull https://PAT:{PAT}@xxx.visualstudio.com/{ProjectName}/_git/{RepoName} master:master

git pull https://$env:SYSTEM_ACCESSTOKEN@xxx.visualstudio.com/{ProjectName}/_git/{RepoName} master:master

使用PAT或URL中的System.AccessToken,您无需配置用户名和密码。请记住,我们应该启用Allow scripts to access the OAuth token,以便我们可以访问System.AccessToken

答案 1 :(得分:0)

请尝试在here的git config中使用System.AccessToken

$mods = (git submodule status) | % { ($_.Trim() -split " ")[1] }
$baserepo = ($env:BUILD_REPOSITORY_URI).TrimEnd($env:BUILD_REPOSITORY_NAME)
foreach($mod in $mods)
{
cd $mod
$cmd = 'git config http.' + $baserepo + $mod + '.extraheader "AUTHORIZATION: bearer ' + $env:System_AccessToken + '"'
write $cmd
iex $cmd
cd ..
}

enter image description here

您也可以认为this topic有用。