5

We have dotnet core application with Nuget.config file . As part of azure devops CI pipeline, we want to read and replace environment variables in Nuget.config file. Environment/Pipeline variables(USERNAME and PWD) are configured in "Build Docker Image" Task in CI pipeline. The azure devops CI build server runs on Ubuntu machine.

Environment variables in MAC/Linux should be replaced in following way as per this documentation.

The syntax "$USERNAME" in Nuget.config is not working here. When we replace static username and password, it works fine.

Nuget.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="abcFeed" value="https://somedevops.com/xyz/packaging/someApps/nuget/v3/index.json" />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
  <packageSourceCredentials>
    <abcFeed>
      <add key="Username" value="$USERNAME"/>
      <add key="ClearTextPassword" value="$PWD"/>
    </abcFeed>
  </packageSourceCredentials>
</configuration>

We have tried below syntax with following documentation, none of them are working: $USERNAME, $(USERNAME), %USERNAME%, $USERNAME$

What is correct syntax or way of replacing environment variables in Nuget.config file with azure devops pipeline?

James Z
  • 12,209
  • 10
  • 24
  • 44
Kiran H
  • 85
  • 1
  • 6
  • Why would you do this as opposed to creating a artifacts feed and using the built-in tasks for restoring from an artifacts feed? Or creating a NuGet service connection pointing to an external feed and restoring from that feed? – Daniel Mann Nov 11 '19 at 14:21
  • @Kiransholi Kiran Not get your response for several days, would you please share your latest information about this issue? If you have any concern, feel free to share it here. – Hugh Lin Nov 18 '19 at 01:55

2 Answers2

2

You could use a Replace Tokens task instead, and configure this to process your nuget.config file.

However, you may want to consider using a Nuget service connection as that will avoid having to save the Nuget credentials as pipeline variables and also make them more secure as they will be exposed in plain text when replaced in the config file.

For more information about using Service Connections in Azure DevOps, see the Microsoft documentation at: https://learn.microsoft.com/en-us/azure/devops/pipelines/library/service-endpoints?view=azure-devops&tabs=yaml#sep-nuget

Mun
  • 14,098
  • 11
  • 59
  • 83
1

I believe our nuget docs need correcting. Use the same characters for environment variable replacement, regardless of platform: %myPW% I tested successfully with dotnet.exe 3.0.100 on Ubuntu 18.*

Please engage with me on the tracking issue I have assigned to me: https://github.com/NuGet/Home/issues/8662

Rob Relyea
  • 401
  • 4
  • 3
  • Thank you Rob. We have Ubuntu/linux docker image with dotnet core 2.2 and targeted Build server is Unbuntu 18.* Will it work with this configuration? – Kiran H Nov 11 '19 at 18:27
  • Hello Rob, we have tried with syntax %USERNAME% and targeted build server is Ubuntu 18.* with Docker Image file, this is not working. @Rob Relyea – Kiran H Nov 12 '19 at 04:30
  • Please work with me on the issue I reference above NuGet/Home/8662. (We call a dotnet core API to get env vars...and it may be that we need to look at those docs to understand why your env vars aren't having an effect. I'll be happy to have that discussion with you...on that issue. Please add your details there.) – Rob Relyea Nov 12 '19 at 21:43