mercredi 19 avril 2017

Jenkins Pipeline Job with Condition

I am creating a Jenkins pipeline. This pipeline is building three jobs (JobOne, JobTwo, JobThree). I am able to run the job with following code.

node {
   stage 'Stage 1'
   echo 'Hello World 1'
   build 'Pipeline-Test/JobOne'

   stage 'Stage 2'
   echo 'Hello World 2'
   build 'Pipeline-Test/JobTwo'

   stage 'Stage 3'
   echo 'Hello World 3'
   build 'Pipeline-Test/JobThree'
}

Now I want to put some condition in it. Example, when JobOne fails, the job has to restart once again. When JobTwo passes, want to run job again. And JobThree should run after a span of 10 mins after JobTwo completes. I am not sure how to make pipeline with this condition. I am new to Jenkins pipeline.

I checked few Jenkins WiKi page but could not find a proper way to implement if condition with above conditions. I tried below code just to check how 'if' condition can be implemented. But it fails.

node {
   stage 'Stage 1'
   echo 'Hello World 1'
   build 'Pipeline-Test/JobOne'
   post {
       always{
           build 'Pipeline-Test/JobOne'
       }
   }

Error:

java.lang.NoSuchMethodError: No such DSL method 'post' found among [archive, bat, build, catchError, checkout, checkpoint, deleteDir, dir, dockerFingerprintFrom, dockerFingerprintRun, echo, error, fileExists, git, input, isUnix, load, mail, node, parallel, properties, publishHTML, pwd, readFile, retry, sh, sleep, sshagent, stage, stash, step, teamconcert, timeout, tool, triggerRemoteJob, unarchive, unstash, waitUntil, withCredentials, withDockerContainer, withDockerRegistry, withDockerServer, withEnv, wrap, writeFile, ws]
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:107)

Can someone please guide me through this?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire