Azure DevOps: YAML Pipeline Tutorial, Setting up CI/CD using YAML Pipeline, Multi Stage/Job Setup.

Azure DevOps: YAML Pipeline Tutorial, Setting up CI/CD using YAML Pipeline, Multi Stage/Job Setup.

ยท

4 min read

Introduction

This article is for understanding the core concept of the YAML Pipeline in Azure DevOps. Further it describes how you can write your own YAML file to implement CI/CD.

What is YAML

YAML is a human-readable data-serialization language and it helps to configure pipelines as a Code.

Indentation is very important in YAML.

YAML Pipeline

In Azure DevOps, Pipelines help to set up Continuous Integration/ Continuous Deployment and to achieve this we have the below two options.

1. Classic Editor

2. YAML Pipeline.

In this Document, we will discuss the setup YAML Pipeline.

YAML Pipeline Structure

Hierarchy of YAML File

stages:
- stage: Build
  jobs:
  - job: BuildPackage
    steps:
    - Build
    - Package
    - Publish
- stage: Deploy
  jobs:
  - job: startDeployment
    steps:
    - Deploypackage

Component of YAML Pipeline

Here is the list of components that we use in YAML pipeline creation.

1. Stages

The stage is a collection of Jobs that runs sequentially.

stages:
- stage: Build
  jobs:
  - job: BuildOnWindows
    steps:
    - Build
  - job: BuildOnMac
    steps:
    - Build

2. Jobs

Job is a Collection of Steps that runs on agents/environment.

jobs:
- job: BuildPackage
  steps:
  - Build
  - Package
  - Publish

3. Steps

Steps help to define the set of processes to set up your task or any activity that you want to perform on any specific job.

Kind of Steps:

  • Task

  • Script

  • template reference

steps:
- Build
- Package
- Publish

Schema of YAML Pipeline file.

name: string # Define Custom Build number/Name
variables: #Define variable for Pipeline 
trigger: trigger/none #it defines which branch to enable for CI
stages:[stage| templateReference ]
  jobs:[job| templateReference ]
    steps:[script | task | templateReference ]

Important Note:-

  1. In some cases, if there is only one stage required for the pipeline we can omit the stage keyword and can directly start from Jobs.

  2. In some build setups where only one agent is required for the pipeline in that case we can omit Job and directly define the Steps.

DEMO:

We can begin with the demo to set up the YAML pipeline for below three scenarios below.

Single Stage, Single Job YAML Pipeline

Single Stage Multi Job YAML Pipeline

Multi-Stage YAML Pipeline

1. Single Stage, Single Job YAML Pipeline

Assume that you have been assigned to set up a build for an application. In this scenario, we can omit stages or Jobs.

name: sampleWeb_$(Date:yyyyMMdd)$(Rev:.r)
trigger:
- main
pool:
  name: Default
steps:
- task: VSBuild@1
  inputs:
    solution: '**\*.sln'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package 
 /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactstagingdirectory)\\"'
    platform: 'any cpu'
    configuration: 'release'

Here you can notice that we did not include the Stage and Job section in the YAML pipeline as it is not required in this scenario.

2. Single Stage Multi Job YAML Pipeline

Let's assume you have to perform some activity on different Agents. Like building applications on Windows, Mac OS, and Linux. In this scenario, we can create multiple jobs and each job can be assigned with the respective agent.

name: sampleWeb_$(Date:yyyyMMdd)$(Rev:.r)
trigger:
- main
jobs:
- job: ActivityonLinux
  pool:
   name: default
  steps:
  - script: echo Hello, world!
    displayName: 'Run a one-line script'
- job: ActivityonWindows
  pool:
   name: Default
  steps:
   - task: PowerShell@2
     inputs:
       targetType: 'inline'
       script: |
         # Write your PowerShell commands here.

         Write-Host "Hello World"

3. Multi-Stage YAML Pipeline.

In this scenario, you have assigned one application where you need to build the Project and package it. After that, you need to deploy the application. In this case, we can create two stages.

Stage1 --> Build the Application

Stage 2 --> Deploy the Application

name: sampleApp_$(Date:yyyyMMdd)$(Rev:.r)
trigger:
- main
stages:
- stage: Build
  jobs:
  - job: Build
    pool:
     name: Default
    workspace:
     clean: outputs 
    steps:
    - task: VSBuild@1
      inputs:
        solution: '**\*.sln'
        msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactstagingdirectory)\\"'
        platform: 'any cpu'
        configuration: 'release'
    - task: PublishBuildArtifacts@1
      inputs:
        PathtoPublish: '$(Build.ArtifactStagingDirectory)'
        ArtifactName: 'drop'
        publishLocation: 'Container'
- stage: Deploy
  jobs:
  - job: DeployWeb
    pool:
      name: Default
    steps:
    - powershell: "write-host"
    - task: DownloadBuildArtifacts@1
      inputs:
        buildType: 'specific'
        project: '0a1af395-65ea-473b-b60e-01fdc0d3f93e'
        pipeline: '11'
        buildVersionToDownload: 'latest'
        downloadType: 'single'
        downloadPath: '$(System.ArtifactsDirectory)'
    - task: AzureRmWebAppDeployment@4
      inputs:
        ConnectionType: 'AzureRM'
        azureSubscription: 'testconn'
        appType: 'webApp'
        WebAppName: 'demotest0103'
        packageForLinux: '$(System.ArtifactsDirectory)/**/*.zip'

Author by:

Join Our Telegram Community || Follow me for more DevOps Content

ย