Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Jenkins environment variable using Groovy

Tags:

jenkins

groovy

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?

like image 599
themaniac27 Avatar asked May 02 '12 12:05

themaniac27


People also ask

How do I set environment variables in Groovy?

Download a binary distribution of Groovy and unpack it into some folder on your local file system. Set your GROOVY_HOME environment variable to the directory where you unpacked the distribution. Add GROOVY_HOME/bin to your PATH environment variable. Set your JAVA_HOME environment variable to point to your JDK.

How do I get environment variables in Groovy Jenkins?

In "Manage Jenkins" -> "Configure System" -> "Global Properties" -> "Environment Variables" I added "ALL_NODES_ENVVAR".

How do I declare a variable in Jenkins Groovy?

Jenkins Pipeline: How to Define a Variable – Jenkins Variables. Variables in a Jenkinsfile can be defined by using the def keyword. Such variables should be defined before the pipeline block starts. When variable is defined, it can be called from the Jenkins declarative pipeline using ${...} syntax.


2 Answers

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'.

import hudson.model.*    def env = System.getenv() def version = env['currentversion'] def m = version =~/\d{1,2}/ def minVerVal = m[0]+"."+m[1]    def pa = new ParametersAction([   new StringParameterValue("miniVersion", minVerVal) ])    // add variable to current job Thread.currentThread().executable.addAction(pa) 

The variable will then be accessible from other build steps. e.g.

echo miniVersion=%miniVersion% 

Outputs:

miniVersion=12.34 

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.keepUndefinedParameters to true. This is potentially very unsafe and intended as a short-term workaround only.

java -Dhudson.model.ParametersAction.keepUndefinedParameters=true -jar jenkins.war 

To allow specific, known safe parameter names to be passed to builds, set the system property hudson.model.ParametersAction.safeParameters to a comma-separated list of safe parameter names.

e.g.

java -Dhudson.model.ParametersAction.safeParameters=miniVersion,FOO,BAR -jar jenkins.war 

And in groovy these two lines should be written this way:

System.setProperty("hudson.model.ParametersAction.keepUndefinedParameters","true"); System.setProperty("hudson.model.ParametersAction.safeParameters","miniVersion,FOO,BAR");   
like image 168
Nick Grealy Avatar answered Sep 27 '22 22:09

Nick Grealy


You can also define a variable without the EnvInject Plugin within your Groovy System Script:

import hudson.model.* def build = Thread.currentThread().executable def pa = new ParametersAction([   new StringParameterValue("FOO", "BAR") ]) build.addAction(pa) 

Then you can access this variable in the next build step which (for example) is an windows batch command:

@echo off Setlocal EnableDelayedExpansion echo FOO=!FOO! 

This echo will show you "FOO=BAR".

Regards

like image 38
stede Avatar answered Sep 27 '22 22:09

stede