首页 > 解决方案 > 签出 Azure DevOps Pipelines (GetSources) 中分支的一部分

问题描述

我组织的 devops 项目中的存储库包含许多 .net 解决方案和一些统一项目。当我运行构建管道时,由于以下几个原因而失败:

错误 MSB3491:无法将行写入文件“obj\Release\path\to\file”。磁盘空间不足。

我希望管道仅签出和获取成功构建所需的存储库部分。这也可能有助于管道的执行时间,因为它目前还使用千兆字节的资源获取我的整个统一项目,这需要永远。

我想将我的项目分布在多个存储库中,但管理员不会给我超过我已经拥有的一个。当我将 git fetch 配置为 shallow ( ) 时,它变得更好了,--depth=1但我仍然时不时地收到错误。

这就是我配置结帐的方式:

steps:
- checkout: self
  clean: true
  # shallow fetch
  fetchDepth: 1
  lfs: false
  submodules: false

构建是使用VSBuild@1任务完成的。

除了使用多个存储库之外,我找不到解决我的问题的有效解决方案,目前这不是一个选项。

编辑:Shayki Abramczyk 的解决方案 #1 完美运行。这是我的完整实现。

GitSparseCheckout.yml

parameters:
  access: ''
  repository: ''
  sourcePath: ''

steps:
- checkout: none

- task: CmdLine@2
  inputs:
    script: |
      ECHO ##[command] git init
      git init
      ECHO ##[command] git sparse-checkout: ${{ parameters.sourcePath }}
      git config core.sparsecheckout true
      echo ${{ parameters.sourcePath }} >> .git/info/sparse-checkout
      ECHO ##[command] git remote add origin https://${{ parameters.repository }}
      git remote add origin https://${{ parameters.access }}@${{ parameters.repository }}
      ECHO ##[command] git fetch --progress --verbose --depth=1 origin master
      git fetch --progress --verbose --depth=1 origin master
      ECHO ##[command] git pull --progress --verbose origin master
      git pull --progress --verbose origin master

Checkout 是这样调用的(必须调整模板路径):

- template: ../steps/GitSparseCheckout.yml
  parameters:
    access: anything:<YOUR_PERSONAL_ACCESS_TOKEN>
    repository: dev.azure.com/organisation/project/_git/repository
    sourcePath: path/to/files/

标签: gitazure-devopsazure-pipelines

解决方案


在 Azure DevOps 中,您无法选择仅获取存储库的一部分,但有一种解决方法:禁用“获取源”步骤并通过在脚本中手动执行相应的 git 命令仅获取所需的源。

要禁用默认的“获取源”,只需none在结帐语句中指定:

- checkout: none

在管道中添加 CMD/PowerShell 任务以使用以下 2 个选项之一手动获取源:

1.使用git sparse-checkout仅获取部分 repo 。例如,仅获取目录src_1和文件夹src_2test(以开头的行REM ###只是通常的批处理注释):

- script: |
    REM ### this will create a 'root' directory for your repo and cd into it
    mkdir myRepo
    cd myRepo
    REM ### initialize Git in the current directory
    git init
    REM ### set Git sparsecheckout to TRUE
    git config core.sparsecheckout true
    REM ### write the directories that you want to pull to the .git/info/sparse-checkout file (without the root directory)
    REM ### you can add multiple directories with multiple lines
    echo test/src_1/ >> .git/info/sparse-checkout
    echo test/src_2/ >> .git/info/sparse-checkout
    REM ### fetch the remote repo using your access token
    git remote add -f origin https://your.access.token@path.to.your/repo
    REM ### pull the files from the source branch of this build, using the build-in Azure DevOps variable for the branch name
    git pull origin $(Build.SourceBranch)
    displayName: 'Get only test/src_1 & test/src_2 directories'

现在在构建任务myRepo中创建工作目录。使用访问令牌获取远程仓库是必要的,因为使用checkout: none会阻止您的登录凭据被使用。在管道的末尾,您可能需要添加步骤来清理myRepo目录。

2.使用Azure DevOps Rest API (Git - Items - Get Items Batch)获取部分 repo 。


推荐阅读