Azure DevOps流水线:如何同一阶段仅下载一次工件供所有作业复用?
解决方案:在Azure DevOps阶段级别统一配置检出与工件下载
可以通过阶段级别的配置实现同一阶段内所有作业共享禁用自动检出和工件下载的逻辑,无需在每个作业中重复编写步骤。以下是具体实现方式:
1. 阶段级统一配置(推荐)
在stage节点下直接设置checkout和download属性,该阶段下的所有作业会自动继承这些配置:
trigger: none pool: name: Default demands: - Agent.Name -equals SelfHostedAgent resources: pipelines: - pipeline: 'repoCi' source: '\\Folder\\Repo-CI' trigger: true stages: - stage: WPF # 阶段级别禁用自动检出,所有作业都不会拉取代码仓库 checkout: none # 阶段级别自动下载repoCi流水线的指定工件 download: repoCi artifacts: - name: WPF # 只下载WPF工件,若要下载所有可省略该段 jobs: - job: 'GetVersions' steps: - task: PowerShell@2 displayName: 'Get Exe Versions' inputs: targetType: 'inline' script: | # 直接访问已下载的WPF工件,路径示例:$(Pipeline.Workspace)/repoCi/WPF Get-ChildItem $(Pipeline.Workspace)/repoCi/WPF - job: InstallerJob1 dependsOn: 'GetVersions' condition: succeeded('GetVersions') steps: # 无需重复写checkout和download,直接使用工件 - task: AdvancedInstaller@1 displayName: '生成WPF安装包' inputs: # 示例:指向已下载的exe路径 aipPath: '$(Pipeline.Workspace)/repoCi/WPF/你的安装包项目.aip' # 其他Advanced Installer配置
关键说明:
checkout: none:作用于整个阶段,所有作业都不会自动拉取代码仓库,避免不必要的代码检出。download: repoCi+artifacts:指定自动下载关联流水线repoCi的WPF工件,每个作业的代理中都会自动完成下载,路径统一为$(Pipeline.Workspace)/<流水线名称>/<工件名称>。
2. 可选:通过模板复用步骤(适合复杂场景)
如果需要对部分作业做差异化配置,可以将checkout和download步骤抽成模板,在需要的作业中引用:
模板文件(如templates/download-artifacts.yml):
steps: - checkout: none - download: 'repoCi' artifact: 'WPF'
流水线中引用模板:
stages: - stage: WPF jobs: - job: 'GetVersions' steps: - template: templates/download-artifacts.yml - task: PowerShell@2 # ... 任务配置 - job: InstallerJob1 dependsOn: 'GetVersions' condition: succeeded('GetVersions') steps: - template: templates/download-artifacts.yml - task: AdvancedInstaller@1 # ... 任务配置
注意事项
- 每个作业的运行环境是独立的(即使在同一代理上,工作目录也可能不同),所以阶段级配置本质是让每个作业自动执行一次下载,但无需手动编写步骤。
- 如果要实现所有作业共享同一份下载的工件,需要确保所有作业在同一个代理上运行,并设置
workspace: reuse,但这种方式会降低流水线的并行性,不推荐用于常规场景。
内容的提问来源于stack exchange,提问作者JuanDYB
相关产品推荐
相关产品推荐

