scala 如何将环境变量传递给 Jenkins 中的 sbt 测试构建步骤?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29627630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass environment variables to a sbt test build step in Jenkins?
提问by piercarlo
In my scala test, I read an environemnt variable via sys.props.getOrElse("cassandra.test.host", DEFAULT_CASSANDRA_TEST_HOST).
在我的 scala 测试中,我通过sys.props.getOrElse("cassandra.test.host", DEFAULT_CASSANDRA_TEST_HOST).
The tests are run via Jenkins.
测试通过 Jenkins 运行。
I have added a Build using sbtas a build step.
我添加了一个Build using sbt作为构建步骤。
By looking at similar questions on SO, I came up with this solution - i.e. setting the Actions field to:
通过查看关于 SO 的类似问题,我想出了这个解决方案 - 即将 Actions 字段设置为:
'; set javaOptions += "-Dcassandra.test.host=XX.XXX.XXX.XXX"; test'
'; set javaOptions += "-Dcassandra.test.host=XX.XXX.XXX.XXX"; test'
But it doesnt work. No variable is set when Properties.envOrElseis executed.
但它不起作用。Properties.envOrElse执行时没有设置变量。
The Jenkins console output contains:
Jenkins 控制台输出包含:
[...]
[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...
[...]
[...]
[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...
[...]
回答by Pierre DAL-PRA
If you're not forking a new JVM to execute your tests, setting javaOptionsdoes nothing.
Excerpt from SBT itself:
如果您没有派生一个新的 JVM 来执行您的测试,则设置javaOptions不会执行任何操作。摘自 SBT 本身:
> help javaOptions
Options passed to a new JVM when forking.
This explains why your javaOptionsare not used when you're not forking your tests.
这解释了为什么javaOptions当您不分叉测试时不使用您。
You have basically two solutions:
您基本上有两种解决方案:
- Either set
fork in Test := trueto run your tests in forked JVMs Or pass your system properties to SBT itself :
sbt -Dcassandra.test.host=XX.XXX.XXX.XXX test
- 要么设置
fork in Test := true为在分叉的 JVM 中运行您的测试 或者将您的系统属性传递给 SBT 本身:
sbt -Dcassandra.test.host=XX.XXX.XXX.XXX test
回答by Dale Wijnand
You're setting a system property with -Dcassandra.test.host=XX.XXX.XXX.XXX", but then using Properties.envOrElsewhich is for environment variables. See Environment Variables.
您正在使用 设置系统属性-Dcassandra.test.host=XX.XXX.XXX.XXX",然后使用Properties.envOrElsewhich 用于环境变量。请参阅环境变量。
Try this:
试试这个:
sys.props.getOrElse("cassandra.test.host", DEFAULT_CASSANDRA_TEST_HOST)
回答by piercarlo
It seems that adding fork in Test := truesolves the problem - even if, to be honest, I did not investigated the extact corrlation between the two events (i.e. adding fork in Test := trueand having the system property passed to my tests.
似乎添加fork in Test := true解决了问题 - 即使,老实说,我没有调查两个事件之间的确切相关性(即添加fork in Test := true并让系统属性传递给我的测试。
So the correct argument to be passed to sbt is:
所以传递给 sbt 的正确参数是:
'; set fork in Test := true; set javaOptions += "-Dcassandra.test.host=XX.XXX.XXX.XXX"; test'
'; set fork in Test := true; set javaOptions += "-Dcassandra.test.host=XX.XXX.XXX.XXX"; test'

