首页 > 解决方案 > 使用 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 installnpm build一个 Angular 项目中,管道中的每个构建都需要 5 分钟来执行 npm install 步骤,可能是因为检索存储库时这个 git clean ?

标签: azure-devopsazure-devops-self-hosted-agentazure-devops-pipelines

解决方案


正如我在下面提到的。您需要在运行 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)

推荐阅读