Azure Devops - .Net “Hello World” Project
This project utilizes Azure DevOps to automate its build, test, package, and deployment processes using CI/CD pipelines. The pipeline is designed to run different tasks depending on the branch (either dev or main) and includes several stages to ensure code quality and proper versioning of NuGet packages. Below is an overview of the pipeline setup and its functionality.
Table of Contents
- Pipeline Overview
- Versioning Strategy
- Pipeline Variables
- Tasks Overview
- Environment Setup
- Customizing the Pipeline
- Conclusion
- License
Pipeline Overview
Trigger
The pipeline is triggered on changes to the following branches:
devmain
Jobs
1. DevTasks: Dev Branch Tasks (SonarQube, NuGet, and Publish)
This job is triggered when changes are pushed to the dev branch. It performs tasks related to dependency restoration, building, packaging, and publishing a NuGet package to Azure Artifacts. The version of the NuGet package is determined dynamically using the build ID and the branch name.
Key Steps:
- Checkout code: Ensures the latest code from the repository is checked out.
- Install .NET SDK: Installs the required .NET SDK version (
8.x). - Install NuGet Tool: Installs the NuGet CLI to restore and publish packages.
- NuGet Authentication: Authenticates to the Azure Artifacts feed.
- SonarQube Analysis: Runs code quality analysis using SonarQube.
- Restore Dependencies: Restores NuGet packages for the project.
- Build Project: Builds the project in the Release configuration.
- SonarQube Analyze: Analyzes the project for code quality.
- Pack Project: Packs the project into a NuGet package, using a version derived from the branch and build ID.
- Push NuGet Package: Pushes the generated NuGet package to the Azure Artifacts feed.
- Print Artifact Version: Prints the version of the artifact that was pushed.
Dynamic Versioning:
The version is set dynamically based on the build ID and the branch name. For dev branch, the version follows the format:
1.0.<BuildId>-dev
2. MainTasks: Main Branch Tasks (Build, Pack, and Publish Project)
This job is triggered when changes are pushed to the main branch. It is primarily focused on building and packaging the project into a NuGet package, which is then published to Azure Artifacts. The versioning strategy here is based on the build ID and is updated with each release.
Key Steps:
- Install .NET SDK: Installs the required .NET SDK version (
8.x). - Restore Dependencies: Restores NuGet packages for the project.
- Build Project: Builds the project in the Release configuration.
- Pack Project: Packs the project into a NuGet package.
- Publish Universal Package: Publishes the NuGet package to Azure Artifacts using Universal Packages.
- Print Artifact Version: Prints the version of the artifact.
- Clean Up: Cleans up the staging directory after the publish step.
Versioning Strategy
- Dev Branch: The version is generated dynamically based on the build ID and branch name. For example, if the build ID is
32and the branch isdev, the version will be1.0.32-dev. - Main Branch: Uses a patch versioning scheme, with the version following the format
1.0.<BuildId>.
Pipeline Variables
- artifactVersion: This variable holds the version number for the artifact. It is set dynamically based on the branch and build ID. For
devbranch, it uses the format1.0.$(Build.BuildId)-$(Build.SourceBranchName).
Tasks Overview
1. UseDotNet: Installs the required .NET SDK version.
- Version:
8.x
2. NuGetToolInstaller: Installs the NuGet tool to manage NuGet packages.
3. NuGetAuthenticate: Authenticates to Azure Artifacts feeds to push and pull packages.
4. SonarQubePrepare and SonarQubeAnalyze: Integrates with SonarQube for static code analysis to ensure code quality.
5. DotNetCoreCLI:
restore: Restores project dependencies.build: Builds the project with theReleaseconfiguration.pack: Packages the project into a NuGet package.push: Pushes the NuGet package to Azure Artifacts.
6. UniversalPackages: Publishes the built package to Azure Artifacts as a Universal Package (for main branch).
7. Clean Up: Removes the staging directory after the package is pushed.
Environment Setup
- Azure Artifacts: The pipeline pushes NuGet packages to the Azure Artifacts feed for your project.
- SonarQube: Ensure your SonarQube instance is properly set up and integrated with Azure DevOps. Replace the
SonarQubetask parameters with your instance details (e.g., organization, project key).
Customizing the Pipeline
You can customize the pipeline by modifying:
- Versioning Logic: Modify the version string to follow your preferred versioning scheme (major, minor, patch).
- Branch Names: Add additional conditions for other branches, such as
feature/*for feature-specific builds. - Additional Tasks: Add tasks like tests, deployments, or notifications as needed.
Conclusion
This pipeline automates the build, test, and deployment process for the HelloWorld project. It integrates code quality checks, dependency management, and versioning to ensure that the project is continuously delivered with high standards. The versioning follows a Semantic Versioning approach with a dynamic version that adjusts depending on the branch and build number.
Feel free to modify and extend the pipeline as needed to suit the specific requirements of your project.
License
This project is licensed under the MIT License - see the LICENSE file for details.