Nuget pack AND push within same batch script

The approved answer won't work any more, unfortunately. You'll need to add the "-Source" parameter.

Nuget push *.nupkg -Source https://www.nuget.org/api/v2/package [API Key Here]

You can see the GitHub issue filed around this in 2016. There is a PR outstanding to fix the docs (they still say "Not Required") but it's not yet shipped.


nuget push supports wildcards, so you could do something like this:

REM delete existing nuget packages
del *.nupkg

nuget pack MyProject.csproj

nuget push *.nupkg

this took way longer than I wanted, but I ultimately end up packing the assembly in a folder called bin\nuget_build, this way since it is in bin it will not be checked into source control. The script deletes and creates the bin\nuget_build each time so we can iterate over the contents of the directory and nuget push each file up to your nuget host. In this way, you don't need to worry about parsing the output from the pack command and will always have the correct version number).

Make sure you change:

  • the project name (find "Some.Project.Web.csproj")
  • your host publish api key (find 55555555-5555-5555-5555-555555555555)
  • and the source of the feed you're pushing to (find https://www.myget.org/F/some_company_feed/api/v2/package)

Here is the script that I check into source control and call from my build machine.

REM This file is used to package projects and publish them to MyGet
REM This file should sit in the same directory as the csproj file you want to package 
REM nuget.exe should be in a directory called ".nuget" one directory up
REM You can get nuget.exe to install by turning on nuget package restore

set config=%1
set PackageVersion=%2
if "%config%" == "" (
   set config=Debug
)

set version=
if not "%PackageVersion%" == "" (
   set version=-Version %PackageVersion%
)

set nuget=..\.nuget\nuget.exe

REM Make sure there is only one file in the package directory because we're going to push everything to myget
del /F /Q bin\nuget_build 
mkdir bin\nuget_build

REM ** Pack the Project **
REM Changing package title/id/description can be done by modifying [AssemblyTitle] and [AssemblyDescription]
REM in the AssemblyInfo.cs file in the project (see: http://stackoverflow.com/questions/22208542/nuget-pack-someproject-csproj-wont-let-me-change-title-or-description/22208543#22208543)

cmd /c %nuget% pack "Company.Project.Web.csproj" -IncludeReferencedProjects -o bin\nuget_build -p Configuration=%config% %version% 

REM ** Push the file to myget ** 
REM There should only be a single file in the 
for /f %%l in ('dir /b /s bin\nuget_build\*.nupkg') do (
    cmd /c %nuget% push %%l 55555555-5555-5555-5555-555555555555 -Source https://www.myget.org/F/some_company_feed/api/v2/package 
)