Επεξεργασία

Κοινή χρήση μέσω


Use packages from Crates.io

Azure DevOps Services | Azure DevOps Server 2022

Azure Artifacts upstream sources enable developers to consume packages from public registries like Crates.io and nuget.org. This article will guide you through setting up your project and using the command line to consume Crates from Crates.io.

This article will guide you through how to:

  • Create an Azure Artifacts feed
  • Connect to your feed
  • Consume crates from upstream

Prerequisites

Create a feed

Azure Artifacts recommends having a dedicated feed for consuming crates from crates.io and a separate feed exclusively for publishing internal crates.

  1. Sign in to your Azure DevOps organization, and then navigate to your project.

  2. Select Artifacts, and then select Create Feed.

  3. Enter a Name for your feed, define its Visibility, and then select your Scope. Make sure to check the Upstream sources checkbox to include packages from public registries.

  4. Select Create when you're done.

    A screenshot showing how to create a feed for cargo upstream.

Connect to your feed

  1. Sign in to your Azure DevOps organization, and then navigate to your project.

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

  3. Select Connect to feed, and then select Cargo from the left navigation pane.

  4. If this is the first time using Cargo with Azure Artifacts, make sure you have installed rustup.

  5. Add the provided snippet from the Project setup section to your .cargo/config.toml file in your source repository:

    • Project-scoped feed:

      [registries]
      <FEED_NAME> = { index = "sparse+https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/Cargo/index/" }
      
      [source.crates-io]
      replace-with = "<FEED_NAME>"
      
    • Organization-scoped feed:

      [registries]
      <FEED_NAME> = { index = "sparse+https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/_packaging/<FEED_NAME>/Cargo/index/" }
      
      [source.crates-io]
      replace-with = "<FEED_NAME>"
      
  1. Sign in to your Azure DevOps collection, and then navigate to your project.

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

  3. Select Connect to feed, and then select Cargo from the left navigation pane.

  4. If this is the first time using Cargo with Azure Artifacts, make sure you have installed rustup.

  5. Add the provided snippet from the Project setup section to your .cargo/config.toml file in your source repository:

    • Project-scoped feed:

      [registries]
      <FEED_NAME> = { index = "sparse+https://pkgs.dev.azure.com/<COLLECTION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/Cargo/index/" }
      
      [source.crates-io]
      replace-with = "<FEED_NAME>"
      
    • Collection-scoped feed:

      [registries]
      <FEED_NAME> = { index = "sparse+https://pkgs.dev.azure.com/<COLLECTION_NAME>/_packaging/<FEED_NAME>/Cargo/index/" }
      
      [source.crates-io]
      replace-with = "<FEED_NAME>"
      

Configure a credential provider

To use Cargo with Azure Artifacts, you need to set up a credential provider. The provided settings will configure a default credential helper for the current user:

Paste the following snippet in your %USERPROFILE%.cargo\config.toml:

[registry]
global-credential-providers = ["cargo:token", "cargo:wincred"]

Log in to your registry

  1. Create a Personal access token with Packaging > Read & write scopes to authenticate with your feed.

  2. Run the following command to log in to your registry. Replace the placeholder with your feed's name, and paste the personal access token you created in the previous step when prompted:

    "Basic " + [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("PAT:" + (Read-Host -MaskInput "Enter PAT"))) | cargo login --registry <FEED_NAME>
    

Save packages from Crates.io

Note

To save packages from upstreams, you must have the Feed and Upstream Reader (Collaborator) role or higher. See Manage Permissions for more details.

Now that we have set up our project, configured a credential provider, and logged into our feed, we can begin consuming packages from upstream. Azure Artifacts saves a copy of any package you install from upstream to your feed.

In this example, we consume the serde crate, a serialization/deserialization framework:

  1. Run the following command in your project directory to add the crate to your cargo.toml:

    cargo add serde
    
  2. Run the following command to build your project and consume your crate:

    cargo build
    

Once your package is installed, a copy will be saved to your feed. Navigate to your feed to verify its presence. Your package should be available in your feed, as shown below:

A screenshot showing the *serde* crate consumed from upstream.