Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a pre-send script retrieve parameters of a parameterized Jenkins job?

Tags:

email-ext

I would like to cancel email sending based on the value of a boolean parameter of a parameterized job. The parameter is named "skip.email".

I tried to write a pre-send script with following content but it doesn't work :

def env = System.getenv()
logger.println("Should I skip email ? " + env['skip.email'])
cancel = env['skip.email']

Here is what I see in the logs :

Should I skip email ? null

I tried to print out all environment variables, but none of the parameters of my parameterized Jenkins job are in the list.

Please help me out, thank you in advance !

like image 1000
Kraal Avatar asked Nov 13 '14 10:11

Kraal


Video Answer


2 Answers

The pre-send script provides a variable named "build", which is of a type that inherits from AbstractBuild. Use the getBuildVariables method to retrieve a Map that includes the parameterized variables.

Example

I have a parameterized variable named "target" that describes a deployment environment. I want emails to only be sent to QA if the target environment was QA's environment.

if (!build.getBuildVariables().get("target").equals("qa")) {   
  // cancel variable cancels the email send when set to true
  cancel = true
}
like image 67
tday03 Avatar answered Oct 13 '22 22:10

tday03


tday03 answer seems correct but is not working for me, I'm injecting the vars with Environment Injector Plugin, I don't know if that's the issue. Anyway I ended up with this script:

def env = build.getEnvironment()
String official = env['OFFICIAL'];

if ((official != null) && official.equals("true")) {
  cancel = false;
} else {
  cancel = true;
}
like image 40
Luca Faggianelli Avatar answered Oct 13 '22 21:10

Luca Faggianelli