DevOps | Scripts | Automation

Azure DevOps

Azure DevOps YAML pipeline basics

Introduction

In this article, we will cover the below topics.

  • Pre-Requisites to Create ADO Pipeline.
  • Pipeline structure
  • Types of Pipeline
  • Agent Types
  • Connect your Repo to Pipeline
  • Run the first pipeline

Let’s start exploring all the terms and then create first your ADO YAML pipeline.

Pre-requisites

  1. DevOps Account: You should have an Azure DevOps account. You can sign in / sign up for an account here (https://dev.azure.com/ ). For licensing check the link (https://azure.microsoft.com/en-in/pricing/details/devops/azure-devops-services/).
  2. Organization and Project: Once you are logged in, you should have your DevOps organization and project. Use this link Create ADO Organization to create your first organization and Create ADO Project.
  3. Enable Features: Once you create the Organization and projects, make sure you have Azure Pipeline and Azure Repos enabled. That you can set it up from the Project Settings page.
  4. Agent: There are two types of agents, Microsoft hosted and Self-hosted. Self Hosted agent requires a separate virtual machine (VM) to run the code and MS Hosted agent is an image that is created run time and destroyed when the pipeline is finished execution.
    For this article, we are going to use the MS-hosted agent. Check this link (Azure Pipeline Agents) to learn more about agents.
  5. YAML understanding: At the initial level, you should understand how to write the basic YAML code and syntax. You can get various learning materials online.

YAML pipeline structure

ADO YAML pipeline is constructed of several components like Stages, jobs, tasks, etc.

See the below, ADO YAML pipeline structure image.

ADO YAML Pipeline

Types of Pipeline

There are three types of Pipelines that ADO supports as of writing this article.

  • YAML Pipeline
  • Classic Build Pipeline (GUI based)
  • Classic Release Pipeline (GUI Based)

Both Classic and YAML pipelines have pros and cons. YAML pipeline is the latest way to create an ADO pipeline and it is generally helpful if you have a scripting or development background.

A classic pipeline is simple and GUI based and may be deprecated in the future (no timeline is provided by MS yet). The classic pipeline is helpful when you have no coding background and people from System Admin backgrounds mostly use this pipeline.

We are not going through each and every detail for both Pipelines. Check the below article to understand how both the Pipelines are different and their features as well.

https://learn.microsoft.com/en-us/azure/devops/pipelines/get-started/pipelines-get-started?view=azure-devops

When you create a pipeline, you will see the options YAML, and the code is stored on the remote repository like git, if you need to use the classic editor, you can use the classic editor option shown below. For Classic Release, select the Release option.

Agent Types

When you execute the pipeline, the code needs the platform to run and the agent is worked as a middleware for it. You also need to install extensions or tools sometimes like Terraform, go, azure cli, etc and for that agent is needed.

There are two types of agents supported by ADO.

  • Self-Hosted Agent
  • MS Hosted Agent

For Self-Hosted agent is a VM (on-premise or cloud) that users need to create and maintain, while the MS-hosted agents are the run time images that MS provides. MS-Hosted agent images are destroyed when the pipeline execution is completed. So anything you store inside the VM image is not accessible unless you publish them as an artifact.

To know more about agents, follow the articles below.

MS-Hosted agent: https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/hosted?view=azure-devops&tabs=yaml
Self-Hosted Agent:
https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/v2-windows?view=azure-devops
https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/v2-linux?view=azure-devops
https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/v2-osx?view=azure-devops

In this article, we will use the MS-hosted agent for the demo.

Connect your Repo to Pipeline

Good if you are still reading and come so far. Now you are just a few steps away from creating and running your first pipeline.

To run the code through the pipeline, you need to store the code somewhere. ADO has provision for multiple repositories where you can store your code and use any of them. For example, when you create a new pipeline, there is an option to select repo.

The same options will be available in the classic editor. For this article, we will use Azure Repos Git. This will use the existing Azure repository.

Select the first option, Azure Repos Git and then select the repository created for your organization.

Once you select the repository, you need to choose if you have the existing YAML pipeline or if you need to create a new one. As we haven’t created one, you need to select the “Starter Pipeline

Once you click on the starter pipeline, you will see the below default page.

In the above image,

  • azure-pipeline.yml is the name of the pipeline. It is the default name. If the file name already exists with this name then the file name is appended with 1 or 2 and so on. (i.e. azure-pipeline-1.yml). If you open the Azure repository, you can see that file.
  • trigger: trigger is used to run the pipeline automatically. Here, when the main branch is updated, this pipeline will trigger, but we will make it none for the safer side. There are various triggers available, check this link https://learn.microsoft.com/en-us/azure/devops/pipelines/build/triggers?view=azure-devops.
  • pool: As discussed, the pool is for self-hosted or MS-hosted agents. By default, MS hosted pool is used. To know more about pools, check this article.
  • steps: Default tasks are added by the ADO pipeline. You can click the show assistant right side button to add more tasks.

Now, we are going to make a few changes here.

  1. We will replace the main trigger with none as we don’t want to execute the pipeline when the main branch is updated.
  2. we will replace the default steps and add PowerShell code to print hello world with the pipeline.

Once you click PowerShell, the default Powershell inline code will be added. Click Save and run. The code will be as below.

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
- none

pool:
  vmImage: ubuntu-latest

steps:
  - task: PowerShell@2
    inputs:
      targetType: 'inline'
      script: |
        # Write your PowerShell commands here.
        
        Write-Host "Hello World"

Once the pipeline is executed, the output will be displayed on the console.

If you are seeing this screen means you could successfully create and run your first ADO pipeline.

Loading