Skip to main content

Command Palette

Search for a command to run...

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

Updated
โ€ข4 min read
Azure DevOps: YAML Pipeline Tutorial, Setting up CI/CD using YAML Pipeline, Multi Stage/Job Setup.
P

๐‘ช๐’๐’๐’–๐’… ๐’‚๐’๐’… ๐‘ซ๐’†๐’—๐‘ถ๐’‘๐’” ๐‘ป๐’†๐’„๐’‰ ๐‘ช๐’๐’Ž๐’Ž๐’–๐’๐’Š๐’•๐’š || ๐‘ท๐’“๐’๐‘ซ๐’†๐’—๐‘ถ๐’‘๐’”๐‘ฎ๐’–๐’š ๐Ÿค– https://t.me/prodevopsguy ๐Ÿ‘‹ Hi there! We are ProDevOpsGuy, a passionate DevOps enthusiast Tech Community with a strong belief in the power of automation and collaboration to drive innovation. ๐Ÿš€ I thrive in bridging the gap between development and operations, creating seamless and efficient software delivery pipelines. My journey in the world of DevOps has allowed me to blend my technical skills with a knack for problem-solving, enabling me to contribute effectively to agile and dynamic environments. ๐Ÿ’ก With a keen interest in continuous integration, continuous delivery (CI/CD), containerization, and orchestration, I've had the privilege to explore cutting-edge technologies like Docker, Kubernetes, Jenkins, and Ansible. I find joy in designing scalable and resilient infrastructures that enable teams to deploy applications faster and with greater confidence. ๐ŸŒ Beyond the tech realm, I'm an advocate for DevOps culture, emphasizing collaboration, communication, and a relentless pursuit of improvement. I'm always eager to connect with fellow professionals, exchange insights, and explore opportunities to collaborate on exciting projects. ๐Ÿ“š When I'm not tinkering with the latest DevOps tools, you can find me indulging in books on technology trends, hiking to rejuvenate, and occasionally experimenting with new coding challenges. ๐ŸŒŸ Let's connect! Whether you're looking to discuss DevOps methodologies, explore partnership opportunities, or simply share experiences, feel free to reach out. I'm excited to be part of the DevOps journey, driving excellence together.

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

More from this blog

P

ProDevOpsGuy Tech Community

73 posts

Home of DevOps Best Blogs/Series