在 YAML 上复制经典发布管道设置

问题描述 投票:0回答:1

如何在 YAML 上实现这种设置:

enter image description here

我特别想知道

artifact
部分。我该如何实现它?我认为我应该做的是在阶段之前声明类型为
resources
pipelines
repositories
,然后在每个阶段下的每个作业上使用
DownloadPipelineArtifact@2
步骤。但在我看来,由于我们有很多阶段,我最终会多次重复代码。有什么办法可以实现这一点吗?

或者更确切地说,我是否创建一个单独的阶段来下载工件和存储库,以及它之后的阶段dependsOn它?

我就是不明白!

azure-devops azure-pipelines-release-pipeline azure-pipelines-yaml azure-artifacts
1个回答
0
投票

部署作业中自动下载

中所述

来自当前管道和关联管道资源的所有可用工件都会自动下载到部署作业中并可供您的部署使用。

但是,根据存储库资源,部署作业不会自动克隆源存储库。我们需要在您的工作中使用

checkout

明确定义源存储库

为了避免重复代码,您可以考虑在 YAML 管道中使用

${{each}}
表达式,并插入 templates 来扩展
steps
jobs
stages
(如果适用)。这是一个示例供您参考。

mainPipeline.yml

trigger: none

pool:
  vmImage: ubuntu-latest

parameters:
- name: environments
  type: object
  default:
  - DEV
  - QA
  - PROD

resources:
  repositories:
  - repository: AnotherRepo # Not self
    type: git
    name: Repo1
  pipelines:
  - pipeline: UpstreamBuildPipeline
    source: PipelinePublishingArtifacts

stages:
- ${{ each env in parameters.environments }}:
  - template: stageTemplate.yml
    parameters:
      env: ${{env}}

- stage: Stage_Downstream
  dependsOn:
  - ${{ each env in parameters.environments }}:
    - Stage_${{ env }}
  jobs:
  - job: Job_0
    steps:
    - checkout: none
    - script: |
        echo "This is a traditional job. When no checkout steps are defined,"
        echo "The default behavior is as if checkout: self were the first step, and the current repository is checked out."
  - job: Job_1
    dependsOn: Job_0
    steps:
    - checkout: self
    - checkout: AnotherRepo
    - download: UpstreamBuildPipeline

stageTemplate.yml

parameters:
- name: env
  default: ''

stages:
- stage: Stage_${{parameters.env}}
  dependsOn: [] # The stage is independent from other stages
  jobs:
  - deployment: Deploy_${{parameters.env}}
    environment: ${{parameters.env}}
    strategy:
      runOnce:
        preDeploy:
         steps:
          - download: none
          - script: |
              echo "This is a deployment job. All available artifacts from the current pipeline and from the associated pipeline resources are automatically downloaded in deployment jobs and made available for your deployment."
              echo "To prevent downloads, specify download: none."
              echo "A deployment job doesn't automatically clone the source repo. You can checkout the source repo within your job with checkout: self."
        deploy:
          steps:
          - checkout: self
          - checkout: AnotherRepo
          - script: |
              tree $(System.DefaultWorkingDirectory)
            displayName: Check repo resources
          # - download: UpstreamBuildPipeline
          - script: |
              tree $(Pipeline.Workspace)
            displayName: Check pipeilne resources
         

Image

Image

© www.soinside.com 2019 - 2024. All rights reserved.