致命错误:无法读取“https://OrganizationName@dev.azure.com”的密码:终端提示已禁用。

25
我正在使用Azure Pipelines PowerShell任务构建时,尝试将develop分支合并到master分支。但是在执行git push命令时,我遇到了以下错误:

Fatal: Could not read password for 'https://OrganizationName@dev.azure.com': terminal prompts disabled

代码存储库是“Azure Repos Git”。
git checkout -b master
git config --global user.email "xxxxxxx@xxxx.xxx"
git config --global user.name "xxxxx"
git merge origin/develop 
git push origin master

参考了一些URL后,我创建了个人访问令牌(Personal Access Token),并将推送命令修改为git push https://PAT@dev.azure.com/OrganizationName,但它还是无法工作。

如果您找到此问题的解决方案,请告知我。


PAT是您的解决方案,"it also not worked"是什么意思?你得到了什么? - Shayki Abramczyk
出现错误:“致命错误:无法从重定向更新URL基础”。 - VKD
你需要在URL中附加团队项目和Git仓库。 - Shayki Abramczyk
7个回答

27

正如您提到的,您需要使用PAT,但是要以这种方式:

git push https://{PAT}@dev.azure.com/{organization}/{project}/_git/{repo-name}

另一个解决方案是在作业选项中“允许脚本访问OAuth令牌”:

photo

在git push中使用System.AccessToken:
git push https://$env:SYSTEM_ACCESSTOKEN@dev.azure.com/......

并授予构建用户推送权限(在存储库设置中):

enter image description here


12
针对使用 Azure Pipelines 的 .yaml 构建管道(目前是默认选项),请将 persistCredentials: true 设置为真。有关详细信息,请参见Checkout 选项 - Jan Dolejsi
Azure今天没有“contribute”权限,有什么替代权限? - JAHelia
今天我可以看到“贡献”权限。 - Shayki Abramczyk
在bash中,将_SYSTEM_ACCESSTOKEN作为环境变量传递给脚本,使用env: SYSTEM_ACCESSTOKEN: $(System.AccessToken),然后在脚本中使用它,无需$env前缀,git ls-remote --exit-code --heads https://$SYSTEM_ACCESSTOKEN@dev.azure.com/<company>/<teamProject>/_git/<repository> refs/heads/<branch>。这对我有用。 - undefined

8
将“结账”作为第一步:

steps:
- checkout: self
  persistCredentials: true

请确保您设置了Git配置

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

请确保向构建服务授予版本控制权限。

  1. 进入项目设置 --> 存储库菜单 --> 您的存储库 --> 安全选项卡,并向 项目集合构建服务 ({your organization}) 身份授予以下权限:
  • 创建分支:允许
  • 贡献:允许
  • 读取:允许
  • 创建标记:允许

现在您可以使用 git 命令而无需手动添加访问令牌到任何 git 命令中。

更多信息请参见此处:https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/git-commands?view=azure-devops&tabs=yaml


这个答案需要更多的赞!谢谢。 - G Clovs

1
# Node.js
# Build a general Node.js project with npm.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/javascript

trigger:
  - master
  - your-branch-name-here

pr: none

pool:
  vmImage: "macos-latest"

jobs:
  - job: Perform_Commit_From_CI
    steps:
      - checkout: self
        persistCredentials: true #Important - Persist creds to run further git command
        clean: true
      - task: NodeTool@0
        inputs:
          versionSpec: "16.13.2"
        displayName: "Install Node.js"
      - script: |
          git config --global user.email test@gmail.com
          git config --global user.name "Test User"
        displayName: Configure git
      - script: |
          yarn install
          yarn start NAME_OF_THE_SCRIPT_YOU_WANT_TO_EXECUTE
          git add -A
          git commit -m 'Test commit [skip ci]'
          git push origin HEAD:your-branch-name-here 
        displayName: "Test Script"

这将在没有PAT的情况下工作。


需要注意的是这个例子中提交信息中的 [skip ci] 部分。这将防止 CI 在您推送时在相同的分支上触发。 - Newteq Developer

1

Shayki的答案类似,但如果您没有运行powershell任务,请使用:

git push https://$(System.AccessToken)@dev.azure.com/......

我主要使用以下内容:

  • 经典流水线
  • 一个本地的 Windows 构建代理
    • 代理作业设置启用了允许脚本访问 OAuth 令牌
  • 命令行任务

0
使用Git凭据助手(例如Git Credential Manager(包含在Git for Windows中)或git-credential-azure(包含在多个Linux发行版中))是替代个人访问令牌的一种选择。两者都支持对Azure Repos(dev.azure.com)进行身份验证。

第一次进行身份验证时,助手会打开一个浏览器窗口以进行Microsoft登录。后续的身份验证将不需要交互。


-1

问题可能出现在您使用的 Azure 存储库是私有存储库。

将项目可见性更改为公共可解决该问题。


-6

删除 C:\program files\Git\dev\fd 然后重新安装 Microsoft git。


这与OP的问题完全无关。OP正在询问Azure DevOps。不是本地Windows安装。即使如此,删除文件并重新安装Git,这又如何解决问题呢? - Newteq Developer

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