如何在测试DSL脚本时设置环境变量



我正在尝试自动化测试Jenkins groovy dsl脚本,就像这里:

https://github.com/sheehan/job-dsl-gradle-example

我认为这个想法非常直接,我遇到的问题是为虚拟Jenkins设置环境变量。我遵循了这里的说明:

https://wiki.jenkins-ci.org/display/JENKINS/Unit +测试

特别是"如何设置环境变量"一节,并在我的测试执行器中添加了以下内容:

import hudson.slaves.EnvironmentVariablesNodeProperty
import hudson.EnvVars
/**
 * Tests that all dsl scripts in the jobs directory will compile.
 */
class JobScriptsSpec extends Specification {
    @Shared
    @ClassRule
    JenkinsRule jenkinsRule = new JenkinsRule()

    EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
    EnvVars envVars = prop.getEnvVars();

    @Unroll
    void 'test script #file.name'(File file) {
        given:
        envVars.put("ENVS", "dev19");
        jenkinsRule.jenkins.getGlobalNodeProperties().add(prop);
        JobManagement jm = new JenkinsJobManagement(System.out, [:], new File('.'))
        when:
        new DslScriptLoader(jm).runScript(file.text)
        then:
        noExceptionThrown()
        where:
        file << jobFiles
    }

然而,当我为其中一个脚本运行实际测试时,我仍然看到以下内容:

Failed tests

test script Build.groovy
Expected no exception to be thrown, but got 'javaposse.jobdsl.dsl.DslScriptException'
    at spock.lang.Specification.noExceptionThrown(Specification.java:118)
    at com.dslexample.JobScriptsSpec.test script #file.name(JobScriptsSpec.groovy:40)
Caused by: javaposse.jobdsl.dsl.DslScriptException: (script, line 3) No such property: ENVS for class: script

脚本构建。groovy使用变量"${ENVS}"(就好像它是由Jenkins的种子作业中的参数提供的一样),它在实际运行在Jenkins中时按预期工作…那么在测试jenkins上下文中设置这些"参数"或环境变量的任何方法?

如何在Build.groovy中使用ENVS变量的示例:

def envs = '-'
"${ENVS}".eachLine{
    def env = it
    envs+=env+'-'
}
envs.substring(0,envs.length()-1)
job('Build'+envs) { 
...
}

JenkinsJobManagement构造函数的第二个参数是将在DSL脚本中可用的环境变量映射。

Map<String, String> envVars = [
    FOO: 'BAR'
]
JobManagement jm = new JenkinsJobManagement(System.out, envVars, new File('.'))

相关内容

  • 没有找到相关文章

最新更新