1
votes

Azure Pipelines support multiple stages in YAML. One typical example would be to have something like :

trigger:
- master

pool:
  name: Default
  demands:
  - npm
  - msbuild
  - visualstudio

stages:

  - stage: build

    jobs:
     - job: Build app

  - stage: deploy
   
    jobs:
    - job: Deploy to dev

I'm not used to working like this. Usually, I would run the pipeline to build my application and drop artifacts to a drop folder. The pipeline would be the same regardless the environment that would later be targeted by the release. Then I would choose to run a release, either Integration, UAT, or Production.

However, having multi-stages pipeline we are mixing the build and the release together. So how would I release in a given environment ? Do I have to duplicate this pipeline per environment ?

1
I think this is a good question and the guidance in MS Docs is not clear to me. The problem with having all stages in one big yaml (even when using templates) is that they will all be potentially executed (except one puts in conditions and/or approvals) upon some trigger (manual or automated). But often I only want to build or only build and deploy to dev, etc. So the question is should I still be putting all in one big main yaml (with conditions) or seperate them into dedicated release yamls per environment (group, e.g. dev+tst, acc, prd). Not clear to me.Triamus

1 Answers

1
votes

You can use template structure here. In this you will need to create separate files for different jobs and variables. Then you will need to execute templates with suitable variable template files for each stage.

Directory structure Directory Structure

Pipeline: Pipeline stages

Environments Environments

Here's the sample pipeline:

trigger:
- master

variables:
  - name: vmImage
    value: 'ubuntu-latest'

stages:
  - stage: Build
    displayName: Build stage
    jobs:
    - job: BuildJob
      pool:
        vmImage: $(vmImage)
      steps:
      - template: Jobs/build.yml
 
  - stage: NonProd
    displayName: Deploy non prod stage
    jobs:
    - deployment: DeploymentJob1
      pool:
        vmImage: $(vmImage)
      environment: non-prod
      variables:
        - template: Variables/non-prod.yml
      strategy:
        runOnce:
          deploy:
            steps:
            - template: Jobs/deploy.yml

  - stage: Prod
    displayName: Deploy prod stage
    jobs:
    - deployment: DeploymentJob2
      pool:
        vmImage: $(vmImage)
      environment: prod
      variables:
        - template: Variables/prod.yml
      strategy:
        runOnce:
          deploy:
            steps:
            - template: Jobs/deploy.yml
    

Jobs/build.yml

steps:
- script: echo I am building!
  displayName: 'Run Build'

Jobs/deploy.yml

steps:
- script: echo I am deploying to $(Environment)!
  displayName: 'Run Deployment'

Variables/non-prod.yml

variables:
- name: Environment
  value: non-prod

Variables/prod.yml

variables:
- name: Environment
  value: prod