Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a Visual Studio project which is built as a NuGet lib package. But every time I publish the package, I have to change the version number manually. That's a prone-to-error work. I'd like to generate and increase the package version number automatically.


I found GitVersion tool to solve this problem. And I also found some semantic versioning blogs to explain the package version of continuous delivery.

  • GitTools/GitVersion: Easy Semantic Versioning (http://semver.org) for projects using Git
  • GitVersion Documentation
  • Versioning NuGet packages in a continuous delivery world: part 1 – Microsoft DevOps Blog
  • Versioning NuGet packages in a continuous delivery world: part 2 – Microsoft DevOps Blog
  • Versioning NuGet packages in a continuous delivery world: part 3 – Microsoft DevOps Blog

But unfortunately, The GitVersion package does not work correctly for me.

  • It gives me an error that AssemblyVersionAttribute is duplicated.
  • If I add <GenerateAssemblyInfo>false</GenerateAssemblyInfo> into the csproj file, It will do nothing and the package version will be 0.0.0.0.

Maybe the reason is that I'm using the new csproj format. See here to view the csproj file and the file looks like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net45;net47;netstandard2.0</TargetFrameworks>
  </PropertyGroup>
</Project>

Any reply is appreciated.


UPDATE:

I find that there is an issue to mention my problem: Gitversion Task for VS2017-style csproj · Issue #1349 · GitTools/GitVersion. I'm trying the new solution.

like image 366
walterlv Avatar asked Apr 11 '18 02:04

walterlv


2 Answers

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

like image 118
lindexi Avatar answered Oct 05 '22 02:10

lindexi


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

like image 38
walterlv Avatar answered Oct 05 '22 03:10

walterlv