Azure pipelines Azure构建管道-使用多个作业构建多个存储库-如何在每个构建中重用相同的工作目录?
我在不同的存储库中有一个主解决方案和N个其他解决方案。每次构建主解决方案时,我都必须构建N个其他解决方案,并创建一个包含其他解决方案的主解决方案工件。我当前的构建管道如下所示Azure pipelines Azure构建管道-使用多个作业构建多个存储库-如何在每个构建中重用相同的工作目录?,azure-pipelines,multistage-pipeline,azure-pipelines-yaml,Azure Pipelines,Multistage Pipeline,Azure Pipelines Yaml,我在不同的存储库中有一个主解决方案和N个其他解决方案。每次构建主解决方案时,我都必须构建N个其他解决方案,并创建一个包含其他解决方案的主解决方案工件。我当前的构建管道如下所示 [第一阶段] [Job 1]构建主解决方案->发布管道工件(其他解决方案所需)[代理文件夹1] [作业2..N]签出解决方案“N”->生成->发布管道工件[代理文件夹2..N] [阶段2]-所有作业成功后 [作业1]-下载主工件->下载其他工件->将它们放入一个工件->发布[代理文件夹N+1] 我的问题是,下次
- [第一阶段]
- [Job 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个辅助解决方案的模板的代码(在本例中仅一个)
每个作业都有自己的默认工作目录,并且与其他作业分开,由代理自动设置,不能更改。因此,不能在同一代理工作目录中运行每个作业 有一种解决方法可以在同一工作文件夹下下载多个repo,并在单个作业中构建它们 您可以在powershell任务中运行git命令来克隆同一工作目录中的多个repo。然后将生成任务点设置为每个repo文件夹以生成每个解决方案。检查以下示例: 您需要使用PAT来进行提示。选中以生成具有代码读写范围的PAT 您还可以使用将生成的工件移动到不同的文件夹 通过在powershell任务中使用git clone命令克隆repo,您可以将作业和阶段合并到一个作业中 更新: 在检查yaml管道并进行测试之后,我发现
-checkout:Secondary.Repository
导致在重新运行管道的地方创建了新文件夹
解决方法是使用powershell任务克隆Secondary.Repository
,而不是使用-签出:Secondary.Repository
。请查看以上解决方法
您还可以通过向Microsoft报告此问题(单击“报告问题”并选择Azure devops)
希望以上有帮助。我在最后一步尝试删除作业2..N的文件夹,但由于作业后签出需要源文件夹,因此无法删除。您好,谢谢您的回答。我明白这一点,但我需要使用多个作业(因此我只能重新运行失败的作业)。困扰我的是,如果我只有一个作业,代理会创建一个文件夹(即_works/7),如果我重新运行构建管道,他会再次使用相同的文件夹(回收),并且没有副本。我能在N个作业中有这样的行为吗?这样,再次运行的每个作业(在构建管道的另一次运行期间)都会重用相同的文件夹?我不理解重用相同的文件夹?您的意思是当管道再次运行时,您不想清除代理文件夹中以前的内容和生成结果吗。您可以通过签出设置覆盖作业上的清理行为。假设我有一个包含一个作业的生成定义。代理将使用/s、/a等文件夹创建文件夹(即_work/17)。如果我再次运行此生成定义(新生成,相同管道),代理将不会创建文件夹_work/18,而是重新使用文件夹_work/17),因此不会出现内存问题。如果我的生成定义有2个作业,代理将创建文件夹17和18…在我重新运行相同的生成代理后,将不再重用17和18,而是创建文件夹19和20,将前两个文件夹保留在磁盘上。我是否可以改变这一点,使其行为与重用1个作业相同17,18)?您是否可以共享您的管道?我有一个有两个任务的测试管道,但我无法重现您的场景。我的管道始终在我的本地托管代理上仅创建一个文件夹(即_work/1)。忘了提及管道具有到另一azure管道的存储库资源,第一个作业执行签出:self和第二个作业执行签出:“theOtherRepo”在该场景中,代理创建多个文件夹,并且在另一个管道运行期间不重用它们。我会很快分享的抱歉耽搁了
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'
- 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
....