Cargo Artifact Registry
About 534 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>, please refer to Obtaining the Artifact Registry Address.
For how to get <CNB_TOKEN>, please refer to Creating an Access Token.
Push Artifacts
Follow the above Configure Credentials
Execute the following command to publish to CNB:
cargo publish- Paste the following content into
.cnb.yaml, commit to the repository:
$:
tag_push:
- docker:
image: docker.cnb.cool/examples/language/rust-1.87
stages:
- name: Configure authentication credentials
script: echo ${CNB_TOKEN} | cargo login
- name: Publish to CNB
script: cargo publish --allow-dirty- Update the
.cargo/config.tomlfile in the repository, add the following lines. For<CNB_CARGO_URL>, please refer to Obtaining the Artifact Registry Address:
[registry]
default = "cnb-cargo-repo"
global-credential-providers = ["cargo:token"]
[registries.cnb-cargo-repo]
index = "sparse+<CNB_CARGO_URL>"- Update the version field in the
Cargo.tomlfile in the repository:
[package]
name = "<PACKAGE_NAME>"
version = "<VERSION>"- Commit a Tag, which will trigger Cloud Native Build.
- Paste the following content into
.cnb.yaml, 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:
# Using rand as an example
cargo add randYank/Unyank Artifacts
# Yank artifact
cargo yank my-crate@0.1.0
# Unyank artifact
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, please check 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.