yk28
yk28

Reputation: 25

Jenkins - not able to run pipelines from Jenkinsfile

I have a Jenkinsfile where the build and tests run on the same Slave. My requirement is that the build must be on one Slave (say A) and the tests must run on another slave (say B).

I just setup the slave B and I can see both my slaves A and B in Jenkins->Manage nodes. The problem is that the build stage works successfully , but my prepare_test and test stages are not executed on slave B.

Below are problems seen:

1.) I get the following error after the build stage is successful:

"java.lang.NoSuchMethodError: No such DSL method 'agent' found among steps [archive, bat, build, catchError, checkout, deleteDir, dir, dockerFingerprintFrom, dockerFingerprintRun, dockerNode, echo, emailext, emailextrecipients, envVarsForTool, error, fileExists, findBuildScans, getContext, git, input, isUnix, junit, library, libraryResource, load, lock, mail, milestone, node, parallel, powershell, properties, publishHTML, pwd, readFile, readTrusted, resolveScm, retry, script, sh, sleep, stage, stash, step, svn, timeout, timestamps, tm, tool, unarchive, unstable, unstash, validateDeclarativePipeline, waitUntil, warnError, withContext, withCredentials, withDockerContainer, withDockerRegistry, withDockerServer, withEnv, wrap, writeFile, ws] or symbols [all, allOf, always, ant, antFromApache, antOutcome, antTarget, any, anyOf, apiToken, architecture, archiveArtifacts, artifactManager, attach, authorizationMatrix, batchFile, booleanParam, branch, brokenBuildSuspects, brokenTestsSuspects, buildButton, buildDiscarder, buildTimestamp, buildTimestamp java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java at java.lang.Thread.run(Thread.java:748) Finished: FAILURE "

2.) Do not see the stages 'prepare_test' and 'test' for my branch, while I can see the stage related to build.

Attached my jenkinsfile code:

pipeline {
    agent none
    options {
        skipDefaultCheckout true
    }
    stages {
        stage('build') {
            agent {
                docker {
                    image 'XYZ'
                    args '-v /etc/localtime:/etc/localtime:ro'
                }
            }
            options { skipDefaultCheckout(true) }
            steps {
                echo '########################################## Building #########################################'
                // trigger the build
                sh 'scm-src/build.sh all-projects'
            }
        }   

   }
} 


pipeline {
    agent {
        label 'laptop-hp'
    }
    stages {
        stage('prepare_test') {
              agent {
                 docker {
                    image 'ABC'
                    args '-v /home/jenkins/.ssh/:/home/jenkins/.ssh:ro -v /etc/localtime:/etc/localtime:ro'
                }
               }
            options { skipDefaultCheckout(true) }
            steps {
                echo '####################################### Prepare Test Environment ############################'
                sh 'scm-src/test.sh prepare'
            }
         }


        stage('test') {
            agent {
              docker {
                     image 'ABC'
                     args '-v /home/jenkins/.ssh/:/home/jenkins/.ssh:ro -v /etc/localtime:/etc/localtime:ro'
                 }
               }

            options { skipDefaultCheckout(true) }
            steps {
               echo '########################################## Testing ##########################################'
                sh 'scm-src/test.sh run'
            }
        }
    }  
}


The name of my slave B is 'laptop-hp' as seen in Jenkinsfile

Is there a problem with Jenkinsfile or do I miss some settings on my slave B ?

Regards kdy

Upvotes: 0

Views: 2729

Answers (1)

Dibakar Aditya
Dibakar Aditya

Reputation: 4203

You cannot have more than one pipeline {} block in a Declarative Pipeline. The correct syntax is to append your test stages right after the build stage. Then define the parameter label for each stage within docker {} to run on the corresponding slave. Also, it is sufficient to add the option skipDefaultCheckout true once at the top-level of the pipeline.

Your pipeline should now look like:

pipeline {
    agent none
    options {
        skipDefaultCheckout true
    }
    stages {
        stage('build') {
            agent {
                docker {
                    image 'XYZ'
                    label 'slave-A'
                    args '-v /etc/localtime:/etc/localtime:ro'
                }
            }
            steps {
                echo '########################################## Building #########################################'
                // trigger the build
                sh 'scm-src/build.sh all-projects'
            }
        }   
        stage('prepare_test') {
            agent {
                docker {
                    image 'ABC'
                    label 'laptop-hp'
                    args '-v /home/jenkins/.ssh/:/home/jenkins/.ssh:ro -v /etc/localtime:/etc/localtime:ro'
                }
            }
            steps {
                echo '####################################### Prepare Test Environment ############################'
                sh 'scm-src/test.sh prepare'
            }
        }
        stage('test') {
            agent {
                docker {
                    image 'ABC'
                    label 'laptop-hp'
                    args '-v /home/jenkins/.ssh/:/home/jenkins/.ssh:ro -v /etc/localtime:/etc/localtime:ro'
                }
            }
            steps {
                echo '########################################## Testing ##########################################'
                sh 'scm-src/test.sh run'
            }
        }
    }
} 

Upvotes: 0

Related Questions