Jenkins & PowerShell – Create Jenkins environment variables during a job run

In Jenkins there are a set of default jenkins environment variables that you can call from within a Powershell Build step. You can add a powershell script into a job’s build step with the used of the Jenkins Powershell plugin. The Powershell build step can then access a jenkins job’s build parameter, under the $env scope, that is::

 if ($env:JenkinsParameter -eq $null){
        "ERROR: Cannot find value for 'JenkinsParameter'"
        exit 1
    }
 $JenkinsParameter = $env:JenkinsParameter

You can also access Jenkins environment variables in the same way, E.g. if you want to access the “Job_name” envrinment variable, then you do:

$JOB_NAME= $env:JOB_NAME
write-host $JOB_NAME

Also in your powershell script you can create environment variables that can be accessible by any latter powershell build steps. Also if you set the environment at the “machine” scope than these variables will be available by powershell build steps in different jenkins jobs. But that’s not good practice because you want your Jenkins jobs self contained.

However “powershell environment variables” are not accessible by any other types of build step (e.g. a python build step). You can only pass them from one powershell build step to any other successive powershell build steps that are within the same Job.

The only way round this issue to create a variable as “Jenkins environment variable” (aka a jenkins job parameter that gets set midway through the job’s run). This is possible with the help of the EnvInject plugin.

It is actually really easy to do. The way it works is that your powershell build step actually outputs all the variables to a file using the key=value syntax, e.g. here we created a file falled “env.properties”:

PS C:\> "message=HelloWorld" | Out-File env.properties -Encoding ASCII

Straight after the powershell build step we add a new “inject” build step:

 

 

Jenkins-inject-plugin

 

Then do:

 

Jenkins-inject-plugin2

 

Finally you can append another powershell build step and call this new jenkins environment variable like this:

 

write-host "variables have been injected"
write-host "the message is $env:message"

Note: This, this technique of using envinject plugin isn’t limited to powershell, the equivalent should work with other languages e.g. with python and ruby.

Note: you can also pass the variables stored in env.properties to other jobs by triggering them from the current job. You can do because these types of build steps natively supports a properties file, e.g. :

buildstep-trigger-anotherjob

 
here’s an example of a post build step triggering another jenkins job:

jenkins-postbuildstep
 

Hence this env.properties file has two possible uses, it creates new environment variables midway through current jobs (using the EnvInject) plugin, and it can be used as an input file for triggering other jenkins jobs.

Warning: You might have problems getting your powershell script working becuase of “second-hop”/remoting related issues. The way I have managed to fix this is by not using the jenkins powershell plugin. Instead I ran the powershell script inside an ordinary “execute windows batch command build step”:

windows-batch-command-to-run-powershell-script-in-jenkins

Note, to do the above, you first need to save your powershell code into a .ps1 file and add it to your code repo, in order to make it available via your jenkins workspace.