Karthik
Karthik

Reputation: 2399

How to create a pre-release nuget package using version number from the assembly

I am trying to create a Nuget Package using azure devops. I want to create a pre-release version of a package before a stable release is created.Currently am trying to package an .netstandard 2.0 app.

What i have tried -

Tried to set version in csproj - <Version>1.0.6-alpha</Version> . This actually works but am not sure how can this alpha tag be removed when i want to promote it to a stable version

I want the package to take the version from the assembly (not use auto versioning ) for example if the assembly version is 1.0.0 i need a package that is 1.0.0-alpha and later 1.0.0 when its moved to production . I can see many solutions online that uses preset version numbers (in the variables tab) and appending build number etc but i am looking for a way that can use the version from assembly itself and not custom defined. This is the link which explains package versioning

Below is the pipeline that i have tried

enter image description here

Upvotes: 5

Views: 6243

Answers (2)

Leo Liu
Leo Liu

Reputation: 76670

How to create a pre-release nuget package using version number from the assembly

Since you are trying to package an .netstandard 2.0 app, we could use the dotnet pack, the version is pulled from the project definition (previously project.json, now *.csproj), not the assembly AssemblyInfo.cs.

From the project.json to csproj migration docs, you can use the VersionPrefix and VersionSuffix properties in the project file .csproj:

<PropertyGroup>
  <VersionPrefix>1.0.0</VersionPrefix>
  <VersionSuffix>alpha</VersionSuffix>
</PropertyGroup>

According to your request:

if the assembly version is 1.0.0 i need a package that is 1.0.0-alpha and later 1.0.0 when its moved to production

To resolve this issue, I set the value of VersionSuffix is null, so, it is:

<PropertyGroup>
  <VersionPrefix>1.0.0</VersionPrefix>
  <VersionSuffix></VersionSuffix>
</PropertyGroup>

Then we use the DotNetCoreCLI custom donet task instead of nuget pack task with argument --version-suffix "alpha":

enter image description here

The result is:

enter image description here

In this case, we could generate package version is 1.0.0-alpha when the assembly version is 1.0.0.

On the other hand, we could remove the argument --version-suffix "alpha" when its moved to production:

enter image description here

Now, This should be all you want.

Hope this helps.

Upvotes: 4

devlead
devlead

Reputation: 5010

NuGet.exe is mainly used to pack nuspec files.

When working with the modern csproj format, things like version is specified using MSBuild properties, if it's specified it'll override what's set in the csproj i.e. from the command line using .NET CLI.

dotnet pack /p:Version=VERSIONNUMBER (or launching MSBuild with pack target).

In a Azure DevOps Pipeline task you can enter it by in the ".NET CLI" task using pack command and specifying additional options Additional pack options

But recommended use is automated build versioning using, preferable using build number (build number can be set from configuration, scripts or inferred using tasks like i.e. GitVersion, using build number will give traceability)

Automatic packaging versioning

Upvotes: 1

Related Questions