16

I'm having a heck of time writing a batch script to both pack and push the resulting .nupkg file up to my private nuget feed. I can call pack just fine, but I don't know how to parse the name of the nupkg out of the resulting output in order to do the push. Unfortunately, the version number auto-increments all the time, so I can't just make an assumption of what the resulting file would be named.

FWIW, I'm using myget.org for my private hosting, but this is could easily be applied to any host (including nuget.org).

viggity
  • 15,039
  • 7
  • 88
  • 96
  • Excuse me. It seems that you have prepared both the question _and_ the answer in advance and posted both at the same time. Although this type of questions/answers are sometimes suspicious (if you already know the answer, why post the question?) there is another point in this case: your question is NOT a _real_ question! It have too many obscure points that nobody could answer it, so it is amazing to read the number of details that the answer have until we realize that the answerer and the OP, is the same person! I suppose that the next step is that you choose your answer as Best Answer... – Aacini Mar 10 '14 at 23:42
  • 8
    dude, there is a checkbox underneath the "post your question" submission button called "answer your own question - share your knowledge Q&A-style". Hell, look at this page: http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/. There is a preponderance of batch file questions on the site, as I'm trying to programatically do two things. Also, if I "accept" my own answer, I don't get any points. Do you actually have 15K rep and not know this? – viggity Mar 11 '14 at 14:30

3 Answers3

20

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
Taylor Lafrinere
  • 3,084
  • 18
  • 27
  • This is a useful script. I added it as an External Tool and hooked it up to my project context menu (see http://stackoverflow.com/questions/5605885/how-to-run-a-bat-from-inside-the-ide). This means I can just right-click on a project and have it packed and pushed to nuget. – cbp Apr 09 '15 at 01:21
  • How can I exclude: *.symbols.nupkg? – kwesolowski Jul 29 '15 at 09:42
  • This doesn't work any longer, as the "-Source" parameter is required circa 2016. – Chris M. Mar 04 '19 at 20:42
  • @kwesolowski for excluding "symbols.nupkg": `for /f %%l in ('dir /b /s bin\nuget_build\*.nupkg ^| findstr /e /i /v /c:"symbols.nupkg"') do ( nuget push %%l -src NServer -SymbolSource NSources )` Sorry. I couldn't format the code. – Drakosha Sep 03 '21 at 12:47
2

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:

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 
)
viggity
  • 15,039
  • 7
  • 88
  • 96
2

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.

Chris M.
  • 1,731
  • 14
  • 15