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

4

6

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 !

Aerology answered 13/11, 2014 at 10:2 Comment(1)
Seems that nobody has an answer :-/Aerology
T
7

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
}
Thyestes answered 22/7, 2015 at 13:13 Comment(0)
E
3

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;
}
Epiphenomenalism answered 23/6, 2016 at 15:6 Comment(0)
W
0

The parameters of a Parameterized job are not environment variables!! They can't be accessed that way. If you want to access them, use the Environment Inject Plugin to add them to the environment as a build step and then follow these steps. It should fix your problem.

Wittman answered 8/7, 2015 at 14:24 Comment(0)
S
0

build.getEnvironment(TaskListener.NULL) if injecting environment variables as a prior step. If looking just to get the build parameters, build.buildvariables should work just fine.

Siliqua answered 1/5, 2017 at 17:44 Comment(1)
For me there was no need to specify TaskListener.NULL as parameter, just build.getEnvironment() did suffice. Although I don't understand why because indeed the getEnvironment() is defined with nonnull argument (github.com/jenkinsci/jenkins/blob/…).Ella

© 2022 - 2024 — McMap. All rights reserved.