DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

Azure DevOps YAML pipeline: Use Artifact as NuGet feed

Azure DevOps Artifacts is one of my favorite features which works as private feed.

Scenario

Whenever you have some sharable modules, the best way to share them, in .NET world, is to use NuGet. However, if you cannot or don't want to upload the package to public NuGet server, then you need to use private NuGet server.

Azure DevOps Artifacts works as private NuGet server!! (and npm, pip, etc.)

How to use it

To illustrate how to use it, I created two sample solution with two projects. One for shared module and another to consume it.

Alt Text

At this moment, I am referencing shared project directly,

Create Feed

Next, go to Azure DevOps project and create feed.

1. Go to Artifact and click "Create Feed".

Alt Text

2. Give any name and scope to create feed. It could be project scope or organization scope.

That's it! Simple, isn't it?

Create pipeline to push package

Time to write first pipeline for shared module. The yaml below build the shared project, pack it, then push to newly created feed.

trigger:
  branches:
    include:
    - master
  paths:
    exclude:
      - ArtifactSample/*
pool:
  vmImage: 'ubuntu-latest'

variables:
  NugetVersion: 1.0.$(Build.BuildId)

steps:
- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
- task: NuGetCommand@2
  inputs:
    command: 'pack'
    packagesToPack: '**/SharedLibrary.csproj'
    versioningScheme: 'byEnvVar'
    versionEnvVar: 'NugetVersion'
- task: NuGetCommand@2
  inputs:
    command: 'push'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
    nuGetFeedType: 'internal'
    publishVstsFeed: 'd7c15b03-b716-41bd-9d61-4d01ca7d0c21/43af5651-da68-4b39-ae90-dd5ba3638a22'

The great part is that I don't need to know the feed GUID at the bottom of the yaml, but I can select it via GUI by using NuGet task.

Alt Text

When I run the pipeline, it push the package to MyFeed.

Alt Text

Consume the package in Visual Studio

Let's use it in Visual Studio then.

1. Click "Connect to feed" button in Artifact and select Visual Studio.

Alt Text

2. Come back to Visual Studio and open Manage NuGet package window. Click small gear icon to configure the feed.

Alt Text

3. Add new feed by using the address you obtained in step 1. Select the feed and you can find the package.

Alt Text

4. Add the package, remove project reference and compile.

Alt Text

Build pipeline

Finally, add another pipeline for main program after you push all the changes into repo. The yaml is simple enough but one important part is that you need to restore packages by specifying your feed.

trigger:
  branches:
    include:
    - master
  paths:
    exclude:
      - SharedLibrary/*

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '**/*.sln'
    feedsToUse: 'select'
    vstsFeed: 'd7c15b03-b716-41bd-9d61-4d01ca7d0c21/43af5651-da68-4b39-ae90-dd5ba3638a22'
- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    projects: '**/*ArtifactSample.csproj'

When you run the pipeline, you can see the packages restored from the feed.

Alt Text

Summary

It's super simple and straight forward if you know NuGet task. Please try to use it and let me know if it's useful or not!

Top comments (0)