Building a Build Server – Building your first NuGet Package with Jenkins

In the previous article, titled Building a Build Server – Installing NAnt, we just did that, installed NAnt on our build server. Building on this, let’s create our first NuGet package.

Installing the NuGet command line tool

Open a Cygwin command-line prompt as an administrator and execute the following commands on the build server:

mkdir /cygdrive/c/NuGet

cd /cygdrive/c/NuGet

wget https://dist.nuget.org/win-x86-commandline/latest/nuget.exe

cd /cygdrive/c

icacls NuGet /T /Q /C /RESET

On the Jenkins management homepage, from the project page of JenkinsHelloWorld project, click on Configure. The JenkinsHelloWorld project was created in the article titled Build Server – Building your first C# Project with Jenkins.

The Build section should look like the following:

Execute NAnt build

NAnt Version

NAnt-0.92

Nant Build File

HelloLibrary/HelloLibrary.xml

Targets

build

Explaining the components of the solution

HelloLibrary.nuspec

<?xml
version=1.0encoding=utf-8?>

<package>

  <metadata>

    <id>$id$</id>

    <version>$version$</version>

    <title>$title$</title>

    <authors>$author$</authors>

    <owners>$author$</owners>

 

    <requireLicenseAcceptance>false</requireLicenseAcceptance>

    <projectUrl>http://bit.ly/2kByZ8y</projectUrl>

    <licenseUrl>http://bit.ly/2jObJEL</licenseUrl>

    <description>Library for Saying Hello.</description>

    <dependencies />

    <releaseNotes>

1.0.0:

– Say hello to my new NuGet packaged library.

    </releaseNotes>

    <copyright>Copyright 2017</copyright>

  </metadata>

</package>

The nuspec file describes how should the package from the project look like. The above is a minimalistic version of the NuGet specification file. You can add more information to the package, see the relevant part from the NuGet documentation. The variables between dollar signs, for example $id$, are coming from the assembly information. Because of this, we filled in the Description and the Company information in the assembly information.

AssemblyInfo.cs

//...

[assembly: AssemblyDescription("Hello Library for presenting
    how to create NuGet packages with Jenkins.")]

[assembly: AssemblyCompany(“Company building NuGets,
    not the nugget kind of ones.”)]

//…

HelloLibrary.xml

<?xml version=1.0encoding=utf-8?>

<project name=HelloLibraryxmlns=http://nant.sf.net/release/0.92/nant.xsd>

  <property name=msbuildvalue=C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe/>

  <property name=nugetvalue=C:\NuGet\nuget.exe/>

  <property name=repositoryvalue=C:\Shares\NuGets/>

  
 

  <target name=build>

    <exec program=${msbuild}basedir=HelloLibrary/>

    <exec program=${nuget}commandline=pack/>

    <mkdir dir=${repository}failonerror=false/>

    <copy todir=${repository}>

      <fileset>

        <include name=**/*.nupkg/>

        <exclude name=packages/**/>

      </fileset>

    </copy>

  </target>

</project>

This is the build file that we use to execute NAnt. First, we define three variables:

  • msbuild: This is the location of the MSBuild executable that we use to compile the visual studio project.
  • nuget: This is the nuget executable, that we just installed.
  • repository: This is the directory where we will put the nupkg files, that we will build now. To host your own private repository, the easiest way is to share this directory as a shared file system. You can add it as a NuGet package source in Visual Studio menu, Tools / Options / NuGet Package Manager / Package Sources.

The target part describes the build instructions that we are referring to from the Jenkins build instructions:

  • First, we execute MSBuild for the HelloLibrary C# project.
  • The next step is that we ask the nuget executable to pack up the project outputs.
  • Then, we make sure that the repository directory exists and copy every *.nupkg, except the ones that we installed via the NuGet package manager. This is narrowing it down to the NuGet package that we’ve just built.

For further information on NAnt build instructions, see
the NAnt documentation
.

In the next article, we are going to discuss how to publish our NuGet package on http://nuget.org. Please leave a comment below, or follow me on twitter or LinkedIn by using the buttons below. Also, if you find this article useful, feel free to share the link.

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