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

  1. 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.

  1. Package Types

  • NuGet: Used for .NET packages.
  • npm: Used for JavaScript packages.
  • Maven: Used for Java packages.
  • Python: Used for Python packages.

  1. 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.

  1. 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

  1. Navigate to your Azure DevOps project.
  2. Select Artifacts from the left-hand menu.
  3. Click on New Feed.
  4. Provide a name for your feed and configure its visibility (public or private).
  5. 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

  1. Create a NuGet Package:

    dotnet pack -o ./nupkgs
    
  2. Publish the Package:

    nuget push ./nupkgs/YourPackage.1.0.0.nupkg -Source "YourFeedName"
    

Example: Consuming a NuGet Package

  1. 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>
    
  2. Install the Package:

    dotnet add package YourPackage --version 1.0.0
    

Exercises

Exercise 1: Create and Publish a NuGet Package

  1. Create a simple .NET library project.
  2. Pack the project into a NuGet package.
  3. Create a feed in Azure Artifacts.
  4. Publish the package to the feed.

Exercise 2: Consume a Package from Azure Artifacts

  1. Create a new .NET project.
  2. Add the feed URL to the NuGet.config file.
  3. Install the package you published in Exercise 1.
  4. Use the package in your project.

Solutions

Solution to Exercise 1

  1. Create a .NET Library Project:

    dotnet new classlib -o MyLibrary
    cd MyLibrary
    
  2. Pack the Project:

    dotnet pack -o ./nupkgs
    
  3. Create a Feed:

    • Follow the steps in the "Setting Up Azure Artifacts" section.
  4. Publish the Package:

    nuget push ./nupkgs/MyLibrary.1.0.0.nupkg -Source "YourFeedName"
    

Solution to Exercise 2

  1. Create a New .NET Project:

    dotnet new console -o MyApp
    cd MyApp
    
  2. 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>
    
  3. Install the Package:

    dotnet add package MyLibrary --version 1.0.0
    
  4. 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.

© Copyright 2024. All rights reserved