3
votes

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

2
@LeoLiu-MSFT sorry am just trying this out now. Looks like the versioning works. But am trying to find out how to create the nupkg file in the artifacts directory. Its now getting created in bin and so artifact publish job cannot find it .Karthik
You just need to add another argument --output to specify the artifacts directory:docs.microsoft.com/en-us/dotnet/core/tools/…Leo Liu-MSFT
@LeoLiu-MSFT yea i got it to work by using --output $(system.artifactsdirectory) . Is this the right directory to use? i got this by checking where the nuget pack command packs the file and mapping it to system variables.Karthik
Yes, as I pointed in above comment, that is correct argument. Since my answer helps you, you could accept it as answer, so it could help other community members who get the same issues and we could archive this thread, thanks.Leo Liu-MSFT
@LeoLiu-MSFT Please confirm the directory ($(system.artifactsdirectory) ) i have used is correct to create nuget packagesKarthik

2 Answers

4
votes

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.

0
votes

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