Jenkins pipeline - No such DSL method 'build' - jenkins

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

Related

"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' ... }

Not serializable error for jenkins declarative pipeline

I am trying to trigger my email promotion job from my pipeline which extracts the repo name from Jenkins messages. But not able to resolve the SerializableException error for this block. Any help is greatly appreciated.
post{
success{
script{
#NonCPS
//upstream_job_name = null
def manager = manager.getLogMatcher('.*Obtained Jenkinsfile from git (.*)$')
if(manager.matches()){
def gitMsg=manager.group(1)
gitrepo = "${gitMsg}"
echo gitrepo
def upstream_job_name = gitrepo.split("/")[4].replace(".git", "")
println upstream_job_name
}
build job: 'job-approval' , parameters: [[$class: 'StringParameterValue', name: 'upstream_job_name', value: upstream_job_name]]
}
}
}
Below is the error messages i am receiving :
[Pipeline] // script
Error when executing success post condition:
java.io.NotSerializableException: java.util.regex.Matcher
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:926)
at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
You need to release manager immediately after using. More detail can find in this post
script{
//upstream_job_name = null
def manager = manager.getLogMatcher('.*Obtained Jenkinsfile from git (.*)$')
if(manager.matches()){
def gitMsg=manager.group(1)
gitrepo = "${gitMsg}"
echo gitrepo
def upstream_job_name = gitrepo.split("/")[4].replace(".git", "")
println upstream_job_name
}
manager = null
build job: 'job-approval' ,
parameters: [
[$class: 'StringParameterValue', name: 'upstream_job_name', value: upstream_job_name]
]
}

How can I get the node name where the upstream job has run in the downstream job in pipeline

I have multiple nodes with 'build_run' label (Eg; node1, node2 , node3). So when I run this pipeline I can't be sure that 'Build' and 'Run' job has run in same node. 'Build' can happen in 'node1' and 'Run' can happen in 'node3'. I want both Build and run happen in the same node. But I don't want to hard code the same.
I want to know which node has picke up by 'Build'. So that I can pass it as a node parameter to Run.
How can I solve this?
stage('Build, run) {
for(int i=0; i<4; ++i){
def builds = {
stage('Build') {
build job: 'Build', parameters: [[$class: 'LabelParameterValue', name: 'TestMachine', label: 'build_run']]
}
stage('Run') {
build job: 'Run', parameters: [[$class: 'LabelParameterValue', name: 'TestMachine', label: 'build_run']]
}
}
}
}
parallel builds
I have used something like below and it worked for me. (Used rawBuild.getEnvironment()['NODE_NAME'] to get the node in which the job has run.)
def node_to_use = ""
stage('Build, run) {
for(int i=0; i<4; ++i){
def builds = {
stage('Build') {
def build_var = build job: 'Build', parameters: [[$class: 'LabelParameterValue', name: 'TestMachine', label: 'build_run']]
node_to_use = build_var.rawBuild.getEnvironment()['NODE_NAME']
}
stage('Run') {
build job: 'Run', parameters: [[$class: 'LabelParameterValue', name: 'TestMachine', label: node_to_use]]
}
}
}
}
parallel builds

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 job DSL guard-rescue in pipeline

Lately, I'm working on a project where I'm swapping deprecated build flows to build pipeline in several job DSL generated Jenkins.
One particular problem was to swap the build flows guard-rescue mechanism to pipeline syntax. I'm curious what you think about my solution:
See the following flow DSL:
guard {
b = build("parameterised_job")
} rescue {
build("analyzer_job",
PARAMETER_ONE:b.environment.get("PARAMETER_ONE"),
PARAMETER_TWO:b.environment.get("PARAMETER_TWO")
)
}
I created the following alternative with the pipeline syntax:
pipeline {
agent any
stages {
stage("build") {
steps {
script {
def b = build(job: "parameterised_job", propagate: false)
build(job: "analyzer_job",
parameters:
[[$class: 'StringParameterValue', name: 'PARAMETER_ONE', value: b.buildVariables.PARAMETER_ONE],
[$class: 'StringParameterValue', name: 'PARAMETER_TWO', value: b.buildVariables.PARAMETER_TWO]])
if(b.result == 'FAILURE') {
error("${b.projectName} FAILED")
}
}
}
}
}
}

Resources