Azure

Azure DevOps : Setting up and pushing nuget package

So its been a while since I posted. But thought it would be good to finally add the 2nd part of this, where this time we will look at how to create and push to Azure DevOps hosted Nuget feeds.

 

Creating a AzureDevops project

The 1st step is to create a new Azure Devops project, and from there you will need to go into the newly created project, and turn on the following

  • Repos : Which allows you to host repos in Azure Devops
  • Artifacts :  Which allows you create new Nuget feeds in Azure Devops

image

 

Once you have done that you can grab the repos remote clone details. So for a new project this may be something like this

 

image

 

So for me I then cloned the repo, and created a simple .NET Standard 2.0 library, that simply adds numbers

image

From there I simply pushed up the code to the remote repo.

 

So all good so far, we should have a new project which supports feeds and has our new code in it. Lets carry on

 

Creating a Nuget feed

So now that we have some code pushed up. How do we make it available on our own hosted Nuget feed. There are a couple of steps to perform here

 

Firstly we need to create a new Feed, which is done by going into the artifacts menu, and clicking the “Create Feed” button

 

image

 

You need to give the feed a name, lets suppose I chose “foofeed2” as the name, you should see something like this, where you will now need to go into the feed settings

 

image

 

If you click on the drop down you should see that the feed is created as “project scoped”, which means that it  belongs to the project. Until very recently this was not the case and all new feeds used to be scoped at organizational level, which effects how the build definition works. This was bug in Azure Devops. Which you can read more about in this StackOverflow which I created https://stackoverflow.com/questions/58856604/azure-devops-publish-nuget-to-hosted-feed

 

This was quite a weird bug to have but it did mean that all of a sudden for any Nuget feed you created and tried to publish it would not work. This is now fixed, and I will explain the difference between pushing to a project based NuGet feed and an Organizational one later when we discuss the build definition

 

For now you should make sure that the permissions for your need look something like this. Please forgive me but I am using a screen shot here from my actual project, where above I am just showing you what a new project will look like

 

image

 

I really do urge you to read the StackOverflow page as there is some really valuable discussions in there about scoping, and what extra permissions you need to ensure are there

 

Build Pipeline

So once we have the feed set up with the correct permissions, we can focus our attention to the build side of things.

 

This is complete source code for a build pipeline that does the following

  • Nuget restore
  • Builds
  • Package
  • Push to AzureDevop feed

 

# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

variables:
  buildConfiguration: 'Release'
  Major: '1'
  Minor: '0'
  Patch: '0'

steps:

- task: DotNetCoreCLI@2
  displayName: 'Restore'
  inputs:
    command: restore
    projects: '**/MathsLib.csproj'


- task: DotNetCoreCLI@2
  displayName: Build
  inputs:
    command: build
    projects: '**/MathsLib.csproj'
    arguments: '--configuration Release' # Update this to match your need


- task: DotNetCoreCLI@2
  inputs: 
    command: 'pack'
    projects: '**/MathsLib.csproj'
    outputDir: '$(Build.ArtifactStagingDirectory)'
    versioningScheme: 'byPrereleaseNumber'
    majorVersion: '1'
    minorVersion: '0'
    patchVersion: '0'


- task: NuGetCommand@2
  displayName: 'nuget push'
  inputs:
    command: 'push'
    feedsToUse: 'select'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
    nuGetFeedType: 'internal'
    vstsFeed: 'nugetprojects/anotherfeed'
    publishVstsFeed: 'nugetprojects/anotherfeed'
    versioningScheme: 'off'
    allowPackageConflicts: true

 

Project Scoped Feed

image

It should be noted that this is using a “project scoped” nuget feed. See these entries ‘nugetprojects/anotherfeed’ that is the syntax you need to use when using project scoped Nuget feeds.

 

Organizational Scoped Feed

image

 

In contrast to that, if you use an organizational feed you will need to ensure you use feed settings in the YAML above that just have the name of your feed so the push step would be this instead

- task: NuGetCommand@2
  displayName: 'nuget push'
  inputs:
    command: 'push'
    feedsToUse: 'select'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
    nuGetFeedType: 'internal'
    vstsFeed: 'testfeed'
    publishVstsFeed: 'testfeed'
    versioningScheme: 'off'
    allowPackageConflicts: true

As I say this was a VERY recent changed introduced in Azure Devops, and you can read more in the StackOverflow post that I refer to above

 

For now lets test this pipeline using the Project scoped feed “anotherfeed” above

image

 

Consuming the feed from a new project

With all this in place we should now be able to consume the feed from a new project, so lets see how we can so that. We can go to the feed to grab how to connect to it

 

image

Then pick the VisualStudio settings (or Nuget if you want to use nuget.config to configure your sources)

 

I’ll use VisualStudio one

 

image

 

I can then set this up in VisualStudio as a new Nuget feed

image

And search for my Nuget package (I did not create proper release so you need to  “include pre-releases”

image

 

Cool all working. So that’s it for this time