Jenkins: setting up a the environment for a Windows/Microsoft compilation job

There are a couple of different ways of injecting environment variables:

Within the “Build Environment” / “Inject environment variables to the build process” section of a Jenkins job (provided by the EnvInject plugin) you may me tempted to make use of a file named by “Properties File Path” that adds settings to your environment variables, e.g. “c:jenkins-slaveslave-env.txt“.

As opposed to properties injection through such file using a section “Properties Content” makes your environment set up more obvious and transparent. If you have a lot of jobs with an identical subset of properties, it certainly makes sense to have them within such a shared file. But there are also situations you can neither handle through “Properties File Path” nor through “Properties Content“:

For using Microsoft Visual Studio’s C compiler on the command line you ought to call their vcvarsall.bat. Do not try to set up your compilation environment (INCLUDE, Path, LIB, LIBPATH, SDK, …) yourself! You can neither use “Script File Path” nor “Script Content” for that, because “adding or overriding environment variables in the script doesn’t have any impacts in the build job”.

This is what I suggest: Prepare the call of vcvarsall.bat within the section “Build Environment” / “Properties Content” instead.

PF=C:Program Files
PFx86=C:Program Files (x86)
VSINSTALLDIR=%PFx86%Microsoft Visual Studio 10.0

Within the section “Build” / “Execute Windows batch commandcall vcvarsall.bat:

call "%VCINSTALLDIR%vcvarsall.bat"

Variables set up through vcvarsall.bat that you may want to make use of:

  • WindowsSdkDir=…Microsoft SDKsWindows…

If you want to call ant as a separate build step (“Add build step“), this is not going to work, as the environment set up through vcvarsall.bat is not available within that separate build step. That’s unfortunate, but that’s the way it is. You rather want to call ant explicitly within the same Windows batch:

call "%VCINSTALLDIR%vcvarsall.bat"

cd src || exit 1
"%PF%apache-ant-1.9.6binant.bat" -file build.xml && exit %%ERRORLEVEL%%

Good old make checked each action line’s exit code, and if an action had a non-zero exit code, make stopped and told you why it stopped. You could circumvent that globally through the command line option “-i“, you could circumvent that locally through an action line prefix: ““.

Windows batch jobs (within a Jenkins job) do not run within a framework like that. You have to check exit codes yourself, add “|| exit 1” wherever appropriate. If you don’t, certain problems do not get recognised.

This site uses Akismet to reduce spam. Learn how your comment data is processed.