简体   繁体   中英

Jenkins groovy script to get triggered build number

I'm trying to write down a script that will get the Build Number of a Build that has been triggered by another job. For example: I have a build job that calls two other jobs(Call/trigger builds on other project). When the main job is finished with success I would like to get the number of the first build job that was triggered from within. The script I'm trying to run founds the main job, however I can't get in any way the build number of the triggered job.

 def job = jenkins.model.Jenkins.instance.getItem("Hourly")
job.builds.each {
def build = it
if (it.getResult().toString().equals("SUCCESS")) {The rest of the code should go here!}

I was trying to find it in the Jenkins java-doc API and online, however without any luck. Can somebody please help me with that?

PS The script runs after the job has finished(triggered when needed only).

You can parse the build number (of the child job) from the build log (of the parent job). For example:

j = Jenkins.getInstance();
jobName = "parentJobName";
job = j.getItem(jobName);
bld = job.getBuildByNumber(parentBuildNumber);

def buildLog = bld.getLog(10); //make sure you read enough lines
def group = (buildLog =~ /#(\d+) of Job : childJobName with/ );
println("The triggered build number: ${group[0][1]}");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM