Calling Multiple Jobs in Jenkins – Chapter 5
In the last chapter, we learnt about creating declarative scripts in Jenkins. In this chapter, we will learn the process of calling multiple jobs in one pipeline.
Let us take an example that we have a ‘myfirstpipeline‘ job that needs to call three different sub-jobs (‘test1’, ‘test2’, ‘test3’). As explained in the last chapter, we will create 3 new jobs. The below snapshot shows the pipelines-
Once the pipelines are created, configure the script in ‘myfirstpipeline’ job as shown below-
As you read the script, you will find that when the ‘myfirstpipeline’ will be triggered for execution – it will first build ‘test1’ job, then ‘test2’ job and then build ‘test3’. The keyword ‘build’ is used to call an existing job.
Let us run this pipeline and see the results…
Click on the build number, in our case it is #8 – to view the Output..
The sequence of these three sub-jobs can be changed as per the need – change the order in the ‘myfirstpipeline’ script and re-run to observe the results. This was just an example – now lets take a real scenario – you want to execute the following jobs for restarting a web application due to some DB changes-
#1 –> Stop the web server
#2 –> Remove the web application
#3 –> Update the DB
#4 –> Copy latest version of web application
#5 –> Restart web application
All the above jobs will be separate jobs and will be called in one master pipeline / job. This way these sub-jobs act as independent modules that can be reused in other jobs as well.
Sound cool ? Try out the above jobs and share in your experiences / comments. Stay tuned for more such tutorials.