title: How to set environment variables when testing DSL scripts against dummy jenkins?
body: <p>I am trying to automate testing Jenkins groovy dsl scripts, like here:</p>
  
  <p><a href="https://github.com/sheehan/job-dsl-gradle-example" rel="nofollow">https://github.com/sheehan/job-dsl-gradle-example</a></p>
  
  <p>The idea I think is very straight forward, what I'm having issues with is setting environment variables for the dummy Jenkins. I followed the instructions here: </p>
  
  <p><a href="https://wiki.jenkins-ci.org/display/JENKINS/Unit+Test" rel="nofollow">https://wiki.jenkins-ci.org/display/JENKINS/Unit+Test</a></p>
  
  <p>Specifically "How to set env variables" section and added the following to my test executor:</p>
  
  <pre><code>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 &lt;&lt; jobFiles
      }
  </code></pre>
  
  <p>However when I run the actual tests for one of the scripts, I still see the following:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The script Build.groovy uses the variable "${ENVS}" (as if it were provided by parameter in seed job of Jenkins), which works as expected when actually running in Jenkins... So any way to set these "parameters" or env variables in the test jenkins context?</p>
  
  <p>Example of how I use the ENVS variable in the Build.groovy:</p>
  
  <pre><code>def envs = '-'
  "${ENVS}".eachLine{
      def env = it
      envs+=env+'-'
  }
  envs.substring(0,envs.length()-1)
  
  job('Build'+envs) { 
  ...
  }
  </code></pre>