Azure生成管道-用multipe作业构建的多个存储库-如何在每个构建中重复使用相同的工作目录?

时间:2020-02-23 15:28:23

标签: azure-pipelines multistage-pipeline azure-pipelines-yaml

我在不同的存储库中有一个主要解决方案和N个其他解决方案。每次构建主要解决方案时,我都必须构建N个其他解决方案,并创建包含其他解决方案的主要解决方案的工件。我当前的构建管道如下所示。

  • [阶段1]

    • [作业1]构建主解决方案->发布管道工件(其他解决方案所需)[agent文件夹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'

1 个答案:

答案 0 :(得分:1)

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

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

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

您将需要使用PAT进行选育。选中here以生成具有代码读写范围

的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
  ....

您还可以使用Copy file task将构建的工件移至其他文件夹。

通过在powershell任务中使用git clone命令克隆存储库,您可以将作业和阶段组合为一个作业。

更新:

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

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

您还可以通过Reporting a problem向Microsoft报告此问题(单击“报告问题并选择Azure devops”

希望上面有帮助。