You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure DevOps使用服务主体克隆Git子模块失败问题求助

解决Azure DevOps Git子模块服务主体认证失败问题

问题重现

原使用PAT令牌完成Azure DevOps Git子模块认证,切换为服务主体认证后,已成功获取Bearer令牌,但执行git submodule update --init --recursive时触发错误:

fatal: could not read Password for 'https://xxx@dev.azure.com': terminal prompts disabled

服务主体已添加为Azure DevOps用户,并赋予目标仓库读取权限,执行脚本见原提问。

核心原因

  1. 令牌Scope错误:请求令牌时使用了Graph API的https://graph.microsoft.com/.default,但Azure DevOps Git认证需要的是Azure DevOps专属的API Scope。
  2. Git配置匹配范围不足:针对单个仓库的http.https://dev.azure.com/conso/DevOps/_git/terraform.extraheader配置,无法覆盖子模块的远程URL(若子模块URL与主仓库路径不一致),导致Git未携带认证头,触发密码提示。

修正方案

调整脚本中的令牌请求Scope,并放宽Git认证头的匹配范围,确保所有Azure DevOps仓库请求都携带认证信息:

- task: Bash@3
  displayName: Fetch Submodules
  inputs:
    targetType: 'inline'
    script: |
      # 获取Azure DevOps专用Bearer令牌
      creds=$(curl -X POST -H "Content-Type: application/x-www-form-urlencoded" \
        -d "client_id=$client_id&scope=https%3A%2F%2Fdev.azure.com%2F.default&client_secret=$client_secret&grant_type=client_credentials" \
        https://login.microsoftonline.com/$tenant_id/oauth2/v2.0/token)
      access_token=$(echo "$creds" | jq -r .access_token)
      
      # 全局配置所有Azure DevOps仓库的认证头
      git config --global http.https://dev.azure.com/.extraheader "AUTHORIZATION: Bearer $access_token"
      
      # 无交互模式更新子模块
      git submodule update --init --recursive --no-tags
  env:
    client_id: xxx
    client_secret: "xxxx"
    tenant_id: xxx

额外验证步骤

  • 确认服务主体权限:在Azure DevOps项目设置→权限→用户中,确保该服务主体对主仓库及所有子模块仓库均拥有「读取」权限(跨项目子模块需单独授权)。
  • 检查子模块URL:确保.gitmodules文件中子模块的远程URL为https://dev.azure.com/<组织>/<项目>/_git/<仓库>格式,未使用SSH或自定义路径。

内容的提问来源于stack exchange,提问作者Michele

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 19:52:40