Cargo Artifact Registry
About 494 wordsAbout 2 min
Configure Credentials
Update the .cargo/config.toml file under the current project, add the following lines:
[registry]
default = "cnb-cargo-repo"
global-credential-providers = ["cargo:token"]
[registries.cnb-cargo-repo]
index = "sparse+<CNB_CARGO_URL>"Use the following command to configure credentials:
echo ${CNB_TOKEN} | cargo login --registry cnb-cargo-repoFor how to get <CNB_CARGO_URL>, refer to Obtaining the Artifact Registry Address.
For how to get <CNB_TOKEN>, refer to Creating an Access Token.
Push Artifacts
Follow the above Configure Credentials
Execute the following command to publish to CNB:
cargo publishAfter completing the .cargo/config.toml configuration in the repository (see Configure Credentials above), commit to the repository and push a Tag to trigger the build:
$:
tag_push:
- docker:
image: docker.cnb.cool/examples/language/rust-1.87
stages:
- name: update version
script: sed -i "s#^version = .*#version = \"${CNB_BRANCH}\"#" Cargo.toml
- name: cargo login
script: echo ${CNB_TOKEN} | cargo login
- name: cargo publish
script: cargo publish --allow-dirtyThe update version step in the pipeline automatically writes the Tag value into the version field of Cargo.toml.
- Paste the following content into
.cnb.yml, after committing to the repository, click the 「Workspace」 button in the upper right corner of the repository page to enter the development environment:
$:
vscode:
- docker:
image: docker.cnb.cool/examples/language/rust-1.87
services:
- vscode
- dockerFollow the above Configure Credentials
Update the version field in the
Cargo.tomlfile in the repository:
[package]
name = "<PACKAGE_NAME>"
version = "<VERSION>"- Publish to CNB:
cargo publishUse Artifacts
Pull Artifacts
After following the above Configure Credentials, use the following command to pull artifacts:
cargo add randYank/Unyank Artifacts
cargo yank my-crate@0.1.0
cargo yank my-crate@0.1.0 --undoDelete Artifacts
Cargo does not provide a delete command. Please refer to Deleting Artifacts.
FAQ
Q1: How to configure global credentials
Global configuration paths are as follows:
Windows: %USERPROFILE%\.cargo\config.toml
Unix: $HOME/.cargo/config.toml
For more usage, refer to the official documentation.
Q2: Cargo version requirements
Cargo 1.73.0 or later is recommended.
Versions below 1.73.0 have insufficient support for private repositories — in some scenarios, credentials may not be passed correctly, leading to authentication failures.
Q3: When uploading an open-source package to CNB, it prompts that the artifact version already exists
CNB proxies the official source by default. When a package does not exist on the server, it is automatically served from the proxy, causing the Cargo client to mistakenly believe the package already exists on CNB.
To resolve this, upload a non-existent version as a placeholder first, then upload the correct version.