Jenkins error when calling pipeline B from Pipeline Job A - jenkins

I am calling a pipeline job (new_job) from pipeline job 'test' along with passing the variables, but it's failing with error.
Both jobs are running fine individually.
Test Job:
pipeline {
agent any
stages {
stage('Executing First job') {
steps {
sh 'echo "Running first job"'
}
}
stage('Running another job') {
when {
equals expected: true, actual: params.deploy2job
}
steps {
build job: 'new_job', parameters: [string(name: 'abc', value: qa), string(name: 'xyz', value: test)]
}
}
}
}
Error:
[Pipeline] { (Running another job)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: qa for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:251)
at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:353)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:357)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:333)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:333)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at WorkflowScript.run(WorkflowScript:19)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.delegateAndExecute(ModelInterpreter.groovy:137)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(ModelInterpreter.groovy:666)
at
....
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
Finished: FAILURE

As the error indicates you are trying to refer to a nonexistent variable named qa in your Pipline. If this is not a variable declared within the Pipeline, and if you just need to pass the values qa and test to the new_job just wrap the values with quotes.
build job: 'new_job', parameters: [string(name: 'abc', value: 'qa'), string(name: 'xyz', value: 'test')]

Related

How to add ant build properties in jenkinsfile

I am trying to create a jenkinsfile using ant script to build a salesforce project. I need to provide some build properties to execute the ant script.
Here is my jenkinsfile:
pipeline{
agent any
environment{
def antVersion ='Ant 1.9.16'
}
stages{
stage('Checkout'){
steps{
git credentialsId: '******', url: 'https://********/test_repo.git', branch: 'develop'
}
}
stage('Execute Ant Script'){
steps{
withEnv ([
["ANT_HOME=${tool antVersion}"],["readFile('./abc.txt').split('\n') as List"]
])
{
sh 'ant compile'
}
}
}
}
post {
always {
cleanWs()
}
}
}
Jenkins build fails with below error:
java.lang.ClassCastException: org.jenkinsci.plugins.workflow.steps.EnvStep.overrides expects class java.lang.String but received class java.util.ArrayList
at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:492)
Caused: java.lang.IllegalArgumentException: Could not instantiate {overrides=[[ANT_HOME=C:\Users\********\Documents\Softwares\apache-ant-1.9.16-bin], [readFile('./abc.txt').split('
') as List]]} for org.jenkinsci.plugins.workflow.steps.EnvStep
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:334)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:302)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:193)
Finished: FAILURE
Can anyone help me resolve this?

Failure to trigger freestyle job from declarative pipeline

I have a Jenkins declarative pipeline job that is required to trigger a downstream freestyle job. I do that using this snippet:
build job: 'DL_TVG_Backward_Compatibility_Verification',
parameters: [booleanParam(name: 'CHECK_CAM2', value: true),
[$class: 'ListSubversionTagsParameterValue',
name: 'CAM2_GOLDEN_TAG', tag: '',
tagsDir: '<snip>/tags'],
string(name: 'CAM2_SCENARIOS', value: ''),
booleanParam(name: 'CHECK_CAM3', value: false),
<snip>
[$class: 'NodeParameterValue',
name: 'UPSTREAM_NODE',
labels: ['jenkinswin10'],
nodeEligibility: [$class: 'AllNodeEligibility']],
string(name: 'EMAIL_RECIPIENTS',
value: '<snip>')
]
The downstream job fails:
[Pipeline] build (Building DL_TVG_Backward_Compatibility_Verification)
Scheduling project: DL_TVG_Backward_Compatibility_Verification
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
ERROR: Failed to trigger build of DL_TVG_Backward_Compatibility_Verification
Finished: FAILURE
Unfortunately no reason for the failure is given. Is there a way to get more information about the reason?
I am unsure about the line:
[$class: 'NodeParameterValue',
name: 'UPSTREAM_NODE',
labels: ['jenkinswin10'],
nodeEligibility: [$class: 'AllNodeEligibility']],
Maybe that is wrong.
Any idea why this snippet fails?
Did you check the jenkins log for errors? Looks like the Backward-Job was found by Jenkins (Consolen-Log: "Scheduling...").
Do you have a Node-Parameter UPSTREAM_NODE in your Downstream-Job deklared? Check: How to trigger a jenkins build on specific node using pipeline plugin?
Syntax of your NodeParameterValue in the build-Step looks fine. Check: How to use a parameter from NodeLabelParameter Plugin with the "build" step of Jenkins Workflow
Did you checked the configuration of the label 'jenkinswin10'? Did you already executed builds successfully on one agent of the label?

Jenkins shared pipeline java.io.NotSerializableException: hudson.plugins.sshslaves.SSHLauncher

I am having this issue with Jenkins shared pipeline. Tried with a script tag and still getting the same error. The job is running fine but adding stack trace in the log
def call(def suite = "*") {
cucumber jsonReportDirectory: env.Q3_DIR, parallelTesting: true, fileIncludePattern: "**/${suite}.json", sortingMethod: 'ALPHABETICAL'
List<String> logPaths = findFiles(glob: "**/q3.log")
if (!logPaths.empty && logPaths != null) {
log.debug("log: ${logPaths.first().path}")
if(suite != "*")
sh "mv ${logPaths.first().path} ${suite}_q3.log"
archiveArtifacts "**/*q3.log"
}
echo "Pipeline running on node: ${env.NODE_NAME}"
def currentNode =jenkins.model.Jenkins.instance.getNode(env.NODE_NAME)
/* change node lable before restore job as invoking job is asyncronous */
echo "Changing label for Node ${currentNode.getNodeName()}"
String oldLabel = currentNode.getLabelString();
echo "Old label is ${oldLabel}"
script {
currentNode.setLabelString("BeingRestored")
currentNode.save()
echo "Changed label to 'BeingRestored'"
echo "Preparing to cleanup node: ${env.NODE_NAME}"
//build job: "Restore-${env.NODE_NAME}", wait: false
build job: 'Restore-VM-Node', parameters: [[$class: 'StringParameterValue', name: 'REQUESTED_ACTION', value: 'restore'],
[$class: 'StringParameterValue', name: 'NODE_LABEL', value: "${env.NODE_NAME}"],
[$class: 'StringParameterValue', name: 'LABEL_NAME', value: "${currentNode.getNodeName()}"]]
}
}
Error details
DEBUG: log: q3.log
[Pipeline] step
Archiving artifacts
[Pipeline] echo
Pipeline running on node: server-vm2
[Pipeline] echo
Changing label for Node server-vm2
[Pipeline] echo
Changed label to 'BeingRestored'
[Pipeline] echo
Preparing to cleanup node: server-vm2
[Pipeline] build (Building Restore-VM-Node)
Error when executing always post condition:
java.io.NotSerializableException: hudson.plugins.sshslaves.SSHLauncher
at
.....
Scheduling project: Restore-VM-Node
I could not able to figure out the issue with the above code.
The issue is with TreeMap, build job parameter are passed as TreeMap with non-serializable comparators. below code change working fine
build job: 'Restore-VM-Node', parameters: [
string( name: 'REQUESTED_ACTION', value: 'restore'),
string(name: 'NODE_LABEL', value: "${env.NODE_NAME}"),
string(name: 'LABEL_NAME', value: "${oldLabel}")
], wait:false

How to use Jenkins String Parameter in pipeline

We am using Jenkins Pipeline to configure jobs in jenkins. For a bunch of jobs we need user input for which we use parameterised build where user can input parameter values and later we use the values in our .jenkinsfile in sh like
sh "./build-apply.sh ${accountnumber} ${volumename} ${vpcname} services ${snapshotid}"
This used to work with
Jenkins 2.16
Pipeline 2.3
Groovy 2.15
However, when I rebuild Jenkins to:
2.16 or latest 2.26
Pipeline 2.5
Pipeline: Groovy 2.19
The above sh stopped working. Error being
groovy.lang.MissingPropertyException: No such property: accountnumber for class: groovy.lang.Binding
Any idea what I am missing? Is the syntax not correct?
For reference full Jenkinsfile for reference
node {
// Mark the code checkout 'stage'....
stage 'Checkout'
git branch: '****', credentialsId: '***', url: '****'
stage 'Provision Volume'
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: '*****',
credentialsId: '****',
secretKeyVariable: '*****']]) {
// Run the terraform build
env.PATH = "${env.PATH}:/jenkins/terraform"
sh "./build-apply.sh ${accountnumber} ${volumename} ${vpcname} services ${snapshotid}"
}
}
Copy and paste the below code in the pipeline script
node: {
stage ('BCCdlVsLib') {
build job: 'BCCdlVsLib', parameters:
[
[$class: 'StringParameterValue', name: 'BINPATH', value: 'BINPATH'],
[$class: 'StringParameterValue', name: 'SOURCEFILE', value: 'SOURCEFILE']
]
}
In the jobs (BCCdlVsLib) enable the option "this project is parametrized" and enter 2 string parameters job_binpath,job_sourcefile.
Print the variables in the pipeline jobs
echo job_binpath
echo job_sourcefile
After run the pipeline job,will get the below output.
BINPATH
SOURCEFILE

Access builds from Parallel execution in Jenkins 2.0 Pipeline

I'm currently using the Build Flow plugin, which seems to have been abandoned in favor of Pipelines in Jenkins 2.0.
Running into some problems re-building our existing jobs using the new pipelines.
Currently, I have code similar to this:
ignore(FAILURE) {
join = parallel([
job1: {build('job1')},
job2: {build('job2')},
job3: {build('job3')}
])
}
results = [join.job1.result.toString(), join.job2.result.toString(), join.job2.result.toString()]
if(join.job1.result.toString() == 'SUCCESS') {
buildList << join.job1.lastBuild.getDisplayName()
}
The goal here is to run multiple existing jobs in parallel, and then access information about the builds that completed. This has been working without issue in the Build Flow plugin.
I have been unable find a way to access this data using the new Pipelines.
echo 'Checking streams for latest builds'
join = [:]
join['Job1'] = { build job: 'Job1', parameters: [[$class: 'StringParameterValue', name: 'TimeWindow', value: '1200']], propagate: false}
join['Job2'] = { build job: 'Job2', parameters: [[$class: 'StringParameterValue', name: 'TimeWindow', value: '1200']], propagate: false}
join['Job3'] = { build job: 'Job3', parameters: [[$class: 'StringParameterValue', name: 'TimeWindow', value: '1200']], propagate: false}
parallel join
A dump of join['Job1'] doesn't give access to an AbstractBuild or similar, the way the Build Flow plugin does. Instead, it shows:
<org.jenkinsci.plugins.workflow.cps.CpsClosure2#2eac6ed9
def=com.cloudbees.groovy.cps.impl.CpsClosureDef#59647704
delegate=WorkflowScript#3aa1807f
owner=WorkflowScript#3aa1807f
thisObject=WorkflowScript#3aa1807f
resolveStrategy=0
directive=0
parameterTypes=null
maximumNumberOfParameters=0
bcw=null>
Using the new Pipelines, is there a way to access data like job1.result, job1.lastBuild, job1.lastBuild.getDisplayName()?
a little late but you can also define the runWrapper object returned by build command in your closure and place it in a map defined outside of the parallel command.
Here's an example. Note: I am using propagate: false so that exceptions (JUnit test failures, etc) are not thrown. You would have to decide how you want to handle exceptions, try/catch/finally, etc.
Example Pipeline Job to execute (needs to be parameterized with a string param commandStr):
env.PASSED_CMD="${params.commandStr}"
stage('command-exec') {
node {
sh "${commandStr}"
}
}
Executing job (config):
buildRuns = [:]
buildResults = [:]
def buildClosure(String jobKey, String paramAValue) {
return {
def runWrapper = build(
job: 'command-test-job',
propagate: false,
parameters: [[$class: 'StringParameterValue', name: 'commandStr', value: paramAValue]]
)
buildResults."$jobKey" = runWrapper
}
}
buildRuns."job1" = buildClosure("job1", "echo 'HI' && exit 0")
buildRuns."job2" = buildClosure("job2", "echo 'HO' && exit 0")
parallel buildRuns
for(k in buildRuns.keySet()) {
def runResult = buildResults."$k"
echo "$k -> ${runResult.result}"
echo "$k -> ${runResult.buildVariables.PASSED_CMD}"
}
The build log shows:
[Pipeline] parallel
[Pipeline] [job1] { (Branch: job1)
[Pipeline] [job2] { (Branch: job2)
[Pipeline] [job1] build (Building command-test-job)
[job1] Scheduling project: command-test-job
[Pipeline] [job2] build (Building command-test-job)
[job2] Scheduling project: command-test-job
[job1] Starting building: command-test-job #7
[job2] Starting building: command-test-job #8
[Pipeline] [job2] }
[Pipeline] [job1] }
[Pipeline] // parallel
[Pipeline] echo
job1 -> SUCCESS
[Pipeline] echo
job1 -> echo 'HI' && exit 0
[Pipeline] echo
job2 -> SUCCESS
[Pipeline] echo
job2 -> echo 'HO' && exit 0
[Pipeline] End of Pipeline
Finished: SUCCESS
This is very similar to Steve-B's Answer, but you don't actually need to define the runwrapper explicitly or place it in the additional map before hand.
tl;dr You can just store the parallel build to a hashMap and access that map by directly looping over it's keySet instead
Take this answer with a grain of salt, I am using an older version of pipeline (Jenkins 2.7.2 and Pipeline 2.2).
You can store the parallel build results to a hashMap and loop over the map's keySet to get some information about the build.
def create_build_job(job_name, pool_label="master", propagate=false) {
build job: job_name, parameters: [[$class: 'LabelParameterValue', name: "node_label", label: "${pool_label}"]], propagate: propagate, wait:true
}
def buildmap = [:]
def build_results
stage 'Perform Build'
//test1 is set to fail, test2 is set to succeed
buildmap['test1'] = {create_build_job('test1', "your_node_label")}
buildmap['test2'] = {create_build_job('test2', "your_node_label")}
build_results = parallel buildmap
for(k in build_results.keySet()){
println build_results["${k}"].getProperties()
}
For this pipeline I'm just dumping all of the properties of the RunWrapper stored in item in the map, however you can access each property directly, so if you want the result of the build you can just do:
build_results["${k}"].result
The console output produced by this pipeline (with any potentially identifying information redacted is:
Started by user <user>
[Pipeline] stage (Perform Build)
Entering stage Perform Build
Proceeding
[Pipeline] parallel
[Pipeline] [test1] { (Branch: test1)
[Pipeline] [test2] { (Branch: test2)
[Pipeline] [test1] build (Building test1)
[test1] Scheduling project: test1
[test1] Starting building: test1 #11
[Pipeline] [test2] build (Building test2)
[test2] Scheduling project: test2
[test2] Starting building: test2 #11
[Pipeline] }
[Pipeline] }
[Pipeline] // parallel
[Pipeline] echo
{rawBuild=test1 #11, class=class org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper, absoluteUrl=<jenkins_url>/job/test1/11/, buildVariables={}, previousBuild=org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper#1480013a, id=11, nextBuild=null, changeSets=[], result=FAILURE, description=null, startTimeInMillis=1509667550519, timeInMillis=1509667550510, duration=956, number=11, displayName=#11}
[Pipeline] echo
{rawBuild=test2 #11, class=class org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper, absoluteUrl=<jenkins_url>/job/test2/11/, buildVariables={}, previousBuild=org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper#2d9c7128, id=11, nextBuild=null, changeSets=[], result=SUCCESS, description=null, startTimeInMillis=1509667550546, timeInMillis=1509667550539, duration=992, number=11, displayName=#11}
[Pipeline] End of Pipeline
Finished: SUCCESS
You can access that data by using the Jenkins API after the parallel step:
Jenkins.instance.getItemByFullName('Job1').lastBuild

Resources