DevOps Classroomnotes 02/Jul/2023

Variables in Azure DevOps Pipelines

  • Azure DevOps Pipeline has variables, we can assign values to be used within pipeline across stages, jobs or steps.
  • Variables values can be changed during the pipeline execution
  • Pipeline execution has 3 phases
    • Queue time phase
    • Compile phase
    • Runtime phase
  • Variables can be used by 3 different expressions Refer Here
    • macro $(var)
    • template ${{ variables.var }}
    • runtime expression $[variables.var]
  • Syntax to be used in pipelines Refer Here
  • Script variables can be set from scripts "##vso[task.setvariable variable=myVar;]foo"
  • Variables can be created at
    • pipeline level
    • stages level
    • jobs level
  • Try this pipeline
---
trigger:
  - main

pool: 
  vmImage: ubuntu-22.04

variables:
  - name: var1
    value: value1

stages:
  - stage: stage1
    displayName: 'Stage 1'
    variables:
      - name: var2
        value: value2
    jobs:
      - job: job1
        displayName: Job 1
        variables:
          - name: var3
            value: value3
        steps:
          - bash: echo "##vso[task.setvariable variable=var1;]newvalue"
          - script: "echo $(var1)"
          - script: "echo $(var2)"
          - script: "echo $(var3)"

  • I have 10 variables and i want to use them in multiple pipelines these values can be secrets or text values, in these kind of cases we use variable groups. To create a variable group
  • Lets create a variable group
    Preview
    Preview
  • Now look at below pipeline
---
trigger:
  - main

pool: 
  vmImage: ubuntu-22.04

variables:
  - name: var1
    value: value1
  - group: group1

stages:
  - stage: stage1
    displayName: 'Stage 1'
    variables:
      - name: var2
        value: value2
    jobs:
      - job: job1
        displayName: Job 1
        variables:
          - name: var3
            value: value3
        steps:
          - bash: echo "##vso[task.setvariable variable=var1;]newvalue"
          - script: "echo $(var1)"
          - script: "echo $(var2)"
          - script: "echo $(var3)"
          - script: "echo java_version $(java_version) password = $(password)"
            displayName: "Displaying values from group"

Preview

Lets try writing a pipeline for the following

  • The application which we need to work
  • manual steps
git clone https://github.com/DevProjectsForDevOps/StudentCoursesRestAPI.git
cd StudentCoursesRestAPI
docker image build -t shaikkhajaibrahim/studentcoursescd:(Build-ID) .
docker image push shaikkhajaibrahim/studentcoursescd:(Build-ID)
  • Azure DevOps when it needs to connect to something external uses Service Connections.
  • Lets create a service connection to docker hub
    • Navigate to Project Settings => Service Connections => New service connection and follow the screenshots below
      Preview
      Preview
      Preview
      Preview
  • We have created a pipeline with following content
---

trigger:
  - master

pool: 
  vmImage: ubuntu-22.04

steps:
  - task: Docker@2
    displayName: build and push docker image
    inputs:
      containerRegistry: 'DockerHubConnection'
      command: buildAndPush
      repository: 'shaikkhajaibrahim/studentcoursescd'
      tags: '$(Build.BuildId)'
  • Run the pipeline
    Preview
    Preview

Azure DevOps Extensions

  • Azure DevOPs Extensions provide extra set of tasks to Azure Pipelines. Refer Here
  • Refer Here for notifications on teams channel
  • Examples from Microsoft Refer Here

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner