Publish Cargo packages with Azure Pipelines

Azure Pipelines enables developers to publish their Cargo packages to Azure Artifacts feeds and public registries such as Crates.io. In this article, you will learn how to publish your Cargo packages to an Azure Artifacts feed using both YAML and Classic pipelines.

This article will guide you through how to:

  • Create an Azure Artifacts feed
  • Authenticate with Azure Artifacts
  • Publish Cargo packages

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. If you already have a feed, you can proceed to the next section.

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

  2. Select Artifacts, and then select Create Feed.

  3. Provide a Name for your feed, specify its Visibility, and then choose a Scope for your feed.

  4. Select Create when you're done.

    A screenshot showing how to create a cargo feed for internal packages.

Authenticate with Azure Artifacts

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

  2. Select Artifacts, and then select your feed.

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

  4. Follow the instructions in the Project setup section by adding the provided snippet to your 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>"
      
  5. Create a Personal access token with Packaging > Read & write scopes to authenticate with your feed.

  6. Use the CargoAuthenticate task to authenticate from your pipeline:

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

    2. Select Pipelines, select your pipeline definition, and then select Edit.

    3. Select the + sign to add a new task. Search for the Cargo Authenticate task, and then select Add to add it to your pipeline.

    4. Select the ellipsis icon to open a new window displaying your repository contents, and then choose your config.toml file.

      A screenshot showing the Cargo authenticate task in a classic pipeline.

Publish crates to your feed

  1. From your Azure DevOps project, select Pipelines, select your pipeline definition, and then select Edit.

  2. Select the + sign on your agent job to add a new task. Find the PowerShell task through the search function, and then select Add to add it to your pipeline.

  3. Give your task a name, e.g., Publish, and then select Inline as the type. Paste your publish command inline, replacing the placeholder with your feed name:

    cargo publish --registry <FEED_NAME>
    

A screenshot showing how to publish crates to and Azure Artifacts feed using a classic pipeline.

Example

In this example, we will install rustup on the agent, set up the PATH environment variable, build our project, authenticate with CargoAuthenticate, and finally publish our crate to our Azure Artifacts feed:

trigger:
- main

pool:
  vmImage: windows-latest

steps:
- powershell: |
   Invoke-WebRequest -Uri https://sh.rustup.rs -OutFile rustup-init.sh
   bash .\rustup-init.sh -y
   echo "##vso[task.prependpath]$env:USERPROFILE\.cargo\bin"
  displayName: Install

- task: CargoAuthenticate@0
  displayName: 'cargo Authenticate'
  inputs:
    configFile: '.cargo/config.toml'

- script: |
   cargo build --all
  displayName: Build

- powershell: |
   cargo publish --registry CargoInternalFeed
  displayName: Publish

After your pipeline run is completed, your crate should be available in your feed, as shown below:

A screenshot showing the hello-world-cargo crate published to the feed.