Issue
I have Multijob structure:
Master MultiJob Project (Job)
|----- Phase 1
|------> JOB A
|------> JOB D
|----- Phase 2
|------> JOB B
|------> JOB D
|----- Phase 2
|------> JOB C
Job D is always failed, but it's Ok for this project. How I can exclude result job D from final results? (Because all other jobs is success, but final result is FAILED)
Solution
You want to have the Master MultiJob Project (Job)
successful irrespective of JOB D
execution result. But, JOB A
, JOB B
and JOB C
should be successful.
You can make the Master Multijob Project (Job)
successful by using Groovy Postbuild Plugin.
Once you install this plugin in Jenkins, you will get Groovy Postbuild
step in the Post-build Actions
section. Choose Groovy Postbuild
step and it will give an option to add Groovy scripts to manipulate your job behavior.
Add the following script there:
if( manager.build.@result == hudson.model.Result.FAILURE){
errpattern = ~/[FAILURE].*/;
manager.build.logFile.eachLine{ line ->
errmatcher=errpattern.matcher(line)
if (errmatcher.find()) {
manager.build.@result = hudson.model.Result.SUCCESS
}
}
}
What the above script will do is change the build status of your parent job to success if it fails. But, the issue here is it will change the build status to success for any child job failure in your phases.
You only want to ignore the FAILURE status of JOB D
not the other jobs. So, what we can do is determine the execution results of JOB A
, JOB B
and JOB C
. If any of them fails we can prevent the Groovy script from changing the build status of your parent job to SUCCESS.
Now, to determine the build result for JOB A
,JOB B
and JOB C
We can use the following:
JOBA_BUILD_STATUS=$(curl --silent "http://<jenkins_URL>/job/JOBA/$BUILD_ID/api/json" | jq -r '.result')
JOBB_BUILD_STATUS=$(curl --silent "http://<jenkins_URL>/job/JOBB/$BUILD_ID/api/json" | jq -r '.result')
JOBC_BUILD_STATUS=$(curl --silent "http://<jenkins_URL>/job/JOBC/$BUILD_ID/api/json" | jq -r '.result')
In the Build
section of your multi-job configuration, after all the phases add Execute shell
step and add the above scripts to it.
Now, let's tweak the groovy script to ignore JOB D
's build result and consider the build result for JOB A
,JOB B
and JOB C
.
if((manager.build.@result == hudson.model.Result.FAILURE) && ("SUCCESS".equals(manager.build.buildVariables.get("JOBA_BUILD_STATUS"))) && ("SUCCESS".equals(manager.build.buildVariables.get("JOBB_BUILD_STATUS"))) && ("SUCCESS".equals(manager.build.buildVariables.get("JOBC_BUILD_STATUS")))){
errpattern = ~/[FAILURE].*/;
manager.build.logFile.eachLine{ line ->
errmatcher=errpattern.matcher(line)
if (errmatcher.find()) {
manager.build.@result = hudson.model.Result.SUCCESS
}
}
}
Now, it will ignore the build result of JOB D
and then check if the build results for JOB A, B & C
are SUCCESS or not. If they are successful then it will change the build result of your Multijob to SUCCESS.
Answered By - ANIL