2

Is there a way to pass a shell environment variable to Rscript. For example:

Rscript -e 'devtools::install_github("private/repo",auth_token = "$GITHUB_CRED")'

I've tried this and it just passes the literal character. I'm wondering if there's another way?

Zheyuan Li
  • 62,170
  • 17
  • 162
  • 226
Brandon Bertelsen
  • 42,225
  • 33
  • 153
  • 250

3 Answers3

2

Have you tried using Sys.getenv?

Rscript -e 'devtools::install_github("private/repo", auth_token=Sys.getenv("GITHUB_CRED"))'
Hong Ooi
  • 54,701
  • 13
  • 127
  • 173
0

Seems a bit ridiculous, but:

echo $GITHUB_CRED > file.txt
Rscript -e 'devtools::install_github("private/repo",auth_token = readLines("file.txt"))'

I'm using this in jenkins build(s) with docker-slaves-plugin so that I can store all my configuration in Dockerfiles associated with my package repositories (clean builds)

Brandon Bertelsen
  • 42,225
  • 33
  • 153
  • 250
0

In the narrow sense of the question: "yes, Rscript can":

edd@max:~$ SOME_VAR="some value" Rscript -e 'print(Sys.getenv("SOME_VAR"))'
[1] "some value"
edd@max:~$ 

As for dealing with GitHub credentials, there are probably better solutions available via the proper GitHub clients. Did you try the (excellent and recommended) git2r package?

Dirk Eddelbuettel
  • 347,098
  • 55
  • 623
  • 708
  • No, I haven't tried `git2r`. I think the main problem, unstated, is that I'm passing it as an environment variable to a Dockerfile that's being built by a CI system. So none of the more conventional approaches are working. – Brandon Bertelsen Oct 25 '16 at 14:11
  • I am no security expert but some people have worked in that space. Don't reinvent the wheel -- do some google search around Docker, git, credentials, ... – Dirk Eddelbuettel Oct 25 '16 at 14:12
  • Even Docker doesn't have a well-defined strategy for sharing secrets. Everything right now is just a hack with security issues. For your reading pleasure https://github.com/docker/docker/issues/13490 – Brandon Bertelsen Oct 25 '16 at 18:05