Publish NuGet packages with Jenkins

TFS 2017

With Azure Artifacts, you can leverage a variety of build and deployment automation tools such as Maven, Gradle, and Jenkins. This article will walk you through creating and publishing NuGet packages using Jenkins.

Prerequisites

Jenkins Setup

This walkthrough uses the latest Jenkins running on Windows 10. Ensure the following Jenkins plugins are enabled:

Some of these plugins are enabled by default, others you will need to install by using the Jenkins's "Manage Plugins" feature.

The sample project

We will be using a C# class library sample project for this article.

  1. In Visual Studio, create a new project, and then select the C# Class Library template.

  2. Name your solution FabrikamLibrary.

  3. Open your solution and then right click on the project and select Properties.

  4. Select Package and then fill out the description, product, and company fields.

  5. Select Save when you are done.

  6. Check the new solution into a Git repository where your Jenkins server can access it later.

Screenshot showing how to configure the package properties for a class library project.

Create a NuGet package

  1. Open a new command prompt window navigate to your project directory.

  2. Run the nuget spec command to create a nuspec file.

  3. Open the newly created FabrikamLibrary.nuspec and remove the boilerplate tags projectUrl and iconUrl. Add an author inside the authors tag, and then change the tags from Tag1 Tag2 to fabrikam.

  4. Save your file when you are done.

  5. Run the following command to create a NuGet package: nuget pack FabrikamLibrary.csproj.

  6. A NuGet package FabrikamLibrary.1.0.0.nupkg will be generated in the same directory as your .csproj file.

Connect to feed

  1. From within your project, select Artifacts, and then select your feed. You can create a new feed if you don't have one already.

  2. Select Connect to feed.

    Screenshot showing how to connect to your feed

  3. Select NuGet.exe under the NuGet header.

    Screenshot showing the NuGet.exe feed connection

  4. If this is the first time using Azure Artifacts with Nuget.exe, select Get the tools button and follow the instructions to install the prerequisites.

    1. Download the latest NuGet version.
    2. Download and install the Azure Artifacts Credential Provider.
  5. Follow the instructions in the Project setup to connect to your feed.

    Screenshot showing the project setup section

Create a build pipeline in Jenkins

  1. From your Jenkins dashboard, select Create a job.

  2. Enter an item name, and then select Freestyle project. Select OK when you are done.

    Screenshot showing how to create a freestyle project in Jenkins

  3. Select Source Code Management, and then select Git. Enter your Git repository and select the branches to build.

  4. Select Build Environment, and then select Use secret text(s) or file(s).

  5. Select Add, and then select Username and password (separated).

  6. Set the Username Variable to "FEEDUSER" and the Password Variable to "FEEDPASS". Select Add when you are done.

    Screenshot showing how to set up build environment in jenkins.

  7. Set the Username to "token" and the Password to the personal access token PAT you generated earlier. Select Add to save your credentials.

    Screenshot showing how to add your credentials.

  8. Select Build > Add build step., and then select Execute Windows batch command. Enter your batch command in the command box.

  9. Select Save and then queue your build.

Publish a package using Jenkins

  1. Select your build pipeline, and then select Configure to edit your build definition.

  2. Select Build, and then select Add build step to add a new task.

  3. Select Execute a Windows batch command and enter the following commands in the command box:

    .tools\VSS.NuGet\nuget sources update -Name <YOUR_FEED_NAME> -UserName "%FEEDUSER%" -Password "%FEEDPASS%"
    .tools\VSS.NuGet\nuget push *.nupkg -Name <YOUR_FEED_NAME> -ApiKey key
    
  4. Select Save, and then queue your build. Your NuGet package should be published to your Azure Artifacts feed.