Search code examples
nuget

Is there a way to instruct nuget push to use a custom config file location without passing the -ConfigFile argument?


Rationale - I am actually using Publish-Module PS function to publish a PS module. This function internally calls either nuget.exe or dotnet.exe nuget to actually push the PS module package.

Unfortunately, this function does not pass the -ConfigFile argument effectively forcing us to use the user level $env:APPDATA\NuGet\NuGet.config file.

Why the file matters? The repo requires Access Token to authenticate. I do not know any other way except record it in the NuGet.config file. If there is one that would spare the config file at all - I will gladly use it.

So I am looking for one of the following:

  • Provide a custom NuGet.Config location to NuGet.exe without -ConfigFile argument. E.g. - through a well-known environment variable.
  • Eliminate the need for NuGet.Config at all by providing the Access Token in the environment or some custom file location, but not the user level NuGet.Config.

Solution

  • I found the following solution:

    1. Create a temp directory
    2. Generate an empty (just with the root element) nuget.config there
    3. Change to that directory using both [IO.Directory]::SetCurrentDirectory and Set-Location. It is important to use both this API and the Powershell native Set-Location
    4. Call Publish-Module from that directory.