Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically publish a NuGet package built with VS2017

New in Visual Studio 2017 is the ability to generate a NuGet package on build for some target types (namely, .NET Standard 2.0 which is what I'm using).

This works great, and the .nupkg file is generated on successful build.

However, I'm not able to figure out how to get the built package automatically published to our local repository.

I already tried a post-build event of:

nuget push -Source https://my.nuget.server/nuget/ "C:\Source\MyProject\bin\Release\MyProject.1.0.0.nupkg"

But this presents 2 problems:

  1. The name of the package includes the version number and this isn't available as a post-build event variable, so I can't say, for example, nuget push $(NugetPackage). I also can't figure out a combination of macros/variables that would get me the package name effectively.
  2. The automatic NuGet packaging process occurs after the post-build event, so at the time of post-build, the package has not even been generated yet!

Microsoft has provided this kick-ass automatic NuGet packaging, but no way to push it to a local repository (or so it seems)!

Has anyone gotten this to work? Am I missing something? Is there a workaround? Is this something being worked on?

like image 438
qJake Avatar asked Dec 13 '17 20:12

qJake


2 Answers

You can automate a publish to nuget by adding the below line to your libraries csproj directly. You will need to either set an environment variable to the location of your nuget.exe, add the location to your system's PATH, or hard enter it into the Command parameter.

Personally, I prefer to download the latest version of nuget.exe and place it into the user's %userprofile%\.nuget folder. This is where nuget stores the local cache of packages, so it should be already created.

Example of publishing to a remote repository.

<Target Name="PostPackNugetDeploy" AfterTargets="Pack" Condition="'$(Configuration)' == 'Release'">
  <Exec Command="="%userprofile%\.nuget\nuget.exe push &quot;$(OutputPath)$(PackageId).$(PackageVersion).nupkg&quot; -Source &quot;$(NuGetSourceRelease)&quot; -Verbosity Detailed" />
</Target>

Example of publishing to a local repository.

<Target Name="PostPackNugetDeploy" AfterTargets="Pack" Condition="'$(Configuration)' == 'Release'">
 <Exec Command="%userprofile%\.nuget\nuget.exe add &quot;$(OutputPath)$(PackageId).$(PackageVersion).nupkg&quot; -source \\Server\Packages" />
</Target>

You can find the docs on the MSBuild targets for nuget here https://docs.microsoft.com/en-us/nuget/reference/msbuild-targets

Update

I have wrapped this into a nuget package that can be referenced within your project. It takes care of all this, all you have to do is add a property to your project.

You can fine the code and full guide here.
https://github.com/SimplerSoftware/SS.NuGet.Publish

like image 55
John C Avatar answered Nov 12 '22 10:11

John C


This feature has been requested here: Enable push from VS.

NuGet publish is generally handled using build/deployment automation software like TFS, VSTS, Jenkins, etc.

like image 21
desmondgc Avatar answered Nov 12 '22 11:11

desmondgc