How to download another private repository in Github Actions with Cargo?

Viewed 1712

Problem

I have a private Rust project (A) and it depends on another private Rust project (B). On my local machine, it works because I am logged into git. I am not sure how to login into git in Github Actions. I am not sure if it's needed. I am reading so many things about SSH and HTTPS, that I lost track of what I must do.

I saw https://github.com/webfactory/ssh-agent, https://github.com/fusion-engineering/setup-git-credentials and a few other actions, but I am just guessing things I need to do and I can not get it to work.

Setup

This is my Cargo.toml file on in project A:

...
[dependencies]
b = { git = "https://github.com/me/b.git" }

This fails in Github Actions because Github Actions can not download the private repository without some token. I created a personal access token and changed my Cargo.toml to this:

...
[dependencies]
b = { git = "https://ignore:MyPersonalAccessToken@github.com/me/b" }

but I get an email from Github that my token is revoked because it isn't allowed to have it hardcoded in the code...

Now I am not sure what to do. I can put the token in my github secret, but I don't know how my cargo.toml can use that.

Is there an easy way to just login into git in Github Actions? I tried https://github.com/OleksiyRudenko/gha-git-credentials and configured my workflow like this:

- uses: oleksiyrudenko/gha-git-credentials@v2-latest
  with:
    token: '${{ secrets.GIT_CREDENTIALS }}'
    global: true

But cloning still fails:

Caused by: failed to authenticate when downloading repository

  • attempted to find username/password via git's credential.helper support, but failed

I tried also the library https://github.com/marketplace/actions/setup-git-credentials, but I guess I am mis using it

Question

How can I make Cargo clone private git repositories with Github Actions?

1 Answers

You can add this action after your checkout step and GitHub can access your private repo dependancy.

Note:- Make sure to add a server's private key as a secret, public key to GitHub SSH keys and Please replace your private repo URL from https+auth_token to SSH. ssh://git@github.com/your_group/your_project.git

Related