0

我在不同的存储库中有一个主要解决方案和 N 个其他解决方案。每次构建主解决方案时,我都必须构建 N 个其他解决方案并创建一个包含其他解决方案的主解决方案。我当前的构建管道看起来像这样。

  • [阶段1]

    • [工作1]构建主解决方案->发布管道工件(其他解决方案需要)[代理文件夹1]

    • [作业 2..N] 结帐解决方案“n”-> 构建 -> 发布管道工件 [代理文件夹 2..N]

  • [第 2 阶段] - 在所有作业成功后
    • [作业1] - 下载主工件-> 下载其他工件-> 将它们放在一个工件中-> 发布[代理文件夹N + 1]

我的问题是,下次运行此管道时,我的代理会回收最后一个文件夹,而其他文件夹会占用大量内存。有没有办法告诉管道在同一个代理工作目录中运行每个作业?或者重用创建的第一个目录(用于构建主要解决方案的目录)?

编辑:这是我的管道代码

resources:
  repositories:
  - repository: Repo.With.Templates
    type: git
    name: Repo.With.Templates
    ref: feature/templates

  - repository: Secondary.Repository.1
    type: git
    name: Secondary.Repository.1
    ref: f/refactor/cd

  - repository: Secondary.Repository.2
    type: git
    name: Secondary.Repository.2
    ref: f/refactor/cd

trigger:
  batch: true
  branches:
    include:
    - 'f/core/cd-updates'

pool: 'Example pool'

variables:
 solution: '**/*.sln'
 buildPlatform: 'Any CPU'
 buildConfiguration: 'Release'
 scriptSetBuildNumber: 'CI\SetBuildNumber.ps1'
 nugetConfigFile: '$(Build.SourcesDirectory)/NuGet.config'
 checkoutFolderRoot: '$(Build.SourcesDirectory)'

stages:
- stage: BuildingStage
  jobs:
  - job: Main_Solution_Build
    steps:
    - task: VSBuild@1
      displayName: 'Build'
      inputs:
        solution: '$(solution)'
        msbuildArgs: '
            /p:DefineConstants="TESTENV"
            /p:TreatWarningsAsErrors=true
            /p:DeployDefaultTarget=WebPublish
            /p:WebPublishMethod=FileSystem
            /p:DeleteExistingFiles=True
            /p:SkipInvalidConfigurations=false
            /p:VisualStudioVersion=11.0
            /p:publishUrl="$(Build.ArtifactStagingDirectory)\"
            '
        platform: '$(buildPlatform)'
        configuration: '$(buildConfiguration)'

    # Zip bin folder and move to artifact folder
    - task: ArchiveFiles@2
      name: 'MovingMainSolutionBinToArtifacts'
      inputs:
        rootFolderOrFile: '$(Build.SourcesDirectory)/MainSolution/bin'
        includeRootFolder: true
        archiveType: 'zip'
        archiveFile: '$(Build.ArtifactStagingDirectory)/Artifacts/MainSolution.zip'
        replaceExistingArchive: true

    - task: PublishPipelineArtifact@1
      name: PublishMainSolutionBinPipeArtifact
      inputs:
        targetPath: '$(Build.ArtifactStagingDirectory)\Artifacts\MainSolution.zip'
        artifact: 'MainSolutionBinDrop'
        publishLocation: 'pipeline'

  - job: SecondarySolution1
    dependsOn: Main_Solution_Build
    steps:
    - checkout: Secondary.Repository
    - template: build-service-template.yml@Repo.With.Templates
      parameters:
        serviceName: "SecondarySolution1"


## Stage for assembling MainSolution and all the services together
- stage: Assemble
  dependsOn: BuildingStage
  jobs:
  - job: AssembleArtifact
    steps:
    - checkout: none

    - task: DownloadPipelineArtifact@2
      name: "MainSolutionBinDrop"
      inputs:
        buildType: 'specific'
        project: 'a12e0163-b207-400d-ac93-fa47964d5010'
        definition: '2'
        buildVersionToDownload: 'latest'
        artifactName: 'MainSolutionBinDrop'
        targetPath: '$(Build.ArtifactStagingDirectory)/MainSolutionBinDrop'

    - task: DownloadBuildArtifacts@0
      name: "DownloadServicesDrop"
      inputs:
        buildType: 'specific'
        project: 'a12e0163-b207-400d-ac93-fa47964d5010'
        pipeline: '2'
        buildVersionToDownload: 'latest'
        downloadType: 'single'
        artifactName: 'ServicesDrop'
        downloadPath: '$(Build.ArtifactStagingDirectory)'

    # Tasks that produce one artifact out of Main artifact and all Secondary solution artifacts
    # .....
    # .....

      # Publish
    - task: PublishPipelineArtifact@1
      inputs:
        targetPath: '$(Build.ArtifactStagingDirectory)/MainWithSecondary.zip'
        artifact: 'MainWithSecondary'
        publishLocation: 'pipeline'

