简体   繁体   中英

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$

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

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://docs.microsoft.com/en-us/azure/devops/pipelines/library/service-endpoints?view=azure-devops&tabs=yaml#sep-nuget

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM