Building and Publishing Nuget Packages from TeamCity to VSTS
In the current project I'm working on, we have a library that we want to share across other services and these services will be deployed independently. So the best way, in my opnion, to do it is to package this library using nuget and publish to an accessible location for the other projects to get it. We could have used the internal nuget server in TeamCity or even the one in Octopus Deploy, but we decided to publish to VSTS as both the TeamCity and Octopus servers are on promises and they are not really accessible outside their network.
Publishing a nuget package to VSTS seemed like a very easy task, but before we get to that, you might have asked: "Is there a package manager in VSTS?", "Where's this package manager that I've never seen before?". And the answer is no, there isn't package management in VSTS out-of-the-box, but you can install an extension that will let you do it. Here's the extension.
Once you install the Package Management extension, you can access it like this:
And now, how do we push our first nuget package to it? You can follow the steps described in VSTS itself to accomplish that, but that's really not my goal for this post. I want to automate this process.
Let's get to it.
1. We need to create an ApiKey and an Alternate Credential
Yes, for some weird reason, VSTS does require both the ApiKey and your credentials and since I wouldn't configure it with my Microsoft account itself, I'd advise you to create an alternate authentication credential for it.
2. Configure your Nuget feed credentials in TeamCity
Go to you Build Configuration Settings > Build Features > Add Build Feature. Now you can enter your alternate credential previously created as well as your nuget feed url.
3. Create your nuget package
In my case, I'm creating a dotnet core library and the way I want to create a package is the same way I would using the command line dotnet pack -o [OUTPUT DIR] -c Release /p:PackageVersion=[PACKAGE VERSION]
where the package version will eventually be the build version in TeamCity.
To do the same in TeamCity, you have to make sure you have installed the .NET Cli Plugin and .NET Core SDK in the build server. For more information, you can check this post.
Now you should have a new build step called .NET Core (dotnet) so you can package your dotnet core library and set the package version based on the build number
4. Finally, publish the nuget package
TeamCity has a build step specifically for publishing nuget packages. We just need to set the ApiKey (Created in the first step) and the Package Source URL which you can also copy from the first step.
You should now see your newly created and published package in your feed in VSTS.
Hope it helps.
Cheers