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
- Install NuGet CLI.
- A personal access token to authenticate your feed.
- Azure DevOps Services account.
- Azure Artifacts feed.
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.
In Visual Studio, create a new project, and then select the C# Class Library template.
Name your solution FabrikamLibrary.
Open your solution and then right click on the project and select Properties.
Select Package and then fill out the description, product, and company fields.
Select Save when you are done.
Check the new solution into a Git repository where your Jenkins server can access it later.
Create a NuGet package
Open a new command prompt window navigate to your project directory.
Run the
nuget spec
command to create a nuspec file.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.
Save your file when you are done.
Run the following command to create a NuGet package:
nuget pack FabrikamLibrary.csproj
.A NuGet package FabrikamLibrary.1.0.0.nupkg will be generated in the same directory as your .csproj file.
Connect to feed
From within your project, select Artifacts, and then select your feed. You can create a new feed if you don't have one already.
Select Connect to feed.
Select NuGet.exe under the NuGet header.
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.
- Download the latest NuGet version.
- Download and install the Azure Artifacts Credential Provider.
Follow the instructions in the Project setup to connect to your feed.
Create a build pipeline in Jenkins
From your Jenkins dashboard, select Create a job.
Enter an item name, and then select Freestyle project. Select OK when you are done.
Select Source Code Management, and then select Git. Enter your Git repository and select the branches to build.
Select Build Environment, and then select Use secret text(s) or file(s).
Select Add, and then select Username and password (separated).
Set the Username Variable to "FEEDUSER" and the Password Variable to "FEEDPASS". Select Add when you are done.
Set the Username to "token" and the Password to the personal access token PAT you generated earlier. Select Add to save your credentials.
Select Build > Add build step., and then select Execute Windows batch command. Enter your batch command in the command box.
Select Save and then queue your build.
Publish a package using Jenkins
Select your build pipeline, and then select Configure to edit your build definition.
Select Build, and then select Add build step to add a new task.
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
Select Save, and then queue your build. Your NuGet package should be published to your Azure Artifacts feed.