This Video Tutorial Discusses about Jenkins Jobs, Types of Jenkins Jobs, Configuring SCM, Build Triggers, Schedule Build Jobs, Polling SCM, Creating Maven Project:
In our previous tutorial, we have learned about downloading generic Java package, Docker toolbox, pulling in Jenkins image on the toolbox and various methods of running Jenkins, like, running as a standalone application, running on an application server, running as a windows service, and finally running on a Docker.
In this tutorial, we will be discussing everything about a Jenkins job, different types of Jenkins job, configuring Source Code Management, Build Triggers, Schedule Build Jobs, Polling the SCM, Maven Build Steps.
What You Will Learn:
Introduction To Jenkins Jobs
Here are video tutorials:
What Is A Jenkins Job?
In simple words, any automated process that is implemented in Jenkins is a Jenkins Job.
The automated process can be about building the source code. The source code can be merged from any of the source code management like git, SVN, and perforce.
Types Of Jenkins Jobs
Jenkins supports the following different types of job:
#1) Run Jenkins as a standalone application: Please refer to the tutorial “Installing and Running Jenkins” to get the detailed steps.
#2) Log in to the Jenkins.
#3) Click on “New Item” on the Jenkins dashboard as shown in Figure 1 below.
#4) Upon clicking on a New Item, it will give the list of different Jenkins jobs. This is as shown in Figure 2.
As shown in the above figure, different types of Jenkins Jobs are:
(i) Freestyle Project: This is a regular and popular job in Jenkins which allows us to build our project, integrate our builds or source code management with Jenkins, poll the SCM, create triggers, and many more.
(ii) Maven Project: Enables us to build our maven projects. We need to only specify the location of our pom.xml file to build the project. All other features like creating triggers, poll SCM remains the same.
(iii) Pipeline: It is a kind of job which lets us declare the build process like compile, run, and report generations if required.
All the above steps can be mentioned in the file called Jenkinsfile in our code base and specify the same path in Jenkins as well. This will run the Jenkinsfile and show the stages of deployment like build, run, etc. This can be followed if we need to run on only one branch.
If we want to run our pipeline on multiple branches or versions of the codebase we make use of the multibranch pipeline.
(iv) Multi-configuration: This kind of project is for a large codebase that needs to be run on different configurations of operating systems.
(v) Folder: In this kind of project, a folder which is a container for all other kinds of projects is created first. We can also make these folders secure.
Configuring Source Code Management
Following are pre-requisites for configuring source code management:
- Git: It’s a version control tool used here. Any other source code management can be used as well like SVN, Perforce, and many more. When the Docker toolbox is loaded, even Git is loaded. The installation steps of the Docker toolbox are mentioned in my tutorial “Installing and Running Jenkins”.
- Code: Code can be written easily using software like Eclipse, Microsoft Visual Studio, and many more.
- Jenkins up and running and launched using http://localhost:8080.
Following are the steps to configure Source Code Mgement:
#1) First, log in to the GitHub repository and select the repository created. I have already created a repository that contains a Java file having a simple selenium script and the print statement. Please refer to Figure 3.
#2) Click on the repository and make sure that the latest code is present. Note the URL of the repository as shown in Figure 4.
#3) Now in Jenkins dashboard click on “New Item”. Refer Figure 1 for the Jenkins dashboard.
#4) Type a project name and select Freestyle project from a list of jobs enlisted as shown in Figure 5.
#5) Select Git under source code management and provide the Github repository URL (already noted in step #2) and provide the credentials as shown in Figure 6 below.
#6) Provide the required build commands to run the code. Here we have used execute windows batch command as shown in Figure 7 below.
#7) Click on Apply and Save button.
#8) Click on Build Now as shown in Figure 8.
#9) Find the result in the console output as shown in Figure 9.
Note: To get the console output, click on the build result (indicated in blue or red). This will take you to the screen shown in Figure 9.
What is a trigger?
A trigger lets us execute a job on an event occurrence. This event is called a trigger. To see the list of build triggers, we need to login to Jenkins and click on any item already created and click on configure.
The list of triggers is shown in Figure 10 below:
- Trigger build remotely: The job is usually triggered by accessing a specified URL. This is convenient for scripts. With the URL, one needs to mention the authorization token as well.
- Build after other projects are built: As it reads, we need to mention the list of other projects, once those projects are built then the present job is executed.
- Build periodically: The build is triggered based on the mentioned time. A cron has to be mentioned here.
- Github hook trigger for GITSCM polling: If Jenkins receives push GitHub hook from a repository associated with git, then the build process gets executed.
- Poll SCM: Configure Jenkins to poll the SCM for ant pushes or commits and then trigger the jobs.
Jenkins Job Scheduler
We need to do the following to schedule a job to build :
- Log in to the dashboard of Jenkins.
- Click on an item or job.
- Click on Configure.
- Check the build periodically option and set the desired cron as shown in Figure 11.
Note: Only for demo purpose, we are giving cron as ***** which means trigger the build every minute. Please disable it or give a cron of your choice.
Polling the SCM
Follow the below steps:
- Click on the “Configure” of the job created in the Jenkins dashboard.
- Click on build triggers in the configure settings and select the Poll SCM.
- Enter the desired cron to poll the SCM. Here we have given * * * * which means the Jenkins polls the SCM every minute. Please refer to Figure 12.
Maven Build Steps
#1) Run the Jenkins and login to it.
#2) Click on Manage Jenkins
#3) Click on Global Tool Configuration.
#4) Under Maven, click on add maven.
#5) Uncheck install automatically.
#6) Provide the path for the Maven bin as shown in Figure 13.
#7) Click on apply and save.
#8) Go to Jenkins dashboard, click on New Item.
#9) Enter Item name and Select Maven project and click on Apply as shown in Figure 14.
#10) In the configure page of the Maven, enter a description, and under Maven click on advanced.
#11) Choose a custom workspace as shown in Figure 15. Custom workspace is the path where pom.xml is present.
#12) Under build, set the goals and options to clean compile test.
#13) Click on Save.
#14) Click on Build Now which is present in New Item.
#15) Wait till the build is a success as shown in Figure 16.
In this tutorial we learned about different types of Jenkins jobs, what are build triggers, configuring the SCM, polling the SCM, scheduling a job, and finally creating a Maven project.