签出 Azure DevOps Pipelines (GetSources) 中分支的一部分 [英] Checkout part of a branch in Azure DevOps Pipelines (GetSources)

查看:14
本文介绍了签出 Azure DevOps Pipelines (GetSources) 中分支的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My repository in my organisation's devops project contains a lot of .net solutions and some unity projects as well. When I run my build pipeline, it fails due to several of these:

错误 MSB3491:无法将行写入文件objReleasepath ofile".磁盘空间不足.

Error MSB3491: Could not write lines to file "objReleasepath ofile". There is not enough space on the disk.

我希望管道只检出和获取成功构建所需的部分存储库.这也可能有助于缩短管道的执行时间,因为它目前还获取我的整个 Unity 项目以及数以亿计的资源,这需要永远.

I would like the pipeline to only checkout and fetch parts of the repository that are required for a successful build. This might also help with execution time of the pipeline since it currently also fetches the whole of my unity projects with gigabytes of resources which takes forever.

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

I would like to spread my projects across multiple repositories but the admin won't give me more than the one I already have. It got a lot better when I configured git fetch as shallow (--depth=1) but I still get the error every now and then.

这是我配置结帐的方式:

This is how I configured the checkout:

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

使用 VSBuild@1 任务完成构建.

The build is done using VSBuild@1 task.

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

I can't find a valid solution to my problem except for using multiple repositories, which is not an option right now.

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 是这样调用的(需要调整模板路径):

Checkout is called like this (where template path has to be adjusted):

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

推荐答案

在 Azure DevOps 中,您无法选择仅获取存储库的一部分,但有一个解决方法:禁用获取源"步骤,通过在脚本中手动执行相应的 git 命令来仅获取您想要的源.

In Azure DevOps you don't have option to get only part of the repository, but there is a workaround: Disable the "Get sources" step and get only the source you want by manually executing the according git commands in a script.

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

To disable the default "Get Sources" just specify none in the checkout statement:

- checkout: none

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

In the pipeline add a CMD/PowerShell task to get the sources manually with one of the following 2 options:

1. 使用 git 稀疏结帐.例如,仅获取 test 文件夹中的目录 src_1src_2(以 REM ### 开头的行)只是通常的批注):

1. Get only part of the repo with git sparse-checkout. For example, get only the directories src_1 and src_2 within the test folder (lines starting with REM ### are just the usual batch comments):

- 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 目录.

Now in the builds task make myRepo the working directory. Fetching the remote repo using an access token is necessary, since using checkout: none will prevent your login credentials from being used. In the end of the pipeline you may want to add step to clean the myRepo directory.

2. 使用 Azure DevOps Rest API(Git - 项目 - 批量获取项目).

2. Get parts of the repo with Azure DevOps Rest API (Git - Items - Get Items Batch).

这篇关于签出 Azure DevOps Pipelines (GetSources) 中分支的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