Introduction
Azure Artifacts is a service within Azure DevOps that allows you to create, host, and share packages with your team. It supports multiple package types, including NuGet, npm, Maven, and Python. Azure Artifacts helps you manage your dependencies and provides a central place for your team to store and share packages.
Key Concepts
- Feeds
- Definition: Feeds are container-like structures where you can store and organize your packages.
- Usage: You can create multiple feeds to separate different types of packages or to manage packages for different projects.
- Package Types
- NuGet: Used for .NET packages.
- npm: Used for JavaScript packages.
- Maven: Used for Java packages.
- Python: Used for Python packages.
- Upstream Sources
- Definition: Upstream sources allow you to connect your feed to public registries or other feeds.
- Usage: This enables you to consume packages from public sources like npmjs.com or NuGet.org directly from your Azure Artifacts feed.
- Retention Policies
- Definition: Retention policies help you manage the lifecycle of your packages by automatically deleting old versions.
- Usage: This ensures that your feed does not grow indefinitely and helps manage storage costs.
Setting Up Azure Artifacts
Step 1: Create a Feed
- Navigate to your Azure DevOps project.
- Select Artifacts from the left-hand menu.
- Click on New Feed.
- Provide a name for your feed and configure its visibility (public or private).
- Click Create.
Step 2: Connect to Your Feed
- NuGet: Add the feed URL to your
NuGet.config
file. - npm: Add the feed URL to your
.npmrc
file. - Maven: Add the feed URL to your
settings.xml
file. - Python: Add the feed URL to your
pip.conf
file.
Step 3: Publish a Package
- NuGet: Use the
nuget push
command. - npm: Use the
npm publish
command. - Maven: Use the
mvn deploy
command. - Python: Use the
twine upload
command.
Practical Example
Example: Publishing a NuGet Package
-
Create a NuGet Package:
dotnet pack -o ./nupkgs
-
Publish the Package:
nuget push ./nupkgs/YourPackage.1.0.0.nupkg -Source "YourFeedName"
Example: Consuming a NuGet Package
-
Add the Feed to NuGet.config:
<configuration> <packageSources> <add key="YourFeedName" value="https://pkgs.dev.azure.com/YourOrganization/YourProject/_packaging/YourFeedName/nuget/v3/index.json" /> </packageSources> </configuration>
-
Install the Package:
dotnet add package YourPackage --version 1.0.0
Exercises
Exercise 1: Create and Publish a NuGet Package
- Create a simple .NET library project.
- Pack the project into a NuGet package.
- Create a feed in Azure Artifacts.
- Publish the package to the feed.
Exercise 2: Consume a Package from Azure Artifacts
- Create a new .NET project.
- Add the feed URL to the
NuGet.config
file. - Install the package you published in Exercise 1.
- Use the package in your project.
Solutions
Solution to Exercise 1
-
Create a .NET Library Project:
dotnet new classlib -o MyLibrary cd MyLibrary
-
Pack the Project:
dotnet pack -o ./nupkgs
-
Create a Feed:
- Follow the steps in the "Setting Up Azure Artifacts" section.
-
Publish the Package:
nuget push ./nupkgs/MyLibrary.1.0.0.nupkg -Source "YourFeedName"
Solution to Exercise 2
-
Create a New .NET Project:
dotnet new console -o MyApp cd MyApp
-
Add the Feed URL to NuGet.config:
<configuration> <packageSources> <add key="YourFeedName" value="https://pkgs.dev.azure.com/YourOrganization/YourProject/_packaging/YourFeedName/nuget/v3/index.json" /> </packageSources> </configuration>
-
Install the Package:
dotnet add package MyLibrary --version 1.0.0
-
Use the Package:
using MyLibrary; class Program { static void Main(string[] args) { // Your code using MyLibrary } }
Conclusion
In this section, you learned about Azure Artifacts, its key concepts, and how to set it up. You also practiced creating, publishing, and consuming packages. Azure Artifacts is a powerful tool for managing your dependencies and sharing packages within your team, making it an essential part of your DevOps toolkit.