4

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?

6
  • Maybe auth_token = shell("$GITHUB_CRED", intern = TRUE)? Commented Oct 24, 2016 at 20:51
  • I'm going to give it a shot. I have a hacky solution right now where I'm pushing it to a file and then readLines() Commented Oct 24, 2016 at 20:52
  • How about args? Rscript -e 'args <- commandArgs(trailingOnly = TRUE); paste("bla", args[1])' myPassword Commented Oct 24, 2016 at 21:18
  • @RichScriven I tried both shell and system, neither worked. Commented Oct 24, 2016 at 21:18
  • @zx8754 going to try your solution now too. Commented Oct 25, 2016 at 0:14

3 Answers 3

1

Have you tried using Sys.getenv?

Rscript -e 'devtools::install_github("private/repo", auth_token=Sys.getenv("GITHUB_CRED"))'
Sign up to request clarification or add additional context in comments.

2 Comments

Does Sys.getenv retrieve the correct value? You can check by printing it out.
It worked for me as long as I exported the environment variable.
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?

3 Comments

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.
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, ...
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 github.com/docker/docker/issues/13490
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.