Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 838
Raji Avatar asked Feb 05 '23 15:02

Raji


1 Answers

There is definitely a learning curve with Jenkins Pipelines, so don't get discouraged :)

I recommend checking out Jenkins' official documentation as well as the Pipeline Steps Reference page to anyone getting started with Jenkins Pipelines.

FYI, stages without a block argument is deprecated; you should define stages as follows:

stage('Name of Stage') {
   // code
}

Pipelines have a retry step that you can use to retry the JobOne build if it fails.

To wait 10 minutes between stage 2 and 3, you can use the sleep step.

if statements are written just like in Java, since Groovy is actually compiled on a JVM.

if (animal == 'dog' || boolean == true) {

Combining each of these, I think this is what you can use:

node {
     stage ('Stage 1') {
          echo 'Hello World 1'
          retry(1) {
               build 'Pipeline-Test/JobOne'
          }
     }
     stage ('Stage 2') {
          echo 'Hello World 2'
          build 'Pipeline-Test/JobTwo'
     }

     sleep time:10, unit:"MINUTES"

     stage ('Stage 3') {
          echo 'Hello World 3'
          build 'Pipeline-Test/JobThree'
     }
}
like image 68
Christopher Rung Avatar answered Feb 07 '23 10:02

Christopher Rung