How to set up Git credentials to be used by Cargo for access to a *custom* registry in a non-interactive way?

So, we have managed so set up a custom registry for our internal needs.

.cargo/config.toml looks like this:

[net]
git-fetch-with-cli=true

[registries]
ours = { index = "https://git.my-custom-registry.de/rust-registry/index.git" }

If I run a cargo build manually, everything works fine. I can just interactively provide the Git username and the corresponding password for our Git server when Cargo asks for it.

Now this needs to work non-interactively, in a Docker environment :thinking:

Adding prefix username:password@ to the index URL unfortunately does not work:

registry URLs may not contain passwords

I also tried to create ~/.cargo/credentials.toml file, as suggested by some sites:

[registries.ours]
username = "..."
password = "..."

But Cargo does not seem to recognize the credentials! Still asks for passwd interactively :roll_eyes:

Tried with token = "..." too. Not recognized either!

How can I resolve this? Thanks.

Note that since you have set git-fetch-with-cli=true the one asking for the password is not Cargo, but your Git CLI. Thus you need to tell your Git CLI how to do that, see for example The Vanilla DevOps Git Credentials & Private Packages Cheatsheet for a bunch of ways on how to do that.

1 Like

Yeah, that is because without git-fetch-with-cli=true it won't work at all!

Thus you need to tell your Git CLI how to do that, see for example The Vanilla DevOps Git Credentials & Private Packages Cheatsheet for a bunch of ways on how to do that.

Thanks a lot! Makes sense :smirk:

I have now found the solution that does not use ~/.cargo/credentials.toml, but instead:

~/.gitconfig must be set to:

[credential]
helper = store

Then line(s) like this can be added to ~/.git-credentials and it will actually work:

https://username:password@git.my-custom-registry.de/rust-registry/index.git
        ^^^^^^^^^^^^^^^^^^

:+1: