Shell script to accept “jenkins job name” and trig

2019-08-11 07:00发布

问题:

Hi I have written a shellscript which accepts the "jenkins job name" and starts triggering that build via command line , In my Jenkins server I have different jobs which differs in count and names of parameters.

$build_name= Jenkins_job_name   ##(which is the only input)
java -jar jenkins-cli.jar -s http://mydomain:8080/ build $build_name -s -p    PARA1=$paravalue

will work only if i am giving the exact name and count of the parameter which the job expects

I want to generalize this build triggering script, which fetches the names of parameter for a specific job,accepts it and then triggers the build

NB: When I try the above script with different parameter name ("KEY1" instead of "PARA1"), I am getting the message like:

'KEY1' is not a valid parameter. Did you mean "PARA1"?

回答1:

You can use the get-job $build_name on the cli to get the job configuration

The parameters are under:
<parameterDefinitions> <name>PARA1</name>

There will be multiple instances of <name>...</name>

Alternatively, you can make a call to
http://mydomain:8080/job/$build_name/api/json

The parameters will be in format:
"parameterDefinitions":[{..., "name":"PARA1", ...}, ...]

There will be multiple instances of {..., "name":"...", ...},

Parse the file to get all parameters defined for the job. The json format is either for shell since it is all on a single line. A RegEx can detect the start of parameterDefinitions and get the following name values, before the end of brackets.



标签: shell jenkins