Run Jenkins ant build in a special shell environment

Our internal build system uses a shell script to set up the environment for creating projects. Then the actual build tools (ant or make) can reference the environment variables to configure various things. Essentially, it does:

$ /path/to/setup_env.sh . [build env] $ ant compile 

Note that the first command starts and initializes a new shell and expects that all subsequent build operations will be performed in this shell.

Now I'm trying to repeat the same thing in Jenkins. How to run a shell script and then execute the next step of ant build in the same environment?

The built-in Execute Shell built-in module and EnvInject plugin did not help, as they canceled any changes in the environment before moving on to the next build step.

I would prefer not to modify the ant build file, as it should continue to work in the current internal build system.

+6
source share
2 answers

This is a β€œsolution” designed for us. The basic idea is that the setup_env.sh script launches a new shell in which it exports a bunch of environment variables. We needed access to these variable definitions. So, we made three parts of Jenkins Build:

Step 1 - Run the Shell

Use the "Execute Shell" Jenkins to run our setup_env.sh script. Then load the recently launched shell with a simple python script that unloads the environment into 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 - Input Environment Variables

Now we use EnvInject Plugin to input environment variables from the file that was dumped in the previous step. The configuration here is simple, just specify the file name of the reset properties as the value of the Properties File Path field.

Step 3 - Call Ant

Here we run the normal ant build. Since the environment now contains all the necessary definitions, the assembly completes as usual.

+5
source
+1
source

Source: https://habr.com/ru/post/908484/


All Articles