以及用于构建N个二级解决方案的模板代码(本例中只有一个)

parameters:
- name: solution
  type: string
  default: '**/*.sln'

- name: buildPlatform
  type: string
  default: 'Any CPU'

- name: buildConfiguration
  type: string
  default: 'Release'

- name: nugetConfigFile
  type: string 
  default: '$(Build.SourcesDirectory)/NuGet.config'

- name: serviceName
  type: string

steps:

# Tasks that download main build artifact, build secondary solution and publishes secondary artifact
#
#

#Download main solution artifact
- task: DownloadPipelineArtifact@2
  inputs:
    buildType: 'current'
    artifactName: 'MainSolutionDrop'
    targetPath: '$(Agent.BuildDirectory)/MainSolution'

- task: VSBuild@1
  displayName: 'Build'
  inputs:
    solution: '$(solution)'
    msbuildArgs: '
    /p:TreatWarningsAsErrors=true 
    /p:DeployOnBuild=true
    /p:SkipInvalidConfigurations=false'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

# Create artifact folder
- task: PowerShell@2
  name: "CreateArtifactFolder"
  inputs:
    targetType: 'inline'
    script: |
      if (!(Test-Path "$(Build.ArtifactStagingDirectory)\${{ parameters.serviceName }}" -PathType Container)) {    
                New-Item -ItemType Directory -Path "$(Build.ArtifactStagingDirectory)\${{ parameters.serviceName }}"
      }


- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'ServicesDrop'
    publishLocation: 'Container'
4

1 回答 1

1

每个作业都有自己的默认工作目录,并且与其他作业分开,它是自动设置的代理,不能更改。因此,您不能在同一代理工作目录中运行每个作业。

有一种解决方法可以在同一个工作文件夹下下载多个存储库并在一个作业中构建它们。

您可以在 powershell 任务中运行git 命令来克隆同一工作目录中的多个 repos。然后将构建任务指向每个 repo 文件夹以构建每个解决方案。检查以下示例:

您将需要使用 PAT 进行验证。选中此处以生成具有代码读写范围的 PAT

- powershell: |
    git clone https://{PAT}@dev.azure.com/{org}/{proj}/_git/Repo1  #repo1 will be cloned into folder Repo1 under $(Build.SourcesDirectory)
    #cd Repo1 #cd the code folder

- powershell: |
    git clone https://{PAT}@dev.azure.com/{org}/{proj}/_git/Repo2  #repo1 will be cloned into folder Repo2 under $(Build.SourcesDirectory)
    #cd Repo2 #cd the code folder
  ....

- task: Build tasks1 #point the solution folder to $(Build.SourcesDirectory)/Repo1
    ...
- task: Build tasks2 #point the solution folder to $(Build.SourcesDirectory)/Repo2
    ...

#- task: Copy file task # copy the built artifacts to a specified folder.

- task: publish build artifacts task  #to publish repo1 artifacts
  ....
- task: publish build artifacts task  #to publish repo2 artifacts
  ....

您还可以使用复制文件任务将构建的工件移动到不同的文件夹。

通过在 powershell 任务中使用 git clone 命令来克隆 repos,您可以将您的作业和阶段组合成一个作业。

更新:

在检查上述 yaml 管道并进行测试后,我发现- checkout: Secondary.Repository导致在重新运行管道的位置创建了新文件夹。

解决方法是使用 powershell 任务进行克隆Secondary.Repository,而不是使用- checkout: Secondary.Repository. 请查看上述解决方法。

您还可以通过报告问题向 Microsoft 报告此问题(单击报告问题并选择 Azure devops)

以上希望有所帮助。

于 2020-02-24T09:01:52.407 回答