Publish Cargo packages with Azure Pipelines
Azure DevOps Services | Azure DevOps Server 2022
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
An Azure DevOps organization. Create an organization if you haven't already.
An Azure DevOps project. Create a project if you don't have one yet.
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.
Sign in to your Azure DevOps organization, and then navigate to your project.
Select Artifacts, and then select Create Feed.
Provide a Name for your feed, specify its Visibility, and then choose a Scope for your feed.
Select Create when you're done.
Authenticate with Azure Artifacts
Sign in to your Azure DevOps organization, and then navigate to your project.
Select Artifacts, and then select your feed.
Select Connect to feed, and then select Cargo from the left pane.
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>"
Create a Personal access token with Packaging > Read & write scopes to authenticate with your feed.
Use the CargoAuthenticate task to authenticate from your pipeline:
Sign in to your Azure DevOps organization, and then navigate to your project.
Select Pipelines, select your pipeline definition, and then select Edit.
Select the
+
sign to add a new task. Search for the Cargo Authenticate task, and then select Add to add it to your pipeline.Select the ellipsis icon to open a new window displaying your repository contents, and then choose your config.toml file.
Publish crates to your feed
From your Azure DevOps project, select Pipelines, select your pipeline definition, and then select Edit.
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.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>
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: