Azure Pipelines is a cloud-based service provided by Microsoft Azure that allows you to build, test, and deploy your applications automatically. It supports a wide range of programming languages and platforms, including C#. Azure Pipelines integrates with GitHub, Azure Repos, and other version control systems to automate the CI/CD (Continuous Integration and Continuous Deployment) process.
In this guide, I'll explain how to set up Azure Pipelines to generate a C# build step by step.
Before you start, ensure you have the following:
Azure DevOps Account: Sign up at Azure DevOps if you don’t already have an account.
Azure DevOps Project: Create a project in Azure DevOps to manage your pipeline.
Source Code Repository: Your C# project should be in a Git repository (e.g., Azure Repos, GitHub, or Bitbucket). In our case it if Azure Repos.
Basic Knowledge of YAML: Azure Pipelines can be configured using YAML files or the classic editor. This guide will focus on YAML.
Step 1: Create a New Pipeline
Log in to your Azure DevOps account.
Navigate to your project.
Go to the Pipelines section in the left-hand menu.
Click on New Pipeline.
Step 2: Connect Your Repository
Azure Pipelines will ask you to select the source repository for your code. Choose the appropriate option:
Azure Repos Git: If your code is in Azure Repos.
GitHub: If your code is in GitHub.
Other Git Repositories: If your code is hosted elsewhere.
Authenticate and select the repository containing your C# project.
Step 3: Configure the Pipeline
After selecting the repository, Azure Pipelines will ask how you want to configure the pipeline. You can either:
Use the Starter Pipeline template.
Use an existing YAML file in your repository.
Start from scratch.
For a C# project, Azure Pipelines can automatically detect the type of project and suggest a YAML template. If it doesn’t, you can create one manually.
Step 4: Create a YAML File for the Build Pipeline
Here’s an example YAML file for building a .NET (C#) project:
# .azure-pipelines.yml
trigger:
- main # Replace 'main' with your branch name
pool:
vmImage: 'windows-latest'
steps:
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '6.x' # Specify the .NET version (e.g., 6.x, 5.x, 3.1)
- script: |
dotnet restore
dotnet build --configuration Release
displayName: 'Restore and Build'
- script: |
dotnet test --configuration Release
displayName: 'Run Tests'
Copy
Explanation of the YAML File:
trigger: Specifies the branch that triggers the pipeline (e.g., main).
pool: Specifies the virtual machine image to use for the build (e.g., windows-latest).
UseDotNet@2: Installs the required .NET SDK version.
dotnet restore: Restores NuGet packages.
dotnet build: Builds the project in Release mode.
dotnet test: Runs unit tests.
Step 5: Save and Run the Pipeline
Save the YAML file as .azure-pipelines.yml in the root of your repository.
Commit and push the file to your repository.
Azure Pipelines will automatically detect the YAML file and start the build process.
Step 6: Monitor the Build
Go to the Pipelines section in Azure DevOps.
Click on the pipeline you just created.
Monitor the build process in real-time. If there are any errors, Azure Pipelines will display detailed logs.
Step 7: Verify the Build Artifacts (Optional)
If you want to generate build artifacts (e.g., .dll or .exe files), you can add the following step to your YAML file:
- task: PublishBuildArtifacts@1
inputs:
pathToPublish: '$(Build.ArtifactStagingDirectory)'
artifactName: 'drop'
publishLocation: 'Container'
This will publish the build output to Azure DevOps, where you can download it.
Create a new pipeline in Azure DevOps.
Connect your repository.
Configure the pipeline using a YAML file.
Add steps to restore, build, and test your C# project.
Save and run the pipeline.
Monitor the build and verify the output.
Unit Tests: Ensure your C# project has unit tests to validate the build.
Build Triggers: You can configure triggers to run the pipeline on pull requests or specific branch updates.
Environment Variables: Use Azure DevOps variables to manage sensitive data like connection strings or API keys.
By following these steps, you can successfully set up Azure Pipelines to generate builds for your C# project.