skip to Main Content

I’m trying to set up my "first" GitHub action to publish the NuGet package of .NET library to my private GitHub Packages registry.

I want my action to get the version number of the package from the .csproj file. I’m trying to follow the instructions here but looks like they hard-code the version number:
https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-nuget-registry
enter image description here

How do I get the version number from the .csproj file?

Here’s my release.yml file so far:

name: Publish MyApp NuGet to GitHub Packages

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Setup .NET
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: 6.0.x
    - name: Restore dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --no-restore
      
  package:
  
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v3
    - name: Create NuGet package
      run: dotnet pack --configuration Release
  
  publish:
  
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v3
    - name: Publish to GitHub Packages
      run: dotnet nuget push "bin/Release/MyApp.1.0.0.nupkg" --source "github"

And here’s the nuget.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <packageSources>
        <clear />
        <add key="github" value="https://nuget.pkg.github.com/MY_GITHUB_COMPANY_ACCOUNT/index.json" />
    </packageSources>
    <packageSourceCredentials>
        <github>
            <add key="Username" value="MY_GITHUB_USERNAME" />
            <add key="ClearTextPassword" value="MY_GITHUB_PERSONAL_ACCESS_TOKEN" />
        </github>
    </packageSourceCredentials>
</configuration>

And here’s the section in .csproj file where I define package related info:

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
    <Authors>MyCompany, LLC</Authors>
    <Company>MyCompany, LLC</Company>
    <Description>MyApp Library</Description>
    <Version>1.2.1</Version>
    <RepositoryUrl>https://github.com/MY_GITHUB_COMPANY_ACCOUNT/my-app</RepositoryUrl>
    <Copyright>MyCompany, LLC (c) 2015 - 2023</Copyright>
</PropertyGroup>

3

Answers


  1. Try using the Get CSProj Version GH Action. This action should retrieve the current version from a .NET .csproj project file and provide the output that can be used in the next steps.

    Example:

    ...
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout
            uses: actions/checkout@v3
    
          - name: Get version
            id: package_version
            uses: KageKirin/[email protected]
            with:
              file: src/project.csproj # Specify your .csproj file path
    
          - name: Publish to GitHub Packages
            run: dotnet nuget push "bin/Release/MyApp.${{ steps.test.package_version.version }}.nupkg" --source "github"
    

    Similar action: get-net-sdk-project-versions-action

    Login or Signup to reply.
  2. There is a better way to have version numbers, so you don’t have to think about setting up this manually. You can use GitVersion.

    - name: Install GitVersion
      uses: gittools/actions/gitversion/[email protected]
      with:
        versionSpec: '5.x'
    
    - name: Determine Version
      uses: gittools/actions/gitversion/[email protected]
      with:
        useConfigFile: true
    

    This will set env var $GITVERSION_SEMVER and then in your push step you will just say:

    - name: Your project
      run: |
        dotnet pack -p:Version=$GITVERSION_SEMVER projectToPack.csproj
        dotnet push ...
    

    And one more thing. If you specify version in .csproj manually pack/push will take into consideration that number, so there is no need for –p:Version thingy.

    Login or Signup to reply.
  3. An alternative could be using this action

    It is not for C# but you can handle it by specifying a specific path and a specific version prop

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search