Anonymous Person
Anonymous Person

Reputation: 1538

Replicating Classic release pipeline setup on YAML

How do I achieve this kind of a setup on YAML:

enter image description here

I am specifically wondering about the artifact section. How do I achieve it? What I think I ought to do is have resources of the type pipelines and repositories declared before the stages, and then use DownloadPipelineArtifact@2 step on each job under each stage. But the way I see it, that I will end up duplicating the code many times since we have many stages. Is there any way this can be achieved?

Or rather, do I create a separate stage for downloading the artifact and the repos, and the stages after it dependsOn it?

I just don't get it!

Upvotes: 0

Views: 80

Answers (1)

Alvin Zhao - MSFT
Alvin Zhao - MSFT

Reputation: 5797

As stated in Automatic download in deployment jobs

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.

However, as per repository resources, a deployment job doesn't automatically clone the source repo. We need to explicitly define the source repo within your job with checkout

To avoid duplicating the code, you may consider using the ${{each}} expression in a YAML pipeline and inserting templates to expand steps , jobs, or stages if applicable. Here is a sample for your reference.

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

Upvotes: 0

Related Questions