Home > Back-end >  Getting version number for package in Github Actions
Getting version number for package in Github Actions

Time:01-01

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: 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>

CodePudding user response:

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

  • Related