What Was I Thinking?

Follies & Foils of .NET Development
posts - 94 , comments - 351 , trackbacks - 0

Thursday, October 26, 2017

.NET Core–Push Nuget Package After Build

You can configure .NET Core to automatically push your nuget package to the package server of your choice by adding a Target to your project file.

1) If your package server requires an api key, you can set it by calling

nuget.exe SetApiKey <YourKey>

2) Add the following target to your csproj file.   This sample is configured to only fire on Release Builds.

<Target Name="PushTarget" AfterTargets="Pack" Condition=" '$(Configuration)' == 'Release'">   
   <Message Importance="High" Text="This is a test After Build Target--&gt;$(TargetPath)" />
   <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
     <Output TaskParameter="Assemblies" ItemName="AssemblyVersion" />
   <Exec Command="dotnet nuget push $(TargetDir)..\$(TargetName).$(AssemblyVersion).nupkg -s https://www.nuget.org/api/v2/package "></Exec>


Here’s a version that will ensure releases with a .0 revision number are properly pushed.

<Target Name="PushPackageTarget" AfterTargets="Pack" Condition=" '$(Configuration)' == 'Release'">
    < GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
      <Output TaskParameter="Assemblies" ItemName="AssemblyVersion" />
    < PropertyGroup>
      < vMajor>$([System.Version]::Parse(%(AssemblyVersion.Version)).Major)</vMajor>
      < vMinor>$([System.Version]::Parse(%(AssemblyVersion.Version)).Minor)</vMinor>
      < vBuild>$([System.Version]::Parse(%(AssemblyVersion.Version)).Build)</vBuild>
      < vRevision>$([System.Version]::Parse(%(AssemblyVersion.Version)).Revision)</vRevision>
    <Message Importance="High" Text="Property Group MajorVersion: $(vMajor).$(vMinor).$(vBuild)" />   
    <Exec Command="dotnet nuget push $(TargetDir)..\$(TargetName).$(vMajor).$(vMinor).$(vBuild).nupkg -s https://www.nuget.org/api/v2/package "></Exec>

Posted On Thursday, October 26, 2017 2:38 PM | Comments (0) | Filed Under [ Visual Studio ]

Powered by: