Sam
Sam

Reputation: 14586

Azure Pipeline Multi-Stages in YAML vs Separate Release

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 ?

Upvotes: 2

Views: 2124

Answers (1)

Iqan Shaikh
Iqan Shaikh

Reputation: 277

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

Upvotes: 1

Related Questions