How Version Control and Build Numbering works in dot net project
In a .NET project, version control and build numbering are crucial aspects that help manage different versions of the software. This is typically handled using the .csproj
file for versioning, and tools like dotnet
CLI, Git, and CI/CD pipelines for automation. Semantic Versioning (SemVer) is often used, which follows the MAJOR.MINOR.PATCH
pattern.
Setting Up Version Control and Build Numbering in .NET
-
Version Number in
.csproj
: The version number is specified in the.csproj
file of your .NET project.<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net5.0</TargetFramework> <Version>1.0.0</Version> </PropertyGroup> </Project>
Version Control and Version Incrementing
-
Manual Version Increment:
- You can manually update the
<Version>
element in the.csproj
file to the desired version. - Example:
<Version>1.1.0</Version>
- Commit and tag the version in Git:
git commit -am "Release version 1.1.0" git tag -a v1.1.0 -m "Version 1.1.0"
- You can manually update the
-
Automated Version Increment:
- Using
dotnet
CLI, you can automate versioning as part of your CI/CD pipeline.
- Using
Using GitVersion for Automated Versioning
GitVersion is a tool that automates Semantic Versioning based on your Git history. It calculates the next version number and can update your project files accordingly.
-
Install GitVersion:
dotnet tool install --global GitVersion.Tool
-
Configure GitVersion: Create a
GitVersion.yml
file in the root of your repository to configure the versioning strategy.next-version: 1.0.0 branches: master: tag: '' develop: tag: '-beta'
-
Run GitVersion:
- Run GitVersion to calculate the version number.
gitversion /output json /showvariable FullSemVer
- To update the version in your
.csproj
file:gitversion /updateassemblyinfo
- Run GitVersion to calculate the version number.
Automating Versioning and Build Numbering in CI/CD Pipeline
In a CI/CD pipeline (e.g., GitHub Actions, Azure DevOps, Jenkins), you can automate the versioning and build process using GitVersion and dotnet
CLI commands.
Example GitHub Actions Workflow:
name: Build and Release
on:
push:
branches:
- master
- develop
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '5.0.x'
- name: Install GitVersion
run: dotnet tool install --global GitVersion.Tool
- name: Use GitVersion to calculate version
run: gitversion /output buildserver
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
- name: Publish
run: dotnet publish --no-build -c Release -o ./publish
Example Workflow
Here’s a step-by-step example of how version control and build numbering might work in a .NET project using dotnet
CLI and GitVersion:
-
Initial Setup:
- Create a
.csproj
file with the initial version.<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net5.0</TargetFramework> <Version>1.0.0</Version> </PropertyGroup> </Project>
- Create a
-
Install GitVersion:
dotnet tool install --global GitVersion.Tool
-
Configure GitVersion: Create a
GitVersion.yml
file:next-version: 1.0.0 branches: master: tag: '' develop: tag: '-beta'
-
Make Code Changes:
- Develop new features or bug fixes.
- Commit changes with meaningful messages.
-
Automate Versioning:
- Run GitVersion to calculate and update the version:
gitversion /updateassemblyinfo
- Commit the updated version and push the changes:
git commit -am "Apply version updates" git push
- Run GitVersion to calculate and update the version:
-
CI/CD Pipeline:
- Use a CI/CD pipeline to automate the build and release process, integrating the steps for restoring dependencies, building, testing, and publishing the application.