How to pass environment variables to a sbt test build step in Jenkins?
Solution 1
If you're not forking a new JVM to execute your tests, setting javaOptions
does nothing.
Excerpt from SBT itself:
> help javaOptions
Options passed to a new JVM when forking.
This explains why your javaOptions
are not used when you're not forking your tests.
You have basically two solutions:
- Either set
fork in Test := true
to run your tests in forked JVMs -
Or pass your system properties to SBT itself :
sbt -Dcassandra.test.host=XX.XXX.XXX.XXX test
Solution 2
You're setting a system property with -Dcassandra.test.host=XX.XXX.XXX.XXX"
, but then using Properties.envOrElse
which is for environment variables. See Environment Variables.
Try this:
sys.props.getOrElse("cassandra.test.host", DEFAULT_CASSANDRA_TEST_HOST)
Solution 3
It seems that adding fork in Test := true
solves the problem - even if, to be honest, I did not investigated the extact corrlation between the two events (i.e. adding fork in Test := true
and having the system property passed to my tests.
So the correct argument to be passed to sbt is:
'; set fork in Test := true; set javaOptions += "-Dcassandra.test.host=XX.XXX.XXX.XXX"; test'
Related videos on Youtube
piercarlo
Updated on September 15, 2022Comments
-
piercarlo over 1 year
In my scala test, I read an environemnt variable via
sys.props.getOrElse("cassandra.test.host", DEFAULT_CASSANDRA_TEST_HOST)
.The tests are run via Jenkins.
I have added a
Build using sbt
as a build step.By looking at similar questions on SO, I came up with this solution - i.e. setting the Actions field to:
'; set javaOptions += "-Dcassandra.test.host=XX.XXX.XXX.XXX"; test'
But it doesnt work. No variable is set when
Properties.envOrElse
is executed.The Jenkins console output contains:
[...] [util-sessionizer] $ java -jar /usr/local/bin/sbt-launch.jar '; set javaOptions += "-Dcassandra.test.host=XX.XXX.XXX.XXX"; test' [info] Loading project definition from /jenkins/workspace/util-sessionizer/project/project [info] Loading project definition from /jenkins/workspace/util-sessionizer/project [info] Set current project to util-sessionizer (in build file:/jenkins/workspace/util-sessionizer/) [info] Defining *:javaOptions [info] The new value will be used by *:runner, compile:run::runner and 4 others. [info] Run `last` for details. [info] Reapplying settings... [...]