Use Azure #Artifacts in dotnet restore while #Docker Build

Javier Torrecilla
Jtorrecilla .NET
Published in
2 min readApr 10, 2019

This post run away from the line I want to follow, as I want to write a serie of Docker AKS and Devops articles, but in the meantime I will show a problem I found this week.

The problem

Currently I am building an application based on a Microservices Architecture.

I have a very basic structure with Web Apis built with .NET Core with their associated Dockerfiles and I am defining the CI processes in Azure Devops.

We are using Azure Artifacts in the company where we store some packages in order to share them in projects, and my Api use that private feed.

Let me share the initial Dockerfile and the azure-pipeline.yml.

If we run a docker build with the previous file (in our local environment or in our Azure Build Pipeline) we obtain a 401 error due to we didn’t set up the Azure Arifacts feed.

Doing a small research I get into the following choices:

Solution(1) Did not work

Looks like the most promising one and the cleanest, using Azure Artifact Credentials.

Steps for this version:

  1. Modify Our docker file including Args to consume the Feed endpoint and a PAT (Personal Access Token) previously defined in Azure Devops with access to the Package Management.
  2. Modify the docker file to download and run the sh for set up the Nuget Credential Provider, and define the environment variables: NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED and VSS_NUGET_EXTERNAL_FEED_ENDPOINTS. The last one is a json where will set up the feed and the PAT.
  3. In the Azure Pipeline set up a variable group inside the Library, where we will store the Artifact Feed and the PAT, and will allow access to all Build processes.
  4. Modyfy the build definition yml to add parameters to the build definition adding the variables from the group.

All done, and still doesn’t work.

Solución (2) It works!

After the previous failure, I found another alternative that does not seem clearer than previous, but It works.

This solution is based on building a Nuget.Config file on the fly consuming the variables defined in the build process.

Let’s take a look on the Docker file and the yml, and expain them:

We have 3 arguments defined: ARTIFACTS_ENDPOINT, ACCESS_TOKEN and USER. We will use this args to setup the Credentials for our Feed.

Before running the “dotnet restore” command, and in the same work directory, we run an Echo command redirecting to the Nuget.Config file.

As you can see in the file we interpolate the arguments in the file.

Let’s take a look now in the CI yml file:

We allow the use of the group variable defined in the library, and like in the solution 1 we pass the varibles to the docker build process.

If we run the build now, we get a Success!

--

--

Javier Torrecilla
Jtorrecilla .NET

Geek, Map 2010, MCC 2011, MVP C# 2011/12 Msdn Forum MOD