git - 签出 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/
解决方案
在 Azure DevOps 中,您无法选择仅获取存储库的一部分,但有一种解决方法:禁用“获取源”步骤并通过在脚本中手动执行相应的 git 命令仅获取所需的源。
要禁用默认的“获取源”,只需none
在结帐语句中指定:
- checkout: none
在管道中添加 CMD/PowerShell 任务以使用以下 2 个选项之一手动获取源:
1.使用git sparse-checkout仅获取部分 repo 。例如,仅获取目录src_1
和文件夹src_2
内test
(以开头的行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 。
推荐阅读
- javascript - 质数检查Javascript
- reactjs - 无法在 React 中获取 event.target.value
- javascript - 如何使用显示:RWD上的块
- git - 使用子模块克隆存储库时出错,但子模块可以独立克隆
- apache-spark - 无法使用 sparksql 在 Hive 中写入数据
- java - 仅使用 Selenium 将鼠标悬停在元素上
- ios - physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) 方法在实际接触之前调用
- javascript - 在 Salesforce APEX 中生成 SAS 令牌
- python - 如何在pytest中参数化一个非确定性函数?
- ios - UIButton 没有响应点击