In the realm of .NET development, establishing a robust CI/CD pipeline is vital for ensuring code quality and delivering reliable software. Azure Pipelines, part of the Azure DevOps suite, provides a seamless solution for automating build and deployment processes. In this tutorial, we’ll guide you through the process of setting up Azure Pipelines for a .NET project, complete with quality tests and SonarCloud integration.
Prerequisites:
- Azure DevOps Account:
- Sign up for an Azure DevOps account at https://dev.azure.com/.
- .NET Core SDK:
- Install the latest version of the .NET Core SDK from https://dotnet.microsoft.com/download.
Step 1: Create a New Project and Repository
- Azure DevOps Project:
- Log in to your Azure DevOps account and create a new project.
- Source Code Repository:
- Connect your preferred source code repository (e.g., GitHub, Bitbucket, Azure Repos) to your project.
Step 2: Configure the azure-pipelines.yml
File
Create an azure-pipelines.yml
file at the root of your .NET project. Copy and customize the following configuration:
trigger:
- main
pool:
vmImage: 'windows-latest'
jobs:
- job: Build
displayName: 'Build and Test'
steps:
- task: UseDotNet@2
inputs:
version: '3.x'
installationPath: $(Agent.ToolsDirectory)/dotnet
- script: dotnet restore
displayName: 'Restore Dependencies'
- script: dotnet build --configuration Release
displayName: 'Build Solution'
- script: dotnet test --configuration Release --collect:"XPlat Code Coverage"
displayName: 'Run Unit Tests'
env:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
DOTNET_CLI_TELEMETRY_OPTOUT: true
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: 'Cobertura'
summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'
failIfCoverageEmpty: true
displayName: 'Publish Code Coverage'
- job: QualityCheck
displayName: 'Run Quality Checks'
dependsOn: Build
steps:
- task: SonarCloudPrepare@1
inputs:
SonarCloud: 'YourSonarCloudServiceConnection'
organization: 'YourSonarCloudOrganization'
projectKey: 'YourProjectKey'
projectName: 'YourProjectName'
scannerMode: 'MSBuild'
- script: |
# Additional quality checks or static code analysis tools can be added here
displayName: 'Additional Quality Checks'
- task: SonarCloudAnalyze@1
- task: SonarCloudPublish@1
inputs:
pollingTimeoutSec: '300'
Replace the placeholders like '
YourSonarCloudServiceConnection
'
, '
YourSonarCloudOrganization
'
, '
YourProjectKey
'
, and '
YourProjectName
'
with your actual SonarCloud configuration.
Step 3: Commit and Push
Commit the azure-pipelines.yml
file to your repository and push the changes. This will trigger the Azure Pipelines to run.
Step 4: Monitor Pipeline Runs and Iterate
- Azure DevOps Portal:
- Navigate to the Azure DevOps portal and monitor the pipeline runs under the Pipelines section.
- Feedback Loop:
- Review the logs and results provided by the pipeline. Address any issues and iterate on your pipeline configuration as needed.
Conclusion:
Congratulations! You’ve successfully set up Azure Pipelines for your .NET project with quality tests and SonarCloud integration. This automated pipeline will help you maintain code quality and streamline the development process. Keep refining and customizing your pipeline based on the specific needs of your project. Happy coding!