Search This Blog

Breaking

Friday, 15 January 2021

January 15, 2021

Jenkins: How to create a job which Runs other jobs form Jenkins pipeline

Rscenatally, I encountered a problem where we need to call multiple jobs from a Job. There are plugins available for this activity in Jenkin but I could not leverage them because of some security reasons. So to, Implement that, I googled the same and found solutions in a few StackOverflow questions. Long Live StackOverflow ..:). Though those answers didn't work by just copy-paste, so I am writing here the challenges I faced and fixed them for creating the script.





First, below is the script:

   pipeline {
        agent none
        stages {
            stage('Build My first job'){
                steps{
                build job: 'Job1', propagate: false 
                }
            }
                stage('Build Second Job'){
                    steps{
                    build job: 'Job 2', propagate: false 
                    }
                }
            }
        }


So, to create such a script, we can get rid of the agent. Because, for this particular job, it has no use.
Second, I found an error when I didn't put "steps" inside the stage. It looks like, this is a mandatory field now.

Later, My job started failing, because it did not execute the second job if the first job build goes to fail.

So to fix that part, I again googled and found this parameter "propagate".

If it's false, then if the build fails, it doe not allow the next build to execute.

But in my case, I need to execute the other job. So I put it as True and irrespective of the result, it executed the other job as well.

It solved my purpose. For now, I have not seen any new issue because of this, if anything will come, I will update here as well.

Till then, Happy Learning :)


#Jenkins  #Groovy