Monday, June 27, 2011

Automating NuGet Packing and Publishing with MSBuild

We’ve setup a NuGet Gallery and now we wanted to have our builds automatically package and publish to the NuGet Gallery.

The below is more or less the step-by-step process to get it up and running in an automated way.

Step 1 Create your NuSpec for your .csproj. The documentation on NuGet documents is pretty good for this step.

The gist is you will need to have the .nuspec file in the same directory as the .csproj file. You will have to modify this file and ensure you’ve populated all the correct tags as mentioned in the NuGet documents.

Note: An error message will appear when trying to package if something is missing.

Step 2 Modify your .csproj file and add the below snippets. They are generically written so they should fit right into your msbuild file fine. Obviously, edit when appropriate should your msbuild file already be customized.

Edit: Make sure you update the My_Nuget_Gallery_Service_Url token in the script to point to your gallery.

Note: You will need to run nuget setApiKey before this will work as it relies on the API Key for the NuGet Gallery already to be stored locally. This is handy as you can login as your Build Service Identity and run the command. It will then publish as that user from then on out.

Step 3 Run it via MSBUILD.

All you need to do on your build sever is execute the below command:

Edit: Make sure you update the tokens to be the correct values for your project.

4 comments:

korifrancis said...

Hmm, I can't seem to get this to work in a CI build environment using TFS.

I keep getting the error "TestProject.csproj (175): Cannot get assembly name for "bin\TestProject.dll" ... The system cannot find the path specified. Line 175 in my project is a line from your snippet, specifically:



Any suggestions?

Kori (@djbyter, http://scr.im/byter)

Richard Adleta said...

Kori,

Your comment was clipped and I cannot see the line you mention from my snippet. Although, my guess is the build hasn't run, yet. This would mean the dll doesn't exist yet on your build server.

I'm making a big assumption, based on what you posted. Did you add the snippets I posted to your .csproj file? This was meant to be embedded within it. Also, when you execute the msbuild.exe the /t argument is telling it the Build target needs to run prior to the Package and Publish. This assumes you've embedded the snippet within your .csproj.

Let me know if this helps. If it doesn't give me a few more details and I'll see what I can do to help.

Richard Adleta

James said...

Any chance you could post a functioning code example? When I try to implement this in my project, it tries to run the Exec twice. The first time succeeds but does not specify version information, the second time it fails (using the wrong path to NuGet.exe for some reason) but passes the version information successfully.

Richard Adleta said...

James,

What does the output of the below commend produce when you execute?

<Message Text="Info: %(OutputAssemblyInfo.Version)" />

You should see something like:

Info: Myassembly.dll

My guess is your seeing something like MyAssembly.dll;SomethingElse.

Richard

There was an error in this gadget