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
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
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:
Similar action: get-net-sdk-project-versions-action
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.
This will set env var
$GITVERSION_SEMVER
and then in your push step you will just say:And one more thing. If you specify version in
.csproj
manuallypack/push
will take into consideration that number, so there is no need for –p:Version
thingy.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