[HOWTO] Create and Publish C# NuGet package with JetBrains TeamCity

Todays blog post shows how to set up a build configuration on a JetBrains TeamCity 9.1 server that creates and publishes a NuGet package of a C#/.NET-Project.

Build Configuration Setup

The build configuration will be responsible to create (pack) a NuGet-package out of the specified dll files and publish it to NuGet.org. To show the build configuraiton the project biz.dfch.CS.System.Utilities is used as a sample, which is available on GitHub.

Prerequisites

  • Running TeamCity 9 instance (Setup manual can be found here)
  • NuGet has to be enabled as described in the setup manual

Create Project on TeamCity

  1. Log in to your TeamCity server and go to the Administration board
  2. Click Create project from URL
    TeamCity_build-job_1
  3. Select the parent project, enter the repository URL and the repository credentials (username and password)
    c-sharp-nuget-pack-publish-config-1
  4. Define the project and build config names
    c-sharp-nuget-pack-publish-config-2
  5. Next, TeamCity will scan the source code and try to auto detect some build steps

Set up Build Steps

Ignore the auto detected build steps and navigate to the Build Steps menu entry. The release build configuration shown here consists out of 4 build steps. To create a new step click on the Add build step and configure the steps as follows:

  1. Installation of NuGet packages
    c-sharp-nuget-pack-publish-config-4
    For the installation of the NuGet packages only the solution file has to be referenced.
  2. Build sources
    c-sharp-nuget-pack-publish-config-5
    Reference the solution file and select the correct VisualStudio version.
  3. Create (pack) NuGet package
    c-sharp-nuget-pack-publish-config-6-1
    c-sharp-nuget-pack-publish-config-6-2
    In Specification files text field specify the path to the projects .nuspec file (For an example of a .nuspec-file see here). Then the output directory has to defined. In our case it refers to a system variable because it will be used in the next step as well. Its value will be set later.
  4. Publish NuGet package to NuGet.org
    c-sharp-nuget-pack-publish-config-7
    As you can see the system variable will be used to refer the .nupkg file(s) that have to be uploaded. To upload the package file(s) to NuGet.org an API-Key has to entered. The API-Key can be created in your NuGet.org account.

Set Parameter
To finish the setup of the build configuration navigate to Parameters and set the value of the output directory system property as follows
c-sharp-nuget-pack-publish-config-9

[OPTIONAL] Create a Trigger

At d-fens we use the Atlassian SourceTree to do GIT operations because Atlassian SourceTree provides integrated GitFlow support. If on every release done with GitFlow a new version of the NuGet package should be created and published a Trigger could be created as follows

  1. Navigate to Triggers
  2. Add a new Trigger
    c-sharp-nuget-pack-publish-config-8
    Trigger rule: +:root=VCSRootId;comment=Merge branch 'release.\d.\d.\d.:** (Replace the VCSRootId with the Id of the VCS root that belongs to the build configuration).
    This trigger rule will trigger the build every time the release branch gets merged into the master during the finish release of GitFlow.

IMPORTANT: Before finishing the release with GitFlow, adjust the version and the release notes in the .nuspec file and push the changes!

Now the build configuration is ready to run.

Troubleshooting

  • [push] Failed to process request. 'The specified API key is invalid or does not have permission to access the specified package.'.
    This message appears in the build log, if the package already exists on the NuGet repository. Check, if the id and the version in the projects nuspec file are correct and that they not conflict with an existing NuGet package.

  • error MSB4019: The imported project "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk
    If the build fails with this error message add /p:VisualStudioVersion=10.0 to the Command Line Parameters of the step, that builds the sources.

icon_TeamCity
The TeamCity Free Open Source License was generously provided by JetBrains

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: