Azure Devops Pipeline Basics

Azure Devops Pipeline Basics

Azure Devops Pipeline Basics

Azure DevOps Pipeline concepts

  • Pipeline: It is a workflow that defines how our test, build, and deployment steps are run.

  • Stage: It is a logical boundary in the pipeline. It can be used to mark the separation of concerns. Each stage contains one or more jobs.

  • Job: A stage can contain one or more jobs. Each job runs on an agent. It represents an execution boundary of a set of steps.

  • Step: It is the smallest building block of a pipeline. It can either be a script or a task. A task is simply an already created script offered as a convenience to you.

  • Agent and Agent pools: An agent is an installable software that runs one job at a time. Instead of managing each agent individually, you organize agents into agent pools.

  • Artifact: It is a collection of files or packages published by a run. The Artifact is made available to subsequent tasks, such as distribution or deployment.

  • Trigger: It is something that is set up to tell the pipeline when to run. We can configure a pipeline to run upon a push to the repository, at scheduled times, etc.

  • Environment: It is a collection of resources, where you deploy your application. It contains one or more virtual machines, containers, web apps, etc.

  • Checks: Checks define a set of validations required before a deployment can be performed.

  • Runs: It represents a single execution of a pipeline and collects the logs associated with running the steps and the results of running tests.

  • Define your first Pipeline

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

steps:
- script: echo 'Hello, Azure DevOps!'
  displayName: 'Print a greeting'
  • Define stages
trigger:
  branches:
    include:
      - main

stages:
- stage: Example
  displayName: 'Example Stage'
  jobs:
  - job: ExampleJob
    displayName: 'Example Job'
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - script: |
        echo "Hello, World!"        
      displayName: 'Echo Hello World'
  • Define Environment Variable
trigger:
  branches:
    include:
      - main

stages:
- stage: Example
  displayName: 'Example Stage'
  jobs:
  - job: ExampleJob
    displayName: 'Example Job'
    pool:
      vmImage: 'ubuntu-latest'
    environment:
      variables:
        MY_VARIABLE: 'Hello, World!'
    steps:
    - script: |
        echo "The value of MY_VARIABLE is $MY_VARIABLE"        
      displayName: 'Print Environment Variable'
  • Define simple Variable
trigger:
  branches:
    include:
      - main

variables:
  myVariable: 'Hello, World!'

stages:
- stage: Example
  displayName: 'Example Stage'
  jobs:
  - job: ExampleJob
    displayName: 'Example Job'
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - script: |
        echo "The value of myVariable is $(myVariable)"        
      displayName: 'Print Variable Value'
  • Another Sample pipeline
trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

# Pipelines > Stages > Jobs > Tasks(Steps)

jobs:
- job: Job1
  steps:
  - script: echo Job1 - Hello, world, changed!
    displayName: 'Run a one-line script'
  - script: |
      echo Add other tasks to build, test, and deploy your project.
      echo See https://aka.ms/yaml
      echo more information
    displayName: 'Run a multi-line script'
- job: Job2
  dependsOn: Job1
  steps:
  - script: echo Job2!
    displayName: 'Run a one-line script'
- job: Job3
  dependsOn: Job1
  steps:
  - script: echo Job3!
    displayName: 'Run a one-line script'
- job: Job4
  dependsOn: 
  - Job2
  - Job3
  steps:
  - script: echo Job3!
    displayName: 'Run a one-line script'
  • Basic Multi Stage Example for Azure Pipeline
stages:
- stage: Build
  jobs:
  - job: BuildJob
    steps:
    - script: echo "Building the application"

- stage: Test
  jobs:
  - job: UnitTests
    steps:
    - script: echo "Running unit tests"
  - job: IntegrationTests
    steps:
    - script: echo "Running integration tests"

- stage: Deploy
  jobs:
  - job: DeployToStaging
    steps:
    - script: echo "Deploying to staging environment"
  • Ignore Error example
jobs:
- job: MyJob
 steps:
 - script: echo "This step will continue even if it fails"
   continueOnError: true
 - script: echo "This step will run after the previous step"
  • Example for a basic condition
trigger:
- main

pool:
  vmImage: 'windows-latest'

jobs:
- job: BuildAndTest
  steps:
  - script: echo "Building the application"
  - script: echo "Running tests"
  - script: echo "Deploying artifacts to staging"
    condition: eq(variables['Build.SourceBranch'], 'refs/heads/main')
  • Job Example
jobs:
- job: BuildAndTest
  displayName: "Build and Test Job"
  pool:
    vmImage: 'windows-latest'
  steps:
  - script: echo "Building the application"
  - script: echo "Running tests"
  • Example for Azure pipeline schedule
schedules:
- cron: "0 0 * * *"  # Runs every day at midnight UTC
  displayName: DailyBuild
  branches:
    include:
    - main

jobs:
- job: BuildAndTest
  steps:
  - script: echo "Building the application"
  - script: echo "Running tests"
  • Depends on Example
trigger:
- main

stages:
- stage: BuildAndTest
  jobs:
  - job: Build
    steps:
    - script: echo "Building the application"
  - job: Test
    steps:
    - script: echo "Running tests"

- stage: Deploy
  dependsOn: BuildAndTest
  jobs:
  - job: DeployToStaging
    steps:
    - script: echo "Deploying to staging environment"