Jenkins: How to use a variable from a pre-build shell in the Maven "Goals and options"

jenkins, maven

Solution

You're on the right track here, but missed a third feature of the EnvInject-Plugin: The "Inject environment variables" build step that can inject variables into following build steps based on the result of a script or properties.

We're using the EnvInject plugin just like that; A script sets up a resource and communicates its parameters using properties that are then propagated by the plugin as environment variables.

i.e. setting up a temporary database for the build:

Problem

I have a Maven job in Jenkins. Before the actual build step I have an "Execute shell" pre-build step. In that shell I set a variable: ``` REVISION=$(cat .build_revision) ``` I would like to use that variable in the Maven build job in "Goals and options": ``` clean install -Drevision=${REVISION} ``` But that does not work! The "Drevision" is set to "${REVISION}" not the actual value of ${REVISION}. Output: ``` Executing Maven: -B -f /home/gerrit/.jenkins/jobs/<job_name>/workspace/pom.xml clean install -Drevision=${REVISION} ``` It works with Jenkins environment variables: ``` clean install -Dbuild=${BUILD_NUMBER} ``` It sets "Dbuild" to the actual build number. Output: ``` Executing Maven: -B -f /home/gerrit/.jenkins/jobs/<job_name>/workspace/pom.xml clean install -Dbuild=54 ``` My question: How to use a shell variable in Maven "Goals and options"?? EDIT: I tried using Jenkins EnvInject Plugin to "Inject environment variables" after the pre-build shell, and my variable is now accessible by e.g. post-build shells, but it is still not available in Maven "Goals and options". Then it is possible to set "Inject environment variables to the build process" using the EnvInject Plugin, which actually makes those variables available in Maven "Goals and options", but those are set right after SCM checkout, i.e. before pre-build steps, and do not support expression evaluations.

Original source