Push a NuGet package to VSTS with .NET CLI

Jakub Binkowski picture Jakub Binkowski · Jan 2, 2018 · Viewed 8k times · Source

I'm trying to publish a NuGet package to a private VSTS feed. I'd like to achieve that using only .NET CLI and without creating or modifying a nuget.config file.

I've tried to do:

dotnet nuget push <PackageName> --source https://XXX.pkgs.visualstudio.com/_packaging/YYY/nuget/v3/index.json --api-key <VSTS UserName>:<PersonalAccessToken>

I get: error: Unable to load the service index for source https://XXX.pkgs.visualstudio.com/_packaging/YYY/nuget/v3/index.json. error: Response status code does not indicate success: 401 (Unauthorized).

I can see in Fiddler that .NET CLI sends only a GET request to https://XXX.pkgs.visualstudio.com/_packaging/YYY/nuget/v3/index.json without any authorization token. That request ends with 401.

Answer

Marina Liu picture Marina Liu · Jan 3, 2018

The NuGet package credential and API key should be added in the NuGet.config file.

So before using the dotnet nuget push command, you should add the credential and API key in NuGet.config as below two commands:

nuget sources Add -Name "mysource" -Source "https://XXX.pkgs.visualstudio.com/_packaging/YYY/nuget/v3/index.json" -username name -password PAT
nuget setapikey mykey -source mysource

Then push the NuGet package through the dotnet nuget push command:

dotnet nuget push packagename.nupkg --source mysource --api-key mykey