34

My institution recently installed GitLab for us. I've figured out how to install R packages from the GitLab server using devtools::install_git and it works as long as the project is public.

#* When modeltable project has Public status
devtools::install_git('https://mini-me2.lerner.ccf.org/nutterb/modeltable.git')

However, if I have a package that is listed as either "Internal" or "Private," I can't install the package without some form of authentication. As of yet, I haven't figured out how to pass authentication via the URL. Does anyone have experience with downloading packages from GitLab?

#* After changing the 'modeltable' project to Private status
devtools::install_git('https://mini-me2.lerner.ccf.org/nutterb/modeltable.git')
Preparing installation of modeltable using the Git-URL: https://mini-me2.lerner.ccf.org/nutterb/modeltable.git
'/usr/bin/git'clone --depth 1 --no-hardlinks https://mini-me2.lerner.ccf.org/nutterb/modeltable.git /tmp/Rtmp5aj1cU/file24493dc03a32
Error: There seems to be a problem retrieving this Git-URL.
Benjamin
  • 16,897
  • 6
  • 45
  • 65
  • Have you tried the standard `https://user@password:domain.com/user/repo.git`? Of course, ultra insecure, but so will be any scheme that involves automatically storing / passing passwords. – Ciro Santilli OurBigBook.com Dec 05 '14 at 16:48
  • Ah, you might also get away with `.netrc`: https://confluence.atlassian.com/display/STASH/Permanently+authenticating+with+Git+repositories#PermanentlyauthenticatingwithGitrepositories-Usingthe.netrcfile – Ciro Santilli OurBigBook.com Dec 05 '14 at 16:56
  • That exact suggestion doesn't work, but `https://user:password@domain.com/user/repo.git` does it. I already have to be authenticated on my institution's network before I can get to GitLab, so security isn't as critical an issue. This helps though. I can at least use this to write a function to install packages. – Benjamin Dec 05 '14 at 17:03
  • 1
    Can you use ssh? It works fine for our org with ssh URLs. – hrbrmstr Dec 05 '14 at 18:46
  • ssh is on my list of things to get to, and is probably the better solution. I'm sure I'll be coming around again when I start digging into it. Thanks. – Benjamin Dec 08 '14 at 13:16
  • You might now like to try out the `remotes` package on CRAN from the RStudio folks – p0bs Oct 30 '18 at 12:19

4 Answers4

25

I'd highly recommend going the SSH route, and the below works for that. I found making the leap to SSH was easy, especially with R and RStudio. I'm using Windows in the below example. Edits from code I use in practice are in all caps.

creds = git2r::cred_ssh_key("C:\\Users\\MYSELF\\.ssh\\id_rsa.pub",
                            "C:\\Users\\MYSELF\\.ssh\\id_rsa")
devtools::install_git("git@gitlab.WORKDOMAIN.com:GITLABGROUP/PACKAGE.git",
                      credentials = creds)

Two quick additional comments:

  • git2r is imported with devtools, you shouldn't need to install it separately.
  • Also I don't think this should need mentioning, but passwords in plaintext in your script is a very bad idea.
Rafael Zayas
  • 2,061
  • 1
  • 18
  • 20
20

You could try a combination of the devtools and getPass packages.

https://github.com/wrathematics/getPass

devtools::install_git(
  "https://gitlab.com/foo/bar.git", 
  credentials = git2r::cred_user_pass("uname", getPass::getPass())
)

Where uname is your Gitlab user name.

jsta
  • 3,216
  • 25
  • 35
  • 2
    Aren't you sending password over the wire in plaintext then? – Shape Oct 06 '16 at 19:22
  • This is a good option because, unlike Benjamin's answer, getPass masks the password from being displayed on screen. Otherwise I believe your are correct that it will be transmitted plaintext. I think ssh is the way to go if you are concerned about that. – jsta Oct 08 '16 at 01:51
  • 2
    my goto for masking the password or storing credentials is actually to use `getPass` along with the `openssl` package to encrypt stored credentials, so I can continue to use them repeatedly during a session, but I only decrypt when I need them. But for authenticating, I feel very iffy not using `https` – Shape Oct 08 '16 at 03:08
9

Per Ciro's comment, authenticating using

https://user:password@domain.com/user/repo.git

does the trick. So the complete call would be

devtools::install_git('https://user:password@mini-me2.lerner.ccf.org/nutterb/modeltable.git')

Please note that there may be security concerns with passing the user name and password this way. I'm not completely educated on those concerns. This works well enough for my purposes because I am authenticated on my company's network to even see the GitLab server.

Benjamin
  • 16,897
  • 6
  • 45
  • 65
0

Slightly simpler than getting it through SSH might be using Personal access tokens(docs). Once set up and ideally stored as .Renviron variable, PAT can be passed to devtools:

devtools::install_gitlab(repo = "path/to/repository",
    auth_token = Sys.getenv("GITLAB_PAT"))
radek
  • 7,240
  • 8
  • 58
  • 83