Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Create Jenkins Job for Creating NuGet Packages

5.00/5 (7 votes)
18 Feb 2015CPOL3 min read 22.4K  
If you are creating .NET libraries, most probably you need to create NuGet packages to distribute your code. You can make your life easier if you create an automatic Jenkins Job for the purpose.

In my previous article, “Getting Started with Jenkins and GitHub”, I showed you how to create your first Jenkins job and download the source of your project from GitHub.

If your projects are stored in TFS, you can read how to setup your Jenkins job for TFS here: “Integrate Jenkins with Team Foundation Server“.

In order to build your project with MSBuild, follow the following guide: Integrate Jenkins with MSBuild and NuGet.

If you are creating .NET libraries, most probably you need to create NuGet packages to distribute your code. You can make your life easier if you create an automatic Jenkins Job for the purpose.

Steps to Create a Jenkins Job for Creating NuGet Packages

  1. Create a new Jenkins Job
  2. Configure Source Control Section
  3. Create NuGet Restore Step if you are using NuGet packages
  4. Configure MSBuild Build Step

    Next, you need to download the NuGet.exe which you are going to use for the operation. The newest versions don’t support the restore command so you need to download a version that supports it. My preferred for the moment is 2.81.

    As you know, every NuGet package has a specific version. SemVer is a convention for versioning your public APIs in which the version number has meaning attached to it. Each version has three parts, Major.Minor.Patch. In brief, these correspond to * Major: Breaking changes. * Minor: New features, but backwards compatible. * Patch: Backwards compatible bug fixes only. Every package you build should have a higher version in order to be shown in the update wizard.

    Additionally, prerelease versions of your API can be denoted by appending an arbitrary string to the Patch number separated by a dash. For example:

    • 1.0.1-alpha
    • 1.0.1-beta
    • 1.0.1-Fizzleshnizzle
  5. In order to pass the NuGet version to your Jenkins Job, create a parameterized Job. Click This build is parameterized checkbox.

    Next, click Add Parameter button and add five string parameters- MajorVersion, MinorVersion, PatchVersion, PrereleaseString and DeployLocation.

  6. Next, create a new Windows batch command and paste a modified command like the one below:
    C:\J\Nuget\NuGet_2.81.exe pack "%WORKSPACE%\PhantomTube\PhantomTube.Core\PhantomTube.Core.csproj" 
    -IncludeReferencedProjects  -Version %MajorVersion%.%MinorVersion%.%PatchVersion%%PrereleaseString% 
    -Properties Configuration=Release

    Explanations of the Command

    • pack – command-line switch which creates NuGet package
    • -IncludeReferencedProjects – tells NuGet.exe to include all referenced project’s DLLs to the package and all referenced packages as dependencies
    • -Version %MajorVersion%.%MinorVersion%.%PatchVersion%%PrereleaseString% – specifies the exact version of the new NuGet package. With %YourParameter%, you can access your already defined parameters in Windows batch commands.
    • -Properties Configuration=Release – tells NuGet to build the package in Release configuration. The default one is Debug.

    The package will be created in the default Jenkins Job workspace.

  7. Usually, you don’t have access to the Jenkins Server because of that you might want the package to be moved to another shared location or directory.

    Create a new Windows batch command and use the command below:

    move  %WORKSPACE%\PhantomTube.Core.%MajorVersion%.%MinorVersion%.%PatchVersion%%PrereleaseString%.nupkg 
     %DeployLocation%

    The above line is going to cut the created package to the deploy location that the user is going to specify in the DeployLocation parameter.

    Note: Be sure that the user with whom the Jenkins Windows Service is logged-in by default has the right read/write permission to the shared directory.

  8. Now you are ready to test your setup. Open your job from the link listed on the main Jenkins page. Click the Build Now button.

    Click Build and open the console log.

You can find more information about the NuGet.exe console-line switches here.

The post - Create Jenkins Job for Creating NuGet Packages appeared first on Automate The Planet.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)