How to package and publish a NuGet package to NuGet.org using Team Build 2015

In this post we are going to see how we can use Team Build 2015 to create a Nuget package from a class library project and publish it to Nuget.org.

To follow along you will need:

  1. Create a Class Library project in Visual Studio
    1. From the File menu select New / Project...
    2. Select Windows under Visual C#
    3. Select Class Library
    4. Name your project
    5. Check the check box for Add to source control
    6. Click OK
    7. If prompted by Choose Source Control dialog choose Git
      1. This works with TFVC as well. TFVC is not going away and is still a valid source control choice.
    8. Click OK
  2. Commit and push your changes
    1. From the View menu select Team Explorer
    2. Click Changes button
    3. Enter a comment
    4. Select Commit and Push
  3. Create Service Connection to Nuget
    1. Copy nuget API Key
      1. Sign in to nuget.org
      2. Click your username
      3. Copy your API Key
    2. Sign in VSO
    3. Browse to a Team Project
    4. Click the Administer Account Gear icon
    5. Select the Services tab
    6. Click New Service Endpoint
    7. Select Generic
    8. Enter Nuget.org as the Connection Name
    9. Enter https://nuget.org as the Server URL
    10. Enter your Nuget.org user name
    11. Copy and Paste your Nuget.org API Key into Password/Token Key
    12. Click OK

    With our code checked into VSO and our Service Connection made to Nuget.org we can now create our build definition.

  4. Create new Visual Studio Build
    1. Visit the Build hub of your VSO project
    2. Click the Actions link
    3. Select the Visual Studio build template
    4. Click OK
  5. Delete Publish Build Artifacts step
    1. Click x next to the Publish Build Artifacts step

    Because we are going to publish the package to Nuget.org there is no reason to publish the artifacts to VSO.

  6. Add NuGet Packager step after Visual Studio Test step to package your project file
    1. Click + Add Build step…
    2. Select NuGet Packager from the Package section
    3. Drag and drop this step under the Visual Studio Test step
    4. Click the browse button for the Path/Pattern to nuspec files
    5. Select the .csproj file
    6. Expand the Advanced section
    7. Change Configuration to Package to $(BuildConfiguration)
  7. Add NuGet Publisher step after NuGet Packager step to publish your package
    1. Click + Add Build step…
    2. Select NuGet Publisher from the Package section
    3. Drag and drop this step from the bottom to under the NuGet Packager step we added
    4. Set NuGet Server Endpoint to the Service Connection we created
  8. Set Repository Clean to true
    1. Click Repository tab
    2. Change Clean to true

    This will make sure that your nupkg files are removed from your agent. If you do not enable this you will get an error as the publish task attempts to publish previous versions to NuGet.org.

  9. Set the build configuration to Release
    1. Click the Variables tab
    2. Change debug to release for the BuildConfiguration variable
  10. Enable Continuous integration
    1. Click the Triggers tab
    2. Check the Continuous integration (CI) check box
  11. Save your build
    1. Click Save
    2. Give your build a name
    3. Click OK
  12. Queue a build
    1. Click Queue build...
    2. Click OK

    Once the build is complete you can return to your NuGet.org account and see your new package.

    Many properties of your package can be controlled by changing attributes in your project in Visual Studio. Next I will identify some of the attributes you can use.

  13. Change Version of package
    1. Open AssemblyInfo.cs file
    2. Change AssemblyVersion attribute
    3. Change AssemblyDescription attribute
    4. Commit and Push changes

    A CI build should have been triggered and the resulting package will have the version and description from the AssemblyInfo.cs file.

    If you need additional control over the creation of your package you can add a nuspec file to your.

  14. Add a nuspec file to your project.
    1. Right-click your project in Solution Explorer
    2. Select Add / New Item...
    3. Enter xml in the search box
    4. Select XML File
    5. Enter {projectName}.nuspec
      1. Replace {projectName} with the name of your project
    6. Click OK
    7. Copy and paste the text below into your file

      <?xml version="1.0"?>
      <package>
      <metadata>
      <id></id>
      <version></version>
      <title></title>
      <authors></authors>
      <owners></owners>
      <licenseUrl></licenseUrl>
      <projectUrl></projectUrl>
      <iconUrl></iconUrl>
      <requireLicenseAcceptance></requireLicenseAcceptance>
      <description></description>
      <releaseNotes></releaseNotes>
      <copyright></copyright>
      <tags></tags>
      </metadata>
      </package>

      1. You can learn more about nuspec files here
    8. Fill in the elements
    9. Save the file

    The values in your nuspec file will be used to overwrite values during the creation of your package.

Comments (2) -

  • Good walkthrough - thanks!
  • CJ
    Thank you for the walkthrough. This post should really be on the top of the search findings! I searched ages for this description. It was really not clear to me that I needed to set endpoint to generic until you pointed it out.

Add comment

Loading