Azure Pipelines

Overview

Azure Pipelines is a cloud service that supports CI/CD to build, test, and deploy your code to any platform. It works with any language, any platform, and any cloud.

Getting Started

Follow these steps to create your first pipeline:

  1. Navigate to your Azure DevOps project.
  2. Select Pipelines → Create Pipeline.
  3. Choose the repository source (e.g., Azure Repos Git).
  4. Select Starter pipeline or configure a azure-pipelines.yml file.
  5. Save and run the pipeline.
trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

steps:
- script: echo Hello, Azure Pipelines!
  displayName: 'Run a one-line script'

YAML Basics

Azure Pipelines uses a YAML syntax to declare the pipeline structure. Key concepts:

stages:
- stage: Build
  jobs:
  - job: Compile
    steps:
    - task: DotNetCoreCLI@2
      inputs:
        command: 'build'
        projects: '**/*.csproj'
- stage: Test
  dependsOn: Build
  jobs:
  - job: UnitTests
    steps:
    - task: VSTest@2
      inputs:
        testSelector: 'testAssemblies'
        testAssemblyVer2: '**/*Tests.dll'

Built‑in Tasks

Azure Pipelines provides over 150 built‑in tasks. Below is a quick reference for the most common ones.

TaskPurposeTypical Use
DotNetCoreCLI@2Run .NET Core commandsRestore, build, test, publish
NodeTool@0Install a specific Node.js versionBuild JavaScript projects
CopyFiles@2Copy files to a target folderPrepare artifacts
PublishBuildArtifacts@1Publish artifacts for release pipelinesShare binaries, packages

Variables & Secrets

Variables can be defined at pipeline, stage, or job level. Secrets are stored securely.

variables:
  buildConfiguration: 'Release'
  npmVersion: '8.x'

steps:
- task: Npm@1
  inputs:
    command: 'install'
    workingDir: '$(Build.SourcesDirectory)'
  env:
    NODE_AUTH_TOKEN: $(npmToken)   # secret variable

Triggers

Control when pipelines run using trigger, pr, and schedule.

# Trigger on any push to main or develop
trigger:
  branches:
    include:
    - main
    - develop

# PR validation only for feature/* branches
pr:
  branches:
    include:
    - feature/*

# Nightly build at 02:00 UTC
schedules:
- cron: '0 2 * * *'
  displayName: Nightly Build
  branches:
    include:
    - main
  always: true

Deployments

Deploy to Azure, Kubernetes, or any target using release pipelines or multi‑stage YAML.

stages:
- stage: Deploy
  dependsOn: Test
  jobs:
  - deployment: WebApp
    environment: 'prod'
    strategy:
      runOnce:
        deploy:
          steps:
          - task: AzureWebApp@1
            inputs:
              azureSubscription: 'MySubscription'
              appName: 'my-webapp'
              package: '$(Pipeline.Workspace)/drop/**/*.zip'

Best Practices

# Example of a reusable template
# templates/build.yml
parameters:
  vmImage: 'ubuntu-latest'

steps:
- task: DotNetCoreCLI@2
  inputs:
    command: 'restore'
    projects: '**/*.csproj'

# In main pipeline
extends:
  template: templates/build.yml
  parameters:
    vmImage: 'windows-2022'

Reference

For detailed documentation, visit the official Microsoft Docs site.