Friday, 18 April 2025

Azure DevOps YAML Pipeline: Key Concepts, Hierarchy, and Best Practices

Azure DevOps pipeline flow
 


Azure DevOps YAML pipelines provide a powerful and flexible way to automate your CI/CD workflows. Understanding the hierarchy, variable passing, triggers, and conditions is essential for building efficient pipelines.

YAML Pipeline Hierarchy

A YAML pipeline is structured in a hierarchical manner:

  1. Stages – The top-level division in a pipeline (e.g., "Build," "Test," "Deploy").

  2. Jobs – A sequence of steps that run sequentially or in parallel within a stage.

  3. Steps – The smallest executable unit, which can be a script or a predefined task.


1. Stages

In a YAML pipeline, the top level is stages. Each stage can contain multiple jobs, and each job consists of a series of steps.
Stages define logical boundaries in a pipeline,even if not explicitly defined, every pipeline has at least one stage.
Below are examples of stages:
  • Build App
  • Run Tests
  • Deploy to Production

Example:

yaml
stages:  
- stage: Build App
  jobs:  
  - job: BuildJob  
    steps:  
      - script: echo "Building the app..."  


2. Jobs

A job is a set of steps that run together. Jobs can:

  • Run sequentially or in parallel.
  • Have dependencies (e.g., Job2 depends on Job1).
  • Read more about job here

Example:

yaml
jobs:  
- job: JobA  
  steps:  
    - script: echo "Running Job A"  

- job: JobB  
  dependsOn: JobA  
  steps:  
    - script: echo "Running Job B after Job A" 


3. Steps (Tasks)

Steps are the smallest executable units in a pipeline. They can be:

  • Scripts (Bash, PowerShell, etc.)
  • Predefined tasks (e.g., npm installdotnet build)

Example:

yaml
steps:  
- script: echo "Restoring dependencies..."  
- task: DotNetCoreCLI@2  
  inputs:  
    command: 'build'  

Passing Variables Across Tasks, Jobs, and Stages

1. Across Tasks in the Same Job

Use task.setvariable to pass values between tasks.

Example:

yaml
steps:  
- powershell: |  
    Write-Host "##vso[task.setvariable variable=MYVAR]abc"  
- script: echo $(MYVAR)  # Output: abc  

2. Across Different Jobs

Use isOutput=true and reference the variable via dependencies.

Example:

yaml
jobs:  
- job: Job1  
  steps:  
    - bash: |  
        echo "##vso[task.setvariable variable=MYVAR;isOutput=true]abc"  
      name: SetVarStep  

- job: Job2  
  dependsOn: Job1  
  variables:  
    MYVAR: $[ dependencies.Job1.outputs['SetVarStep.MYVAR'] ]  
  steps:  
    - script: echo $(MYVAR)  # Output: abc  

3. Across Different Stages

Use stageDependencies to pass variables between stages.

Example:

yaml
stages:  
- stage: StageA  
  jobs:  
  - job: JobA  
    steps:  
      - bash: |  
          echo "##vso[task.setvariable variable=MYVAR;isOutput=true]abc"  
        name: SetVarStep  

- stage: StageB  
  dependsOn: StageA  
  variables:  
    MYVAR: $[ stageDependencies.StageA.JobA.outputs['SetVarStep.MYVAR'] ]  
  jobs:  
  - job: JobB  
    steps:  
      - script: echo $(MYVAR)  # Output: abc  

Triggers in YAML Pipelines

Triggers automate pipeline execution based on events like code commits or pull requests.

1. CI Trigger (Continuous Integration)

Runs when changes are pushed to specified branches/paths.

Example:

yaml
trigger:  
  branches:  
    include:  
      - main  
  paths:  
    include:  
      - src/**  
    exclude:  
      - src/docs/**  

2. PR Trigger (Pull Request Validation)

Runs when a PR is created or updated.

Example:

yaml
pr:  
  branches:  
    include:  
      - main  
  paths:  
    include:  
      - src/**  

🔹 Best Practices for Triggers:

  • Use batch: true to optimize CI runs.
  • Path filters are case-sensitive.
  • Wildcards (*) are not supported in path filters.


Conditions in Pipelines

Control when jobs, steps, or stages run using conditions.

Common Conditions:

ConditionDescription
succeeded()Runs only if previous steps succeeded (default).
always()Runs regardless of previous failures.
failed()Runs only if previous steps failed.
eq(variables['var'], 'value')Runs if a variable matches a value.

Example:

yaml
steps:  
- script: echo "This runs only on main branch"  
  condition: eq(variables['Build.SourceBranch'], 'refs/heads/main')  




Please let us know in case of any further information or help. Contact us!



Post Top Ad

Your Ad Spot

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template