How to generate NuGet package with Git Tag version using GitHub Action

2022-08-12 19:12:38 浏览数 (1)

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.

代码语言:javascript复制
<Project>
  <Import Project="buildVersion.props" />
</Project>

The step 2:

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

Writing the code to the buildVersion.props file.

代码语言:javascript复制
<Project>
  <PropertyGroup>
    <Version>1.0.5</Version>
  </PropertyGroup>
</Project>

The step 3:

Writing a GitHub Action configuration file in .githubworkflows folder, for example create the .githubworkflowspush tag and pack nuget.yml file

Making the Action trigger by tag push.

代码语言:javascript复制
on:
  push:
    tags:
    - '*' 

Writing the tag as version by dotnet tool.

代码语言:javascript复制
    - 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

代码语言:javascript复制
# 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 .binrelease*.nupkg -Source github -SkipDuplicate
        nuget push .binrelease*.nupkg -Source https://api.nuget.org/v3/index.json -SkipDuplicate -ApiKey ${ { secrets.NugetKey } } -NoSymbols 

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

0 人点赞