Nuget pack AND push within same batch script

2020-08-10 07:28发布

问题:

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).

回答1:

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


回答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:

  • 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 
)


回答3:

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.