azure-devops - 使用 Azure Devops 自托管构建代理避免 git clean
问题描述
我在 Azure 托管的 git 存储库中有一个 YAML 构建脚本,它在本地 VM 上运行的 7 个构建代理中触发。每次运行时,构建都会执行一次 git clean ,这需要花费大量时间,因为一个大node_modules
文件夹需要很长时间才能清理。
这里的 MSDN 页面似乎表明这是可配置的,但没有显示如何配置它的详细信息。我不知道这是应该在代理、YAML 脚本、管道上的 DevOps 中还是在哪里指定的设置。
有没有我遗漏的其他文件或者这是不可能的?
更新: YAML 文件的开头在这里:
variables:
BUILD_VERSION: 1.0.0.$(Build.BuildId)
buildConfiguration: 'Release'
process.clean: false
jobs:
###### ######################################################
###### 1 - Build and publish .NET
#############################################################
- job: net_build_publish
displayName: .NET build and publish
pool:
name: default
steps:
- script: echo $(BUILD_VERSION)
- task: DotNetCoreCLI@2
displayName: dotnet build $(buildConfiguration)
inputs:
command: 'build'
projects: |
myrepo/**/API/*.csproj
arguments: '-c $(buildConfiguration) /p:Version=$(BUILD_VERSION)'
完整的yaml要长很多,但是第一个作业的输出在Checkout
任务中包含了这个输出
Checkout myrepo@master to s
View raw log
Starting: Checkout myrepo@master to s
==============================================================================
Task : Get sources
Description : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.
Version : 1.0.0
Author : Microsoft
Help : [More Information](https://go.microsoft.com/fwlink/?LinkId=798199)
==============================================================================
Syncing repository: myrepo (Git)
Prepending Path environment variable with directory containing 'git.exe'.
git version
git version 2.26.2.windows.1
git lfs version
git-lfs/2.11.0 (GitHub; windows amd64; go 1.14.2; git 48b28d97)
git config --get remote.origin.url
git clean -ffdx
Removing myrepo/Data/Core/API/bin/
Removing myrepo/Data/Core/API/customersettings.json
Removing myrepo/Data/Core/API/obj/
Removing myrepo/Data/Core/Shared/bin/
Removing myrepo/Data/Core/Shared/obj/
....
我们还有另一项工作,它运行npm install
在npm build
一个 Angular 项目中,管道中的每个构建都需要 5 分钟来执行 npm install 步骤,可能是因为检索存储库时这个 git clean ?
解决方案
正如我在下面提到的。您需要在运行 npm install 之前计算哈希值。如果散列与保持接近的散列相同,则node_modules
可以跳过安装依赖项。这可以帮助您实现这一目标:
steps:
- task: PowerShell@2
displayName: 'Calculate and save packages.config hash'
inputs:
targetType: 'inline'
pwsh: true
script: |
# generates a hash of package-lock.json
$newHash = Get-FileHash -Algorithm MD5 -Path (Get-ChildItem package-lock.json)
$hashPath = "$(System.DefaultWorkingDirectory)/cache-npm/hash.txt"
if(Test-Path -path $hashPath) {
if(Compare-Object -ReferenceObject $(Get-Content $hashPath) -DifferenceObject $newHash) {
Write-Host "##vso[task.setvariable variable=NodeModulesAreUpToDate;]true"
$newHash > $hashPath
Write-Host ("Hash File saved to " + $hashPath)
} else {
# files are the same
Write-Host "no need to install node_modules"
}
} else {
$newHash > $hashPath
Write-Host ("Hash File saved to " + $hashPath)
}
$storedHash = Get-Content $hashPath
Write-Host $storedHash
workingDirectory: '$(System.DefaultWorkingDirectory)/cache-npm'
- script: npm install
workingDirectory: '$(Build.SourcesDirectory)/cache-npm'
condition: ne(variables['NodeModulesAreUpToDate'], true)
推荐阅读
- laravel - 如何在 Laravel 中使用原子锁?
- javascript - 如何验证 Grafana 图表并将其嵌入 iframe?
- acumatica - 预填充 PXFilter 后,如何访问用户在对话框智能面板中“按摩”的数据?
- python - PythonSpark:需要从文件列执行配置单元查询
- gridview - Yii2 Gridview URL 太长
- python - 为两个不同的按钮 Web 抓取 Python
- python - 为什么 tkinter 不向我显示图像?
- php - 为什么这个 mysql 查询执行两次或有时三次?
- cassandra-3.0 - 如何使用 QueryBuilder 创建需要更新 UDT 值的更新语句
- python - 无法获得 Python 多处理数组输出的完整副本