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 […]
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.
- 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
- Log in to your TeamCity server and go to the Administration board
- Click Create project from URL
- Select the parent project, enter the repository URL and the repository credentials (username and password)
- Define the project and build config names
- 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:
- Installation of NuGet packages
For the installation of the NuGet packages only the solution file has to be referenced.
- Build sources
Reference the solution file and select the correct VisualStudio version.
- Create (pack) NuGet package
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.
- Publish NuGet package to NuGet.org
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.
[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
- Navigate to Triggers
- Add a new Trigger
+: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
masterduring 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.
[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.0to the Command Line Parameters of the step, that builds the sources.