C#/.NET - How to generate and increase package version automatically especially via CI?

Not sure about Jenkins, but it should be able to generate an incremental number or timestamp by itself that can be accessed via an environment variable on your build pipeline.

I think the most flexible way is to add a PackageVersion tag with a placeholder to your csproj that your build pipeline can then change:

  <PropertyGroup>
    <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
    <PackageVersion>$(PackageVersion)</PackageVersion>
  </PropertyGroup>

So, on your build pipeline, you just pass the version, for example:

dotnet build -p:PackageVersion=$(BUILD_TIMESTAMP)

Actually GitVersionTask is not hard to use. All that you should do is these things below:

  1. Install GitVersionTask from NuGet.
  2. Add a configuration file named GitVersion.yml with some key-values.
  3. Add a tag to your branch.
  4. Build.

After doing that, you can find your output dll file contains a semantic version.


I'm answering my own question because I just wrote the wrong config file name. So it did not work correctly.


This is my configuration file:

mode: ContinuousDelivery
increment: Inherit
tag-prefix: '[vV]'
source-branches: ['master', 'develop', 'hotfix']
branches:
    master:
        regex: master$
        mode: ContinuousDelivery
        tag: ''
        increment: Patch
        prevent-increment-of-merged-branch-version: true
        track-merge-target: false
        tracks-release-branches: false
        is-release-branch: true
    release:
        regex: r(elease$|(eleases)?[-/])
        mode: ContinuousDelivery
        tag: beta
        increment: Patch
        prevent-increment-of-merged-branch-version: true
        track-merge-target: false
        tracks-release-branches: false
        is-release-branch: true
    feature:
        regex: f(eatures)?[-/]
        mode: ContinuousDeployment
        tag: alpha
        increment: Minor
        prevent-increment-of-merged-branch-version: false
        track-merge-target: false
        tracks-release-branches: false
        is-release-branch: false

I've posted this configuration file content here: Automatically increase the semantic version using GitVersion - walterlv


We can trigger the GitHub Action by Git tag pushed and we can read the Git tag name as the version. And then we can generate the NuGet package with this version.

There is a dotnet tool that can read Git tags as a version and write it to the version file.

Before using it, we should create the version file and import the version file.

We should use dotnet to install the dotnetCampus.TagToVersion tool and use the tool to write the Git tag to version file.

The step 1:

Adding the Directory.Build.props file to repo folder.

Writing the code to the Directory.Build.props file.

<Project>
  <Import Project="build\Version.props" />
</Project>

The step 2:

Making a folder named build and adding the Version.props file to this folder.

Writing the code to the build\Version.props file.

<Project>
  <PropertyGroup>
    <Version>1.0.5</Version>
  </PropertyGroup>
</Project>

The step 3:

Writing a GitHub Action configuration file in .github\workflows folder, for example create the .github\workflows\push tag and pack nuget.yml file

Making the Action trigger by tag push.

on:
  push:
    tags:
    - '*' 

Writing the tag as version by dotnet tool.

    - name: Install dotnet tool
      run: dotnet tool install -g dotnetCampus.TagToVersion

    - name: Set tag to version  
      run: dotnet TagToVersion -t ${{ github.ref }}

Building the package

# Build and publish

    - name: Build with dotnet
      run: dotnet build --configuration Release

    - name: Install Nuget
      uses: nuget/setup-nuget@v1
      with:        
        nuget-version: '5.x'

    - name: Add private GitHub registry to NuGet
      run: |
        nuget sources add -name github -Source https://nuget.pkg.github.com/ORGANIZATION_NAME/index.json -Username ORGANIZATION_NAME -Password ${{ secrets.GITHUB_TOKEN }}
    - name: Push generated package to GitHub registry
      run: |
        nuget push .\bin\release\*.nupkg -Source github -SkipDuplicate
        nuget push .\bin\release\*.nupkg -Source https://api.nuget.org/v3/index.json -SkipDuplicate -ApiKey ${{ secrets.NugetKey }} -NoSymbols 

See https://github.com/dotnet-campus/dotnetCampus.TagToVersion