Our internal build system uses a shell script to setup the environment for building projects. Then the actual build tools (ant or make) can reference environment variables for configuring various things. In essence, it does:
$ /path/to/setup_env.sh .
[build env] $ ant compile
Note that the first command launches and initializes a new shell and expects all subsequent build operations to be performed in that shell.
Now I am trying to replicate the same within Jenkins. How do I run a shell script and then have the subsequent ant build step take place in the same environment?
The ‘Execute Shell’ built-in as well as the EnvInject plugin didn’t help since they discard any changes to the environment before moving to the next build step.
I’d prefer not to modify the ant build file since the same should continue to work in the current internal build system.
This is a “solution” that worked out for us. The key idea is that the
setup_env.shscript launches a new shell in which it exports a bunch of environment variables. What we needed was access to those variable definitions. So we did a three part Jenkins Build:Step 1 – Execute Shell
Use the ‘Execute Shell’ Jenkins built-in to run our
setup_env.shscript. Then feed the newly launched shell a simple python script that dumps the environment to a file.Step 2 – Inject Environment Variables
Now we use the EnvInject Plugin to inject environment variables from the file dumped in the previous step. The config here is simple, just specify the dumped properties file name as the
Properties File Pathfield value.Step 3 – Invoke Ant
Here, we kick off the normal
antbuild. Since the environment now contains all the required definitions, the build completes as normal.