My project takes in a version number (separated by ‘.’ or ‘_’). I tried writing a Groovy script that creates a Jenkins environment variable using only the first two of these numbers:
//Get the version parameter
def env = System.getenv()
def version = env['currentversion']
def m = version =~/\d{1,2}/
env = ['miniVersion':m[0].m[1]]
Am I doing this correctly? Can I even create a new environment variable? Is there a better solution to this?
Jenkins 1.x
The following groovy snippet should pass the version (as you’ve already supplied), and store it in the job’s variables as ‘miniVersion’.
The variable will then be accessible from other build steps. e.g.
Outputs:
I believe you’ll need to use the "System Groovy Script" (on the Master node only) as opposed to the "Groovy Plugin" – https://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin#Groovyplugin-GroovyScriptvsSystemGroovyScript
Jenkins 2.x
I believe the previous (Jenkins 1.x) behaviour stopped working because of this Security Advisory…
Solution (paraphrased from the Security Advisory)
It’s possible to restore the previous behaviour by setting the system property
hudson.model.ParametersAction.keepUndefinedParameterstotrue. This is potentially very unsafe and intended as a short-term workaround only.To allow specific, known safe parameter names to be passed to builds, set the system property
hudson.model.ParametersAction.safeParametersto a comma-separated list of safe parameter names.e.g.
And in groovy these two lines should be written this way: