Jenkins EnvInject Plugin and Powershell

I have been working on larger projects in Jenkins. I hope to have a post put together in the next few weeks that shows better uses for multiple build steps. For now I thought I’d talk about a plugin I came across and some of the quirks you’ll run into when trying to do Powershell, Jenkins and the EnvInject plugin.

The plug-in allows you to create a file and define environment variables. These variables can persist between build steps and builds. It also allows you to override the default ENV variables Jenkins gets from the server. This can be used to enforce an environment over different Jenkin servers. For example if you need to modify the windows Path variable ($env:Path in jenkins) you can add a config file that makes the modification temporarily for the build rather than making a permanent change to the Windows OS. You can read more on the Environment Inject Plugin wiki.

For this post I’ll just use a few simple scripts to create a new Environment variable and hand it to a second build step. You could use a CSV file to do this as well, but I like the uniform cleanliness of using the Environment variables. It simplifies script re-use for me, your mileage of course may vary with your work flow.

 

First we will install the plugin. You’ll need to go to Manage Jenkins → Manage Plugins → Available

Filter for “Envinject” and install the plugin:

 

The first build we won’t be enabling the plugin. This is to show the default behavior of Jenkins to contrast the difference between the plug in behavior. Click New Item. I named the build “Env-Inject Example 1” and used a Free style project. Once created the build properties are as show:

The non-working code can be found here on pastebin.

The code simple outputs some text to so we can keep track of what is going on in from the console output.

Unsurprisingly we see a blank spot before Finished: SUCCESS where the $Keep_me variable didn’t make it to the second build step.

The Environment Inject Plugin requires a file to read from. We can define one anywhere and provide the path. For simplicity I’ll add the file to the workspace for the project. The workspace path can be seen on our console output above. Look for the line “Building in Workspace” for my configuration it is found on “C:\Program Files\Jenkins\workspace\Env-Inject Example1”.  Lets create a blank file on the Jenkins Server called build.prop

Once the file is in place go back to jenkins and we will add  “Inject environment Variables” to the build.

We will then move the step to the middle of the Jenkins build like so:

With Injected file at the top of the build we will need to change the code to write to the file. The code (showed above) can be found on pastebin here.

For the first build step the  the changes are at the end step:


echo "build 1"
$keep_me = "fubar"
echo "do more stuff"
$save_variable = "Keep_me=" + $keep_me
$save_variable |Out-file build.prop -Encoding ASCII

We output the variable with the environment tag we want. In this case I called it “Keep_me” just like the variable name. I then output to the build.prop file. Notice how I didn’t define a path. Since we put the original file in the workspace Jenkins checks there first for assets. If you wanted to output this to a different location, simply define the path to use. I also declared explicitly to encode the output as ASCII. Powershell defaults to UTF8 and Jenkins doesn’t like that encoding. So writing the code like:


echo "KEEP_ME=Fubar" >> build.prop

would fail since the encoding would be utf and Jenkins would miss interpret the result.

The second build step we declare the environmental variable and assign it a variable:

echo "build 2"
$keep_me =$env:Keep_me
echo "Did it make it"
echo "$Keep_me"

This is the same way you would declare any other environment variable available to Jenkins. For example the Path environmental variable found in Windows is $env:Path.
When we run the build we see that the console output now shows the expected output just above Finished: Success is now “fubar” like we declared.
If you go back to your build.prop file you will see that it has now been set with the environmental variable.
That is a persistent change. You can overwrite the variable so each build is different, but the file stays with the variables from the last build. You can also prep multiple overriding Environment variables from within the file.
Besides the persistent variables in the file a few other things you should know.
If the file is missing you will see errors like:
To help with troubleshooting and to keep track of what environmental variables were used with each build step a new option has been added to the Jenkins post build report:
The environmental variables used for the build are reported. As you can see the Keep_Me variable we used is shown:
How are you keeping track of variables across builds in Jenkins? Any other preferred methods? Let me know in the comments!
Thanks for reading.
I_Script_Stuff

Sharing is caring!

2 thoughts on “Jenkins EnvInject Plugin and Powershell

  1. Hi and thanks for the nice tutorial but I think there is a typo in the code fragment:

    1: echo “build 1”
    2: $keep_me = “fubar”
    3: echo “do more stuff”
    4: $save_Variable = “Keep_me=” + $keep_me
    5: $save_variable |Out-file build.prop -Encoding ASCII

    in line 4 the variable is written with a capital “V” and in line 5 with a small “v”

  2. Fixed it. Powershell isn’t case sensitive for variables, but I do like to be consistent.

    Thank you.

Leave your comment

20 + six =