---
url: /en/artifact/cargo.md
---
## Configure Credentials

Update the `.cargo/config.toml` file under the current project, add the following lines

```toml
[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

```shell
echo ${CNB_TOKEN} | cargo login --registry cnb-cargo-repo
```

For how to get `<CNB_CARGO_URL>`, please refer to [Obtaining the Artifact Registry Address](./intro.md#obtaining-the-artifact-registry-address)

For how to get `<CNB_TOKEN>`, please refer to [Creating an Access Token](./intro.md#creating-an-access-token)

## Push Artifacts

::: tabs
@tab Local CLI Push

1. Follow the above [Configure Credentials](#configure-credentials)

2. Execute the following command to publish to CNB

```shell
cargo publish
```

@tab Push in Cloud Native Build

1. Paste the following content into `.cnb.yaml`, commit to the repository

```yaml title=".cnb.yml"
$:
  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
```

2. Update the `.cargo/config.toml` file in the repository, add the following lines. For `<CNB_CARGO_URL>`, please refer to [Obtaining the Artifact Registry Address](./intro.md#obtaining-the-artifact-registry-address)

```toml
[registry]
default = "cnb-cargo-repo"
global-credential-providers = ["cargo:token"]

[registries.cnb-cargo-repo]
index = "sparse+<CNB_CARGO_URL>"
```

3. Update the version field in the `Cargo.toml` file in the repository

```toml
[package]
name = "<PACKAGE_NAME>"
version = "<VERSION>"
```

4. Commit a Tag, which will trigger Cloud Native Build

@tab Push in Workspaces

1. 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

```yaml title=".cnb.yml"
$:
  vscode:
    - docker:
        image: docker.cnb.cool/examples/language/rust-1.87
      services:
        - vscode
        - docker
```

2. Follow the above [Configure Credentials](#configure-credentials)

3. Update the version field in the `Cargo.toml` file in the repository

```toml
[package]
name = "<PACKAGE_NAME>"
version = "<VERSION>"
```

4. Publish to CNB

```shell
cargo publish
```

:::

## Use Artifacts

### Pull Artifacts

After following the above [Configure Credentials](#configure-credentials), use the following command to pull artifacts

```shell
# Using rand as an example
cargo add rand
```

### Yank/Unyank Artifacts

```shell
# Yank artifact
cargo yank my-crate@0.1.0
# Unyank artifact
cargo yank my-crate@0.1.0 --undo
```

### Delete Artifacts

cargo does not provide a delete command, please refer to [Deleting Artifacts](./intro.md#deleting-artifacts)

## FQA

### 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 [official documentation](https://doc.rust-lang.org/cargo/reference/config.html)

### Q2: cargo version requirements

It is recommended to use Cargo version 1.73.0 or later.

Versions below 1.73.0 have poor support for private repositories, failing to pass credentials in some scenarios and leading to authentication failures.

### Q3: When uploading an open-source package to CNB, it prompts that the artifact version already exists

This is because CNB proxies the official source by default. When the package does not exist on the server side, it will automatically provide the package to the cargo client from the proxy.

As a result, the cargo client mistakenly assumes that the package already exists in CNB.

To resolve this issue, you can first upload a placeholder with a non-existent version, and then upload the correct version again.
