Adding Azure DevOps as a Nuget source for TeamCity

This article discusses using Azure DevOps as a Nuget source for TeamCity. This configuration was required to support differing versions of tools that supported differing NuGet versions.



Recently I’ve had a situation where I was using an older version of TeamCity, and the most recent version of Octopus deploy. With the more recent Octopus versions, Octopus is unable to consume NuGet v1 package feeds. This caused a problem as the version of TeamCity we were using only produced a v1 feed and there were no plans to upgrade as we were moving to Azure DevOps as our CI\CD platform.

Azure DevOps produces a v2 & a v3 NuGet package feed, which our copy of Octopus could already connect to.

To allow us to deploy our older products, we needed to build our packages and push them to Azure DevOps, whereby Octopus could connect, pull the package down and deploy.

In order to get TeamCity to connect to our Azure DevOps we need to configure a couple of things;

  1. Configure a Personal Access Token (PAT) in Azure DevOps.
  2. Add the NuGet package feed and credentials as a NuGet source.

Configure a Personal Access Token

  1. Log in to Azure DevOps
  2. Click your profile icon in the top right to bring down a menu
  3. Select Security
  4. Select Personal access tokens
  5. Press “+ New Token” to bring up a pop out form

Use this page, set the following settings;

  
NameAnything you want to refer to it as.
OrganisationLeave as default
Expirationcustom, set for a year
ScopesAs I was testing, I set Scopes as Full Access.

Press Create & on the page that is then displayed, copy the token and put somewhere safe for now. This token is not obtainable after you close this page.

Add NuGet package source

This can be added in two ways. The more permanent solution is to add it directly to your build machine alternatively you can register\unregister the source. The latter method is described further in the article;

Add directly to build machine

  1. Log onto your build machine
  2. Open a Command Prompt
  3. Run the following command

nuget.exe sources add -name {your feed name} -source {your feed URL} -username {anything} -password {your PAT}

Close the command window & Log out

Navigate to TeamCity

In the build steps of the project, add a “NuGet Publish” step after your NuGet package step.

This build step will push the package you built to Azure DevOps. Change the settings as follows;

  
Runner TypeNuGet Publish
Step NameAnything you want
ExecuteIf all previous steps finished successfully
NuGet.exeLatest version (at least 3.3)
PackagesThis is the path on your build server to your built nuget package.
API keyYour PAT
Package SourceUrl to your nuget feed


Alternatively, if you prefer not to have a custom package source on your build machine, you can add two further build steps to your build process;

Register the source

This step should be added just before the step added previously to push the package to Azure DevOps.;

  
Step nameAnything you want
RunExecutable with parameters
Command executableThe path to the latest version of nuget. On my install, version 4.7.1 has been added so my command looks like;%teamcity.tool.NuGet.CommandLine.4.7.1.nupkg%\tools\NuGet.exe
Command parametersThis is the most important bit, it registers your custom package URL with nuget using the PAT you obtained earlier;sources add -name {your feed name} -source {your feed URL} -username {anything, but I do use my Azure DevOps email} -password {your PAT}

Unregister the source

After we have done building, we want to remove the package source from NuGet. To do so, add a further build step after the push step;

Add “Command Line” build step

Modify the following;

  
Step nameAnything you want
RunExecutable with parameters
Command executableThe path to the latest version of nuget. On my install, version 4.7.1 has been added so my command looks like;%teamcity.tool.NuGet.CommandLine.4.7.1.nupkg%\tools\NuGet.exe
Command parametersThis is the most important bit, it unregisters your custom package URL with nuget using the PAT you obtained earlier;sources remove -name {your feed name}

Complete TeamCity build process

Once everything has been configured and you have implemented your preferred way of adding the VSTS source, you should kick a build off. Using the dashboard you should see all steps complete, once we are happy we can finish this configuration by updating Octopus.

Octopus

For this implementation, the Octopus change was just a matter of going into my deployments and modifying where the package source came from using the drop down. I didn’t need to configure the Package Source as this had already been completed. For those that may need to do this, the steps are as follows;

  1. Log into Octopus
  2. Select Library
  3. Select External Feeds
  4. Press “Add Feed”

Change the following:

  
Feed TypeNuGet Feed
NameAnything you want
URLThe URL for your nuget package feed
CredentialsThese are the credentials required to access your feed. [Check if this is a PAT]

Press Save & this connection is now available to use.

Leave a Reply

Discover more from Lewis Cornick

Subscribe now to keep reading and get access to the full archive.

Continue reading