how to get parallel build job result in jenkins - jenkins

My Jenkins job run multiple builds in parallel as below:
def branches = [:]
for (int i = 0; i < 4; i++) {
def index = i
branches["branch${i}"] = {
build job: 'Test', parameters: [
string(name: 'param1', value:'test_param'),
string(name:'dummy', value: "${index}")]
}
}
parallel branches
For the above code I want to print all build result. So how can I get build result (e.g. SUCCESS, FAILURE...) of all parallel jobs?

If you want to print all branches result in the same console
you can do it like this
def branches = [:]
for (int i = 0; i < 4; i++) {
def index = i
branches["branch${i}"] = {
build job: 'Test', parameters: [
string(name: 'param1', value:'test_param'),
string(name:'dummy', value: "${index}")]
}
println currentBuild.result
}
parallel branches
currentBuild.result holds status of the build so if you print it in each branch you will get what you need. If the stage

Related

Jenkins run few jobs and get their result

I run job like this:
def job = build job: job_name, parameters:
[
string(name: 'Param1', value: Value1),
string(name: 'Param2', value: Value2),
string(name: 'Param3', value: Value3),
], wait: false
return job
I use wait: false because I need to run few in same time and then waiting for all of them finished. But in this case job is null because I use wait: false.
Maybe do you know other way to run multiply jobs in same time and get their objects ?
Other way that I try:
def buildsNumberStart = GetLastDockerBuildNumber(DockerBuildJobName)
def finishedJobs = overrideBranches.collect { item -> RunJobBuild(item.Tag, author, GetImageNameFromRepository(item.DockerRepository), item.Name, item.Branch, item.Repository, DockerBuildJobName)}
sleep(30)
def buildNumberFinish = GetLastDockerBuildNumber(DockerBuildJobName)
while(true)
{
if (IsAllDockerBuildJobsFinished(buildsNumberStart, buildNumberFinish, DockerBuildJobName))
{
break;
}
sleep(5)
}
for (build in GetFinishedJobs(buildsNumberStart, buildNumberFinish, DockerBuildJobName))
{
def listener = build.getListener()
build
.getEnvironment(listener)
.each
{
// HERE IS PROBLEM, I saw just environment variables that
// set on start and didn`t see that I added in pipeline
println it
}
}
}
#NonCPS
def GetFinishedJobs(buildNumbersStart, buildNumberFinish, String dockerBuildJobName)
{
return jenkins.model.Jenkins
.instance
.getItemByFullName(dockerBuildJobName)
.builds
.findAll { it.number > buildNumbersStart && it.number <= buildNumberFinish}
}
I find solution https://stackoverflow.com/a/40148397/14392639
for my case its looks like:
def jobs = [:]
some_source.each
{
item -> jobs[some_source.Name] = build job: job_name, parameters:
[
string(name: 'Param1', value: Value1),
string(name: 'Param2', value: Value2),
string(name: 'Param3', value: Value3),
]
}
parallel jobs

How can I run each iteration of a for loop parallel in Jenkins pipeline?

I have a pipeline job which run some sequence of action (Eg; Build >> Run >> Report). I ahave put this sequence in a for loop as I can get a parameter how many times I should repeat the same sequence. Please find the sample code I have written.
for (int i = 0; i < <param_val>; ++i){
node{
stage('Build') {
build 'Build'
}
stage('Run') {
build 'Run'
}
stage('Reporting') {
build 'Reporting'
}
}
}
Now my code is waiting for one complete sequence to happen and then continue to run the next sequence. That is time consuming. I have more slave agents and can run the sequence in parallel. How can I run each iteration of a for loop parallel?
I have thought of a solution :
have a pipeline having the actual sequence
node{
stage('Build') {
build 'Build'
}
stage('Run') {
build 'Run'
}
stage('Reporting') {
build 'Reporting'
}
}
have another pipeline with the for loop which will trigger the 1st pipeline with wait: false:
for (int i = 0; i < <param_val>; ++i){
build(job: 'pipeline-1', wait: false)
}
Does this work ? Or do we have a better option to do the same with single pipeline ?
Put the code inside the loop in a closure:
def oneNode = { c ->
node {
build job: 'Build', parameters: [string(name: 'Component', value: c)]
build 'Run'
build 'Reporting'
}
}
Then make a map of all the closures you want to run simultaneously:
def jobs = [:]
def components = params.Componets.split(",")
for (int i = 0; i < components.size(); ++i) {
def component = components[i].trim()
jobs[component] = {
oneNode(component)
}
}
And finally use the parallel step with the resulting map:
stage('Build, run, report') {
<the code from the above steps>
parallel jobs
}
This worked for me.
def jobs = [:]
def components = params.Componets.split(",")
stage('Build, run, report') {
for (int i = 0; i < components.size(); ++i) {
def index = i
def component = components[index].trim()
jobs[i] = {
node {
build job: 'Build', parameters: [string(name: 'Component', value: component)]
build 'Run'
build 'Reporting'
}
}
}
parallel jobs
}

"java.lang.IllegalArgumentException: Expected a closure or failFast" Exception on running parallel builds

I have a pipeline which has two parallel run in sequence.
Setup in many slaves in parallel and after all the machine setup is done I have a Build and run stage as coded below. But when I tried running the script I am getting error java.lang.IllegalArgumentException: Expected a closure or failFast but found 0=org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper.
Code for reference:
def slaves = params.slaves
stage('Setup'){
for(int i=0; i<slaves.size(); ++i){
def slave = slaves[i]
setup_builds[i] = build job: 'setup', parameters: [[$class: 'LabelParameterValue', name: 'TestMachine', label: slave]]
}
parallel setup_builds
}
stage('Build, run) {
for (int i = 0; i < 4; ++i){
def index = i
builds[i] = {
stage('Build') {
build job: 'Build'
}
stage('Run') {
build job: 'Run', parameters: [string(name: 'index', value: index)]
}
}
}
parallel builds
}
I have tried using setup_builds.failFast = true and builds.failFast = true before parallel setup_builds and parallel builds. But even that didn't fix the issue.
I think one of the issues is that it expects a closure on the setup_builds level:
setup_builds[i] = { build job: 'setup' ... }

Parameterized pipeline build to build more than one job in jenkins

Parameterized pipeline job has to take more than one job name as parameter and start parameterized jobs in parallel, I tried below code but it isnt working
def String[] jobs;
stages {
stage('stage1') {
steps {
script {
jobs = jobnames.split(',');
for (ii = 0; ii < jobs.size(); ii++) {
build job: 'startjob_${jobs[ii]}', parameters: [string(name: 'BRANCH',value: String.valueOf(BRANCH)),string(name: 'CHANGENUM',value: String.valueOf(CHANGENUM))]
}
This code is working, but not the way I expected, I want to start all jobs in parallel. but its scheduling one job after other.
can anyone help me with this
Try this
builds = [:]
for (ii = 0; ii < jobs.size(); ii++) {
builds << [
"startjob_${jobs[ii]}": { ->
build job: "startjob_${jobs[ii]}", parameters: [string(name: 'BRANCH', value: String.valueOf(BRANCH)), string(name: 'CHANGENUM', value: String.valueOf(CHANGENUM))]
}
]
}
parallel builds

Jenkins pipeline - No such DSL method 'build'

I am using below Groovy Script in Jenkins Pipeline to call a Free style job but it ends up with "No such DSL method 'build'" error.
node{
def branches = [:]
List rows =["Test2", "Test1"]
for (int i = 0; i <rows.size(); i++)
{
def index = i
String db = rows[i]
branches["branch${i}"] = {
build job: 'CopyFile', parameters: [[$class:
'StringParameterValue', name: 'DatabaseName', value: db], [$class:
'StringParameterValue', name:'dummy', value: "${index}"]]
}
}
parallel branches
}
Installing "Pipeline Build Step Plugin" resolved this issue
https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Build+Step+Plugin

Resources