title: Jenkinsfile Pipeline dynamic environment modification at runtime
body: <p>I need to get <code>GitVersion.exe</code> variables in my Jenkins pipeline.</p>
  
  <p>The GitVersion <a href="https://gitversion.readthedocs.io/en/latest/build-server-support/build-server/jenkins/" rel="nofollow noreferrer">documentation</a> gives a hint on how to do that. Essentially call <code>gitversion /output buildserver</code>. </p>
  
  <p>This call does add the variables to the current step and they are lost once the step completes. I can show this call executes when combining a <code>set</code> command in the same bat execution. The second <code>set</code> shows the variables are gone from the environment.</p>
  
  <pre><code>bat 'nuget install GitVersion.CommandLine -OutputDirectory c:/packages -Version 3.6.5'
  bat 'c:/packages/GitVersion.CommandLine.3.6.5/tools/GitVersion.exe /output buildserver &amp;&amp; set'
  bat 'set'
  </code></pre>
  
  <p>The documentation of GitVersion is aware of that and suggests to use <a href="https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin" rel="nofollow noreferrer">EnvInject</a>.
  Installing the plugin and executing the same pipeline did not change the result. I read that the Plugin is not made for pipelines so that may have something to do with it.</p>
  
  <p>Pipelines support a syntax for <a href="https://jenkins.io/doc/pipeline/tour/environment/" rel="nofollow noreferrer">environment</a>.</p>
  
  <p>Following that syntax I can set static variables at the top of my pipeline like this:</p>
  
  <pre><code>environment {
      ASuperVariable = 'MySuperVariable'
  }
  </code></pre>
  
  <p>What I need is combining those calls so that I can add run time variables to the Jenkinsfile pupeline.</p>
  
  <pre><code>environment {
      bat 'gitversion /output buildserver'
  }
  </code></pre>
  
  <p>Now obviously the above call is not even syntax correct. Is there a way to mark a section so that the contained environment changes are available for other steps?</p>
  
  <p>EDIT:
  This is still unsolved. At the moment I need to create a batch script and pass the tool into it as an argument. Inside the batch I can call the tool to add to the environment of the batch script and use that wile the batch is running. A Multi line batch in the Jenkins file could be a solution if the process remains the same over all the multiple lines.</p>