how to install_gitlab from a private repository

When I try to install from a private repository our own Gitlab CE server with

remotes::install_gitlab(repo = "username/reponame", host = "git.hostname.tld")

I get the following error

Error in utils::download.file(url, path, method = download_method(), quiet = quiet,  : 
  cannot open URL 'https://git.hostname.tld/username/reponame/repository/archive.tar.gz?ref=master&private_token=GITLAB_PAT'

GITLAB_PAT is set in scope "api" (read/write access to the API) and "read_repository" (Grants read-only access to repositories on private projects using Git-over-HTTP (not using the API).)

download.file('https://git.hostname.tld/username/reponame/repository/archive.tar.gz?ref=master&private_token=GITLAB_PAT', destfile = "archive.tar.gz")

gives an "HTTP status was '401 Unauthorized'" error.

Using the corresponding API URL instead works fine:

download.file("https://git.hostname.tld/api/v4/projects/username%2Freponame/repository/archive.tar.gz?ref=master&private_token=GITLAB_PAT", destfile = "archive.tar.gz")

Long story short: Has anyone gotten install_gitlab to work with a private repo and can give me a hint what has gone wrong with my attempts above?

Update: I just tried with a private repo on gitlab.com and I get the same error.
Update2: The main issue seems to be that the repository is private, irrespective of whether the server is gitlab.com or a gitlab CE or EE server. I updated the text accordingly.

1 Like

ping @cderv because I saw that you also use a gitlab server at work? Have you been able to install a package from a private repo with {remotes}?

Yes but I usely use install_git using basic auth. I did before the gilab function was available.
I’ll try tomorrow at work again like you did to see if I can reproduce.

If you could elaborate, that would also be useful as a workaround.

Currently, I use this

cred <- git2r::cred_user_pass(rstudioapi::askForPassword("username"), rstudioapi::askForPassword("Password"))
devtools::install_git("https://mycompany.gitlab.com/username/project.git", credentials = cred)
3 Likes

Thanks for helping me debug this. I've submitted a pull request fixing this issue here.

1 Like

I want to build a gitlab package in a dockerfile, so I dont have the option to ask for user or password. Right now I have username and pwd hardcoded in the Dockerfile, as I was also not able to download the package using the GITLAB_PAT.

My repo is Internal though not private, but the error remains the same.

Is this an install_gitlab bug or where is the mistake?

I did not get install_gitlab to work either and used install_git, effectively running:

Rscript -e "remotes::install_git('https://gitlab-ci-token:$CI_JOB_TOKEN@<server>/<repo.git>')"

Tricky part was how to get $CI_JOB_TOKEN into the docker build. I ended up using build secrets provided by docker buildkit:

    - echo "gitlab-ci-token:$CI_JOB_TOKEN" > auth
    - export DOCKER_BUILDKIT=1
    - docker build --target test --tag "$CI_REGISTRY_IMAGE"/test:"$CI_COMMIT_REF_SLUG" --secret id=auth,src=auth .

Problem was that this disabled build caching, so it is not used although it works in principle.

1 Like

The PR has been merged in the dev version and should hit CRAN with a yet to be released version > 2.0.4.

With that, the following should now be possible:

remotes::install_gitlab(repo = "username/reponame", host = "git.hostname.tld")
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.