title | description | ms.assetid | ms.service | ms.custom | ms.topic | ms.date | monikerRange | recommendations |
---|---|---|---|---|---|---|---|---|
Publish and restore NuGet packages with dotnet CLI |
How to connect to a feed and use the dotnet CLI to publish and restore NuGet packages. |
CA2DCB9D-93FB-4E2D-B953-BF78D5687B35 |
azure-devops-artifacts |
devx-track-dotnet |
conceptual |
04/17/2024 |
>= azure-devops-2020 |
true |
[!INCLUDE version-lt-eq-azure-devops]
Azure Artifacts enables you to publish and restore your NuGet packages to and from your feed, allowing you to share them with others according to your feed's visibility settings. This guide walks you through configuring your project to publish or restore packages using the dotnet command-line interface.
-
An Azure DevOps organization and a project. Create an organization or a project if you haven't already.
-
An Azure Artifacts feed. Create a new feed if you don't have one already.
-
Download and install the Azure Artifacts Credential Provider.
-
Download and install .NET Core SDK (2.1.400+).
::: moniker range="azure-devops"
-
Select Artifacts, and then select your feed from the dropdown menu.
-
Select Connect to feed, and then select dotnet from the NuGet section on the left.
-
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, replacing the placeholders with the relevant information:
-
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>
::: moniker-end
::: moniker range="azure-devops-2020 || azure-devops-2022"
-
Sign in to your Azure DevOps server, and then navigate to your project.
-
Select Artifacts, and then select your feed.
-
Select Connect to Feed, and then select dotnet from the left navigation pane.
-
Follow the instructions in the Project setup section to connect to your feed.
:::image type="content" source="../media/connect-to-feed-dotnet-server-2020-and-2022.png" alt-text="A screenshot showing how to connect to a feed with dotnet in Azure DevOps Server 2020 and 2022." lightbox="../media/connect-to-feed-dotnet-server-2020-and-2022.png":::
Note
dotnet is not supported in Azure DevOps Server 2019.
::: moniker-end
Run the following command to publish a package to your feed. Replace the placeholders with the appropriate information:
dotnet nuget push --source https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json --api-key <ANY_STRING> <PACKAGE_PATH>
Example: dotnet nuget push --source https://pkgs.dev.azure.com/MyOrg/MyProject/_packaging/MyFeed/nuget/v3/index.json --api-key AZ bin/MyPackage.5.0.2.nupkg
Note
The api-key
is only used as a placeholder.
-
Create a personal access token (PAT) with packaging read and write scope.
-
Replace the <PERSONAL_ACCESS_TOKEN> placeholder with your personal access token, and then run the following command to add your package source to your nuget.config file. This adds your PAT to your nuget.config. Make sure to store this file securely and not check it 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>
-
Publish your package:
dotnet nuget push --source <SOURCE_NAME> --api-key <ANY_STRING> <PACKAGE_PATH>
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 --source MySource --api-key AZ nupkgs/mypackage.1.1.0.nupkg
Note
If your organization is using a firewall or a proxy server, make sure you allow the Azure Artifacts Domain URLs and IP addresses.
Run the following command to restore your packages. The --interactive
flag is used to prompt the user for credentials:
dotnet restore --interactive