How to Create a Build Pipeline in Azure DevOps
To create a build pipeline in Azure, use
Azure DevOps Pipelines. Start by connecting your code repository, then define build steps using a YAML file or the visual designer, and finally run the pipeline to automate building your application.Syntax
A build pipeline in Azure DevOps is defined using a YAML file named azure-pipelines.yml or created via the visual designer. The YAML syntax includes:
- trigger: Defines when the pipeline runs (e.g., on code push).
- pool: Specifies the agent machine to run the build.
- steps: Lists tasks like restoring dependencies, building code, running tests.
yaml
trigger:
branches:
include:
- main
pool:
vmImage: 'ubuntu-latest'
steps:
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '6.x'
- script: dotnet build --configuration Release
displayName: 'Build project'
- script: dotnet test
displayName: 'Run tests'Example
This example shows a simple YAML build pipeline for a .NET project that triggers on pushes to the main branch, uses an Ubuntu agent, installs .NET SDK, builds the project, and runs tests.
yaml
trigger:
branches:
include:
- main
pool:
vmImage: 'ubuntu-latest'
steps:
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '6.x'
- script: dotnet build --configuration Release
displayName: 'Build project'
- script: dotnet test
displayName: 'Run tests'Output
Pipeline triggered on push to main branch.
Using agent: ubuntu-latest
Installing .NET SDK 6.x
Building project in Release configuration
Running tests
Build and tests completed successfully.
Common Pitfalls
- Not specifying the correct branch in
triggercauses the pipeline not to run. - Using an unsupported agent image can fail the build.
- Missing or incorrect task versions may cause errors.
- Forgetting to commit the
azure-pipelines.ymlfile to the repository prevents pipeline creation.
yaml
trigger:
branches:
include:
- master # Wrong branch name if your main branch is 'main'
pool:
vmImage: 'windows-latest' # Use 'ubuntu-latest' if your tasks require Linux
steps:
- script: echo Hello
displayName: 'Sample step'
# Corrected version:
trigger:
branches:
include:
- main
pool:
vmImage: 'ubuntu-latest'
steps:
- script: echo Hello
displayName: 'Sample step'Quick Reference
Key points to remember when creating Azure build pipelines:
- Trigger: Controls when the pipeline runs.
- Pool: Selects the build agent environment.
- Steps: Define tasks like build, test, and deploy.
- YAML file: Store pipeline config in
azure-pipelines.ymlat repo root. - Visual Designer: Alternative to YAML for pipeline creation.
Key Takeaways
Use Azure DevOps Pipelines to automate building your code.
Define your build steps clearly in a YAML file or use the visual designer.
Set triggers to control when your pipeline runs, usually on code push.
Choose the correct agent pool matching your build environment needs.
Always commit your pipeline configuration file to your repository.