如何传递构建服务器凭据以访问 Azure DevOps 存储库?

时间:2021-05-20 03:14:18

标签: git azure-devops azure-pipelines

我的用例是我想使用 shallow-since 创建一个浅层克隆,如 How do I remove the old history from a git repository?

中所述

但是,checkout 不提供该选项。

我的假设是做一个

- checkout: none
- bash: |
    git clone $(Build.Repository.Uri) \
      --shallow-since 2020-01-01 \
      $(Build.SourcesDirectory)

但我不确定将凭据放在哪里。我大概可以查到的变量。

我已经添加了 feature request for checkout,所以我以后不需要解决方法了。

我怀疑这与System.AccessToken

有关

2 个答案:

答案 0 :(得分:1)

您有两个选择:

  • 在克隆 URL 中注入您的 PAT

    https://{PAT}@dev.azure.com/{organization}/{project}/_git/{repo-name}
    
  • 在克隆 URL 中注入系统访问令牌:

    https://$(System.AccessToken)@dev.azure.com/{organization}/{project}/_git/{repo-name}
    

答案 1 :(得分:1)

我不使用 URL 操作,而是使用标题修改。这避免了使用 sed

steps:
  - checkout: none
  - bash: |
      git clone $(Build.Repository.Uri) \
        -c http.extraheader="Authorization: Bearer $(System.AccessToken)" \
        --single-branch \
        --no-tags \
         $(Build.SourcesDirectory)
      # --shallow-since 2020-01-01

遗憾的是,截至撰写本文时,Azure DevOps 不支持 --shallow-since