Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 956
Sam Avatar asked Dec 18 '25 11:12

Sam


1 Answers

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
like image 186
Iqan Shaikh Avatar answered Dec 20 '25 10:12

Iqan Shaikh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!