Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Jenkins ant build within special shell environment

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.

like image 300
Deepak Sarda Avatar asked Feb 15 '12 02:02

Deepak Sarda


People also ask

Does Jenkins support Ant scripts?

Jenkins integrates with multiple build tools such as Maven, Gradle, and Ant. In this video, Jenkins expert Kevin Bowersox demonstrates how to automate project builds with Apache Ant, a basic and very useful addition to any developer's continuous integration toolbox.


2 Answers

This is a "solution" that worked out for us. The key idea is that the setup_env.sh script 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.sh script. Then feed the newly launched shell a simple python script that dumps the environment to a file.

/path/to/setup_env.sh . <<< 'python <<SC
print "Exporting env to buildenv.properties file"
import os
f = open("buildenv.properties", "w")
env = os.environ
for k in env:
  f.write("%s=%s\n" % (k, env[k]))
f.close()
print "Done exporting env"
SC'

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 Path field value.

Step 3 - Invoke Ant

Here, we kick off the normal ant build. Since the environment now contains all the required definitions, the build completes as normal.

like image 165
Deepak Sarda Avatar answered Oct 20 '22 16:10

Deepak Sarda


Try EnvInject Plugin.

like image 45
malenkiy_scot Avatar answered Oct 20 '22 16:10

malenkiy_scot