Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass environment variables to a sbt test build step in Jenkins?

Tags:

scala

jenkins

sbt

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... [...]

like image 413
piercarlo Avatar asked Apr 14 '15 12:04

piercarlo


1 Answers

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

like image 76
Pierre DAL-PRA Avatar answered Sep 30 '22 19:09

Pierre DAL-PRA