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
echo "KEEP_ME=Fubar" >> build.prop
The second build step we declare the environmental variable and assign it a variable:
echo "build 2"
echo "Did it make it"