LetsDevOps: Parameterized YAML Pipeline in Azure DevOps

Introduction

In this document we will understand Parametrized YAML pipeline. Further we will setup parameterized pipeline for different scenario with Demo.


If you are new to YAML pipeline you can follow below article which has detailed explanation of YAML pipeline from Basic with Demo.


https://www.letsdevops.net/post/letsdevops-complete-guide-to-learn-and-setup-yaml-pipeline-in-azure-devops


What is the Parameterized YAML pipeline

User interface implementation in pipeline is the key of setting up Parameterized pipeline.


Why do we need Parameterized YAML pipeline

During the Pipeline execution or for any Process Automation if you want end user to provide input which can be further used during the execution of pipeline.


Parameterized YAML Pipeline Schema



A Glance of Keywords


Basic Parameter YAML Pipeline

Lets assume you are going to create YAML pipeline to Build an Application based on the Project selection. In this case we can create YAML pipeline with Parameter where end user can Select the Project and only Specific application can be Build.


parameters:
  - name: ProjectName
    type: string
    displayName: "Select the Project Name to Build"
    values:
        - Payment
        - Profile
        - Admin
        - onlinepayment
  


User Interface -> YAML pipeline

After creating the parameterize pipeline you can see the below User interface once you run the Pipeline.






Use of Parameters in YAML pipeline

Now once the parameter is defines how can we use them in the pipeline steps. Each Parameter name will be used as variable and it can be access through below format.


${{ parameters.<parameterName> }} 

Sample YAML Pipeline

name: yamldemo_$(Date:yyyyMMdd)$(Rev:.r)
parameters:
  - name: ProjectName
    displayName: "Select Project Name to Build"
    type: string
    default: payment
    values:
        - payment
        - profile
        - admin
        - onlinepayment
pool:
 name: default
steps:
  - task: VSBuild@1
    inputs:
      solution: '**\*.sln'
      msbuildArgs: '/t:${{ parameters.ProjectName}}'
      platform: 'any cpu'
      configuration: 'release'


Scenario 1:

Lets assume if you are working on the DevOps Role and you want to setup Build/ Release Pipeline for QA and Dev Team. In this scenario you can configure the pipeline in User interface format so that QA and Dev team can use it and trigger the Build or deployment on Demand.


name: yamldemo_$(Date:yyyyMMdd)$(Rev:.r)
parameters:
  - name: ProjectName
    displayName: "Select Project Name to Build"
    type: string
    default: Payment
    values:
        - Payment
        - Profile
        - Admin
        - onlinepayment
  - name: EnvironmentType
    displayName: 'Select the Envionment'
    type: string
    default: QA
    values:
        - QA
        - Dev
        - Security

  - name: UnitTestRun
    displayName: 'Do you want to Run Unit Test?'
    type: boolean
    default: true

  - name: DeployCheck
    displayName: "Do you want to Deploy?"
    type: boolean
    default: true

trigger: none
stages:
- stage: Build
  jobs:
  - job: Build
    pool:
     name: default
    steps:
      - task: VSBuild@1
        inputs:
          solution: '**\*.sln'
          msbuildArgs: '/t:${{ parameters.ProjectName}}'
          platform: 'any cpu'
          configuration: 'release'
- ${{ if eq(parameters.UnitTestRun, true) }}:
  - stage: UnitTestRun
    jobs:
    - job: rununitest
      pool:
       name: default
      steps:
       - task: PowerShell@2
         inputs:
           targetType: 'inline'
           script: |
             # Write your PowerShell commands here.     
             Write-Host "Unit test Setup"
        
- ${{ if eq(parameters.DeployCheck, true) }}:
  - stage: Deploy
    jobs:
    - job: deployapp
      pool:
       name: default
      steps:
       - task: PowerShell@2
         inputs:
           targetType: 'inline'
           script: |
             # Write your PowerShell commands here.
             
             Write-Host "Deploy Setup.."

Scenario 2:

As part of Automation, assume I have developed PowerShell Script which will be granting access to the Azure Resources and to grant access we need some info from End user.


In this case I will allowing user to provide this input during the Pipeline Run.


Parameter:

1. SubscriptionName

2. WebAppName

3. ResourceGroupName

4. UserAccount

5. AccessType




name: yamldemo_$(Date:yyyyMMdd)$(Rev:.r)
parameters:
  - name: SubscriptionName
    displayName: "Enter Subscription Name"
    type: string
    
  - name: WebAppName
    displayName: 'Enter WebAppName'
    type: string

  - name: ResourceGroupName
    displayName: "Enter ResourceGroupName"
    type: string

  - name: UserAccount
    displayName: "Enter UserAccount for Access like:- test@xyx.com"
    type: string

  - name: AccessType
    displayName: "Enter Access Type"
    type: string
    default: Contributor
    values:
        - Reader
        - Contributor
        - Owner
trigger: none
pool:
 name: default
steps:
 - task: PowerShell@2
   displayName: "Grant Access to Azure Resource"
   inputs:
    filePath: '$(System.DefaultWorkingDirectory)\AccessAutomation.ps1'
    arguments: '-SubscriptionName "${{parameters.SubscriptionName}}" -WebAppName "${{parameters.WebAppName}}" -ResourceGroupName "${{parameters.ResourceGroupName}}" -UserAccount "${{parameters.UserAccount}}" -AccessType "${{parameters.AccessType}}"'