Package Management with Azure Artifacts
Estimated time: 35 minutes
You will learn how to work with Azure Artifacts for package management, including creating and connecting to a feed, creating and publishing a NuGet package, importing a NuGet package, and updating a NuGet package. Azure Artifacts facilitates discovery, installation, and publishing of NuGet, npm, and Maven packages in Azure DevOps.
Before you start
You need:
- Microsoft Edge or an Azure DevOps supported browser
- Azure DevOps organization: Create one if you don't have one
- Visual Studio 2022 Community Edition available from Visual Studio Downloads page. Installation should include:
- ASP.NET and web development workload
- Azure development workload
- .NET Core cross-platform development workload
- .NET Core SDK: Download and install the .NET Core SDK (2.1.400+)
- Azure Artifacts credential provider: Download and install the credential provider
About Azure Artifacts
Azure Artifacts facilitate discovery, installation, and publishing NuGet, npm, and Maven packages in Azure DevOps. It's deeply integrated with other Azure DevOps features such as Build, making package management a seamless part of your existing workflows.
Key benefits:
- Centralized package management across your organization
- Integration with CI/CD pipelines for automatic package publishing
- Support for multiple package types (NuGet, npm, Maven, Python, Universal packages)
- Upstream sources to proxy and cache packages from public repositories
- Security and permissions to control access to your packages
Create and configure the team project
First, you'll create an Azure DevOps project for this lab.
- In your browser, open your Azure DevOps organization
- Click New Project
- Give your project the name eShopOnWeb
- Leave other fields with defaults
- Click Create
Import the eShopOnWeb Git Repository
Next, you'll import the sample repository that contains the application code.
- In your Azure DevOps organization, open the eShopOnWeb project
- Click Repos > Files
- Click Import a Repository
- Select Import
- In the Import a Git Repository window, paste this URL:
https://github.com/MicrosoftLearning/eShopOnWeb.git
- Click Import
The repository is organized this way:
- .ado folder contains Azure DevOps YAML pipelines
- src folder contains the .NET 8 website used in the lab scenarios
- Go to Repos > Branches
- Hover on the main branch then click the ellipsis on the right
- Click Set as default branch
Configure the eShopOnWeb solution in Visual Studio
You'll configure Visual Studio to prepare for the lab.
- Ensure that you are viewing the eShopOnWeb team project on the Azure DevOps portal
Note: You can access the project page directly by navigating to the https://dev.azure.com/`
/eShopOnWeb URL, where the
` placeholder represents your Azure DevOps Organization name.
- In the vertical menu on the left side of the eShopOnWeb pane, click Repos
- On the Files pane, click Clone
- Select the drop-down arrow next to Clone in VS Code, and in the dropdown menu, select Visual Studio
- If prompted whether to proceed, click Open
- If prompted, sign in with the user account you used to set up your Azure DevOps organization
- Within the Visual Studio interface, in the Azure DevOps pop-up window, accept the default local path (C:\eShopOnWeb) and click Clone
- This will automatically import the project into Visual Studio
- Leave Visual Studio window open for use in your lab
Working with Azure Artifacts
You'll learn how to work with Azure Artifacts using the following steps:
- Create and connect to a feed
- Create and publish a NuGet package
- Import a NuGet package
- Update a NuGet package
Create and connect to a feed
A feed is a collection of packages. You'll create a feed to store your NuGet packages.
- In the web browser window displaying your Azure DevOps project, in the vertical navigational pane, select the Artifacts icon
- With the Artifacts hub displayed, click + Create Feed at the top of the pane
Note: This feed will be a collection of NuGet packages available to users within the organization and will sit alongside the public NuGet feed as a peer.
- On the Create new feed pane, in the Name textbox, type eShopOnWebShared
- In the Visibility section, select People in my organization
- In the Upstream sources section, select Include packages from common public sources
- Click Create
Note: Any user who wants to connect to this NuGet feed must configure their environment.
- Back on the Artifacts hub, click Connect to Feed
- On the Connect to feed pane, in the NuGet section, select Visual Studio and copy the Source URL
- Switch back to the Visual Studio window
- In the Visual Studio window, click Tools menu header and, in the dropdown menu, select NuGet Package Manager and, in the cascading menu, select Package Manager Settings
- In the Options dialog box, click Package Sources and click the plus sign to add a new package source
- At the bottom of the dialog box, in the Name textbox, replace Package source with eShopOnWebShared
- In the Source textbox, paste the URL you copied from Azure DevOps
- Click Update and then OK
Note: Visual Studio is now connected to the new feed.
Create and publish a NuGet package
You'll create a custom NuGet package and publish it to the feed.
-
In the Visual Studio window you used to configure the new package source, in the main menu, click File, in the dropdown menu, click New and then, in the cascading menu, click Project
-
On the Create a new project pane of the New Project dialog box, in the list of project templates, locate the Class Library template, select the Class Library (.NET Standard) template, and click Next
-
On the Configure your new project pane of the New Project dialog box, specify the following settings and click Create:
- Project name: eShopOnWeb.Shared
- Location: accept the default value
- Solution: Create new solution
- Solution name: eShopOnWeb.Shared
-
Click Create
-
In the Visual Studio interface, in the Solution Explorer pane, right-click Class1.cs, in the right-click menu, select Delete, and, when prompted for confirmation, click OK
-
Press Ctrl+Shift+B or right-click on the eShopOnWeb.Shared project and select Build to build the project
Note: Next you'll use MSBuild to generate a NuGet package directly from the project. This approach is common for shared libraries to include all the metadata and dependencies in the package.
- Switch to the Azure DevOps web portal and navigate to the Artifacts section
- Click on the eShopOnWebShared feed
- Click Connect to Feed and select NuGet.exe under the NuGet section
- Copy the Project setup commands for later use
Publish the package using dotnet CLI
You'll use the .NET CLI to pack and publish your package.
-
In Visual Studio, right-click on the eShopOnWeb.Shared project and select Open Folder in File Explorer
-
In the File Explorer window, note the path to the project folder
-
Open a Command Prompt or PowerShell window as Administrator
-
Navigate to the project folder using the
cd
command -
Run the following command to create a NuGet package:
dotnet pack --configuration Release
-
This will create a
.nupkg
file in thebin\Release
folder -
Navigate to the
bin\Release
folder:cd bin\Release
-
You need to publish the package to your Azure Artifacts feed. First, add the package source:
dotnet nuget add source "YOUR_FEED_URL" --name "eShopOnWebShared" --username "YOUR_USERNAME" --password "YOUR_PAT"
Note: Replace YOUR_FEED_URL with the feed URL from Azure DevOps, YOUR_USERNAME with your Azure DevOps username, and YOUR_PAT with a Personal Access Token with package read/write permissions.
-
Publish the package:
dotnet nuget push *.nupkg --source "eShopOnWebShared"
-
Switch back to the Azure DevOps web portal displaying the Artifacts tab
-
Click Refresh
-
In the list of packages, click the eShopOnWeb.Shared package
-
On the eShopOnWeb.Shared pane, review its metadata
Import a NuGet package
You'll now import the package you created into another project.
- Switch back to Visual Studio
- In the Solution Explorer, right-click on the eShopOnWeb.Shared solution and select Add > New Project
- Select Console App (.NET Core) template and click Next
- Configure the project:
- Project name: eShopOnWeb.Shared.Client
- Location: accept the default value
- Solution: Add to solution
- Click Create
- In the Solution Explorer, right-click on the eShopOnWeb.Shared.Client project and select Manage NuGet Packages
- In the NuGet Package Manager pane, click the Browse tab
- In the Package source dropdown, select eShopOnWebShared
- In the search box, type eShopOnWeb.Shared and press Enter
- Select the eShopOnWeb.Shared package and click Install
- Accept any license agreements if prompted
- The package is now installed and can be used in your project
Update a NuGet package
You'll update the package by modifying the original project and publishing a new version.
-
In the Solution Explorer, right-click on the eShopOnWeb.Shared project and select Add > Class
-
Name the class ProductHelper and click Add
-
Add some sample code to the class:
using System; namespace eShopOnWeb.Shared { public class ProductHelper { public static string GetProductDisplayName(string name, decimal price) { return $"{name} - ${price:F2}"; } } }
-
Save the file
-
Right-click on the eShopOnWeb.Shared project and select Properties
-
On the Package tab, increment the Package version from 1.0.0 to 1.1.0
-
Save and close the Properties window
-
Build the project by pressing Ctrl+Shift+B
-
Open Command Prompt or PowerShell as Administrator again
-
Navigate to the eShopOnWeb.Shared project folder
-
Pack the new version:
dotnet pack --configuration Release
-
Navigate to the
bin\Release
folder and publish the new version:dotnet nuget push *.nupkg --source "eShopOnWebShared"
-
Switch back to the Azure DevOps web portal
-
Refresh the eShopOnWeb.Shared package page
-
You should now see version 1.1.0 available
Update the package in the client project
- Switch back to Visual Studio
- In the Solution Explorer, right-click on the eShopOnWeb.Shared.Client project and select Manage NuGet Packages
- Click the Updates tab
- You should see that eShopOnWeb.Shared has an update available
- Select the package and click Update
- Accept any license agreements if prompted
- The package is now updated to the latest version
Summary
In this lab, you learned how to work with Azure Artifacts by:
- Creating and connecting to a feed for centralized package management
- Creating and publishing a NuGet package using the .NET CLI
- Importing a NuGet package into another project
- Updating a NuGet package with new versions and functionality
Azure Artifacts provides a powerful platform for managing packages across your organization, integrating seamlessly with your CI/CD pipelines and development workflows. It supports multiple package formats and provides upstream source capabilities to proxy external package repositories.