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:
nuget push $(NugetPackage)
. I also can't figure out a combination of macros/variables that would get me the package name effectively.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?
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 "$(OutputPath)$(PackageId).$(PackageVersion).nupkg" -Source "$(NuGetSourceRelease)" -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 "$(OutputPath)$(PackageId).$(PackageVersion).nupkg" -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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With