Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment Variable replacement in Nuget.config

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$

  • https://learn.microsoft.com/en-us/nuget/reference/nuget-config-file#example-config-file

  • https://intercom.help/appcenter/en/articles/2216043-restore-private-azuredevops-package-management-feeds-using-pat.

  • Setting an environment variable in a NuGet.Config file

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

like image 460
Kiran H Avatar asked Oct 16 '22 09:10

Kiran H


1 Answers

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

like image 134
Mun Avatar answered Oct 19 '22 00:10

Mun