Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do SBT env variables come from?

I'm moving my first steps with Scala (2.12.7) and SBT (1.2.7). At some point, I want to get secret value from the environment:

sys.env("SECRET_TOKEN")

The problem is that, in the sbt shell, SECRET_TOKEN is not defined, therefore the application crashes.

So:

$ export SECRET_TOKEN="xxx"
$ sbt
[... sbt loads]
sbt> run
[ crashes because of the env var not found ]

It's like the sbt shell would get only a subset of the current environment.

Am I missing something?

Thanks

like image 361
Ilario Pierbattista Avatar asked Feb 22 '19 10:02

Ilario Pierbattista


1 Answers

sbt (script + launcher) just launches a fancy java process, which should inherit the environment variables from the parent process.

Given

$ export SECRET_TOKEN="xxx"

Both build.sbt and your application during run should have access to sys.env("SECRET_TOKEN").

In the comment section, Bruno suggested SECRET_TOKEN="xxx" sbt, which apparently worked, but I don't know how that's different from export.

In any case,

object Hello extends App {
  println(sys.env("SECRET_TOKEN"))
}

works for me

sbt:hello> run
[info] Running Hello
xxx
like image 101
Eugene Yokota Avatar answered Sep 24 '22 02:09

Eugene Yokota