Push a NuGet package to VSTS with .NET CLI

10,148

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
Share:
10,148
Jakub Binkowski
Author by

Jakub Binkowski

Updated on June 23, 2022

Comments

  • Jakub Binkowski
    Jakub Binkowski almost 2 years

    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.

  • Jakub Binkowski
    Jakub Binkowski over 6 years
    Thanks. I was looking for an option that: a) doesn't require to store the keys in NuGet config as I wanted to check in the file to repository b) doesn't require nuget.exe But I guess, it can't be done (yet?)
  • Marina Liu
    Marina Liu over 6 years
    Yeah, it can not be done. The credentials should be searched in nuget.config file as the description docs.microsoft.com/en-us/dotnet/core/tools/dotnet-nuget-push says: "The dotnet nuget push command pushes a package to the server and publishes it. The push command uses server and credential details found in the system's NuGet config file or chain of config files. "
  • Alex Mullans
    Alex Mullans over 6 years
    Note that you won't need either the setapikey line or the --api-key parameter when pushing to VSTS Package Management. Otherwise though, the answer is correct. You can manually construct a NuGet.config without using nuget.exe, if you'd like. There's a reference here that can help: docs.microsoft.com/en-us/nuget/schema/…
  • Andre Soares
    Andre Soares about 6 years
    You have used mykey and PAT. I suppose they do not represent the same value. How then can I generate this mykey?
  • KimCM
    KimCM almost 6 years
    @Andre Soares: Yes, mykey and PAT do not represent the same value: "The NuGet client's push command requires an API key. [With VSTS Package Server] You can use any non-empty string you want". Source: docs.microsoft.com/en-us/vsts/package/nuget/publish?view=vst‌​s
  • Konrad
    Konrad almost 6 years
    @MarinaLiu-MSFT is nuget setapikey mykey needed? when you use --api-key mykey
  • Konrad
    Konrad almost 6 years
    "Saves an API key for a given server URL into NuGet.Config so that it doesn't need to be entered for subsequent commands."
  • Konrad
    Konrad almost 6 years
    It would mean that I don't need to use --api-key mykey then
  • Konrad
    Konrad almost 6 years
    oh nevermind I didn't read all comments, it was answered by Alex
  • christopher clark
    christopher clark over 5 years
    I'm convinced none of microsofts shitty vsts tasks work for nuget. Every avenue we try there are issues.
  • Ricardo stands with Ukraine
    Ricardo stands with Ukraine about 3 years
    In my case had to push a package to GitHub Package Registry so did: nuget setapikey <github-token-with-repo-permissions> -source github. It added some gibberish to %appdata%\NuGet\NuGet.Config and then could push the package.