4

Looking for the solution which allow us to automate NuGet package deployment process. At the moment "Pack" option just builds package, but everything else needs to be done manually. I would like to have these steps built into this new "Pack and Deploy" task:

  1. Increment Package version - Something similar to "npm version patch"
  2. Builg package
  3. Push just created package to the NuGet repository

Maybe someone has done this already and could share experiences?


After more research found how to Push after packing: Automating Nuget Package Push With .NetCore RC2

Algis
  • 612
  • 1
  • 8
  • 23

1 Answers1

4
  1. Increment Package version - Something similar to "npm version patch"

Before answering this question, we need to know some info about the AssemblyVersion, AssemblyInformationalVersion and NuGet version, please check this thread for some details. Then you will know that:

Nuget doesn’t use either AssemblyVersion or AssemblyFileVersion in the AssemblyInfo.cs file. It uses a third versioning attribute: AssemblyInformationalVersion.

Nuget actually applys whatever is in that string as the package version. So, to increment package version, we need to manually add following code in the AssemblyInfo.cs:

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("2.0.0")]

So, when we need to release a new version package, we just need to update the value of AssemblyInformationalVersion in the AssemblyInfo.cs file.

  1. Build package

The answer for this question depends on the style of your project, the old project style or the new project style(VS2017-SDK).

Source Old csproj to new csproj: Visual Studio 2017 upgrade guide

When you're in the old style project:

You can just pack the nuget package automatically by a post-build event:

$(PathOfYourNuGet)\nuget.exe pack "$(ProjectPath)"

With this build event, MSBuild will create the nuget package automatically when you build the project.

When you in the New style project: In the new style project, packagin nuget package becomes very easy. Just check the checkbox Generate NuGet packafe on build on package tab of the project properties:

enter image description here

Note: When you are in the new style project, you can increment package version in the Package version on the package tab.

  1. Push just created package to the NuGet repository

Just as you found, after publishing the nuget package automatically, you can add a custom target to push it:

  <Target Name="PushNuGetPackage" AfterTargets="GenerateNuspec">
    <Message Text="Push NuGet Package to NuGet Feed" Importance="high"></Message>
    <Exec Command="$(PathOfYourNuGet)\nuget.exe push $(TargetDir)\xxx.1.0.0.nupkg -Source NuGetrepository"></Exec>
  </Target>

Source Automatic NugetPackage upload to Nuget feed

Note: You can not push the nuget package with same name and same version to the nuget.org.

Piotr Falkowski
  • 1,957
  • 2
  • 16
  • 24
Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • 7
    You are not answering the question. The OP wants an *automated* method to increment the package version and then push the resulting package to the repository. *Automated* meaning not sitting at the keyboard and editing files and rolling thought multiple steps. The OP already knows how to do all this. What the OP wants is to push a button to (1) increment version (2) build (3) push. This is not either trivial or obvious and nothing in your well informed answer illuminates this path. – AQuirky Nov 13 '18 at 03:01