Building a Build Server – Publishing NuGet Packages to nuget.org

In the previous article, titled Building a Build Server – Building your first NuGet Package with Jenkins, we built a dummy C# library and packaged it into a NuGet package. After having created the NuGet package, we published it to our private NuGet package repository, which was basically a shared directory. There are more sophisticated ways to build a private NuGet server. On the other hand, if you would like to distribute your packages to wide audience then you can make use of the public package repository at https://nuget.org. In this article, we are going to add a build configuration to our Jenkins server to publish a previously built NuGet package.

To be able to publish the NuGet package, the first thing that we need to configure is to archive the build output of our build project. This will allow the other build project to grab the artifact as an input. Log on into your Jenkins administration website, and on the JenkinsHelloWorld project page click on Configure. In the Post-build Actions section add the following:

Archive the artifacts

Files to archive

HelloLibrary/*.nupkg

 

On the JenkinsHelloWorld build project page, click on Build Now, so the build project is completed with the newly added archive artifacts post-build action. If everything went right, after completing the build, on the project home page you are going to see a Last Successful Artifacts section below the Workspace link, listing the newly built NuGet package. You might have to refresh the browser page after the build to see the Artifacts section.

To authenticate your user on https://nuget.org, you are going to need the following things:

  1. A user on https://nuget.org. For this, go to the site and click on the Register / Sign in link under the search bar. Choose the option you prefer and complete the process.
  2. A nuget.org API key. This can be obtained after you logged in into the website and visited your user page. The API Keys section allows you to generate new keys, remove old ones and copy the key to your clipboard.
  3. In the example usage section, when you reveal the API Keys section, you will see the similar lines to the below two. You will need the URL after the -Source switch, that is the web API address for package publication.

nuget.exe setApiKey [your API key]

nuget.exe push MyPackage.1.0.nupkg -Source https://www.nuget.org/api/v2/package

 

On the Build Server side, we will need to add the Nuget Plugin Jenkins plugin. To complete this, go to your Jenkins management homepage, and then select Manage Jenkins / Manage Plugins. In the Available section look for the Nuget Plugin and the Copy Artifact Plugin and install them. After installing the new plugin, when Jenkins restarted, from the main Jenkins home page go to Manage Jenkins / Configure System. In the Nuget section, provide the following configuration:

Nuget

NuGet command line executable

C:\NuGet\nuget.exe

Name

NuGet.org

Server Url

Web API address (see above information block on where to acquire)

Api Key

Your API Key

 

After configuring the plugin, create a new build project called Hello nuget.org by selecting the New Item menu item on the main Jenkins page. Define the project as a Freestyle project and click OK.

Use the following build instructions:

Copy artifacts from another project

Project name

JenkinsHelloWorld

Which build

Last successful build

 

(Yes) Stable build only

Artifacts to copy

HelloLibrary/*.nupkg

 

Use the following post-build actions:

Publish NuGet packages

Name

HelloLibrary

Publication

NuGet.org

Packages to publish

HelloLibrary/*.nupkg

 

After you have the JenkinsHelloWorld project built, building this project will publish your package. Be aware that once you publish a package, you cannot republish it with the same package version, because that is not allowed on https://nuget.org. Also, you are not allowed to remove packages, because somebody might depend on it, so always consider what package you publish before doing so, as it is hard to remove it if you decide that you did not want to publish it. Next time, we are going to start to explore creating documentation for our software code and integrating it into build process. Please follow me on LinkedIn or Twitter by using the icons below and share the article if you find it useful.

By subscribing to the email list, you will be notified about new posts.
Loading
No comments yet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Powered by WordPress. Designed by WooThemes