Publish and restore NuGet packages from the command line (dotnet)

Azure DevOps Services | Azure DevOps Server 2022 | Azure DevOps Server 2020 | Azure DevOps Server 2019 | TFS 2018

With Azure Artifacts, you can publish and restore your NuGet packages to/from your feed and share them with others based on your feed's visibility settings. This article will guide you through setting up your project to publish and restore your packages using the dotnet command-line interface.

Prerequisites

Connect to feed

  1. Select Artifacts, and then select your feed from the dropdown menu.

  2. Select Connect to feed.

    Screenshot showing how to connect to a feed.

  3. Select dotnet from the NuGet section.

  4. Create a nuget.config file in the same folder as your .csproj or .sln file. Copy the following XML snippet and paste it into your new file:

  • Organization-scoped feed:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <clear />
        <add key="<FEED_NAME>" value="https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json" />
      </packageSources>
    </configuration>
    
  • Project-scoped feed:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <clear />
        <add key="<FEED_NAME>" value="https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json" />
      </packageSources>
    </configuration>
    
  1. Select Build and Release > Packages.

  2. Select your feed from the dropdown menu or create one if you haven't.

  3. Select Connect to feed.

    A screenshot showing how to connect to a feed in TFS.

  4. Select NuGet and follow the instruction to connect to your feed.

Publish packages

To publish a package to your feed, run the following command in an elevated command prompt. Replace the placeholders with the appropriate information:

dotnet nuget push <PACKAGE_PATH> --source https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json --api-key <ANY_STRING>

Note

The api-key is only used as a placeholder.

  • Example:

    dotnet nuget push MyPackage.5.0.2.nupkg --source https://pkgs.dev.azure.com/MyOrg/MyProject/_packaging/MyFeed/nuget/v3/index.json --api-key AZ
    

Publish packages from external sources

  1. Create a personal access token (PAT) with packaging read and write scope.

  2. Add your package source to your nuget.config file. This will add your PAT to your nuget.config file. Make sure to store this file in a safe place, and do not check this file into source control.

    dotnet nuget add source https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json --name <SOURCE_NAME> --username <USER_NAME> --password <PERSONAL_ACCESS_TOKEN> --configfile <PATH_TO_NUGET_CONFIG_FILE>
    
  3. Publish your package:

    dotnet nuget push <PACKAGE_PATH> --source <SOURCE_NAME> --api-key <ANY_STRING>
    
  • Example:

    dotnet nuget add source https://pkgs.dev.azure.com/MyOrg/MyProject/_packaging/MyFeed/nuget/v3/index.json --name MySource --username MyUserName --password MyPersonalAccessToken --configfile ./nuget.config
    dotnet nuget push nupkgs/mypackage.1.1.0.nupkg --source MySource --api-key AZ
    

Restore packages

To restore your packages, run the following command in an elevated command prompt. The --interactive flag is used to prompt the user for credentials.

dotnet restore --interactive