Azure DevOps YAML pipeline: Use Artifact as NuGet feed
Tags: azuredevops, artifact, nuget
Published: 2020-08-14 07:26:28

[Azure DevOps Artifacts](https://azure.microsoft.com/en-us/services/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](https://dev-to-uploads.s3.amazonaws.com/i/jw1d21edpvgtgiy8gz2d.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/rwxkhkofmfhh49ygzh70.png)

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.

```yaml
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](https://dev-to-uploads.s3.amazonaws.com/i/1m1pb7s14pyu3vabmab5.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/f93eld4gm4lha00vbc3d.png)

# 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](https://dev-to-uploads.s3.amazonaws.com/i/2d2mh2eox28cy9lus6rt.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/s7ielum3y5dnyewrkdqd.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lcjrdev26b3b90q66et5.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fptrh1phsgc3tpp5t2zo.png)

# 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.

```yaml
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](https://dev-to-uploads.s3.amazonaws.com/i/amnehjndmyii1uu34pkt.png)

# 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!