3

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
  • 71,365
  • 17
  • 180
  • 248
Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255

3 Answers3

2

Have you tried using Sys.getenv?

Rscript -e 'devtools::install_github("private/repo", auth_token=Sys.getenv("GITHUB_CRED"))'
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
1

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
  • 360,940
  • 56
  • 644
  • 725
  • 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
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
  • 43,807
  • 34
  • 160
  • 255