In this article, we will learn about the Jenkins tool, its Architecture and its advantages. We will also see how to use Jenkins with Maven build tool.
What Is Jenkins?
Jenkins is an open-source automation tool that helps to automate the software development process – building, testing, and deployment to production or different servers. It allows continuous integration and continuous delivery of the software.
Jenkins works on master-slave architecture. The Main Jenkins server works as a master and is connected with one or more slaves. Slaves are the remote machines. Therefore, we are able to run various builds, tests, and production environments across the entire architecture.
Master provides clear instructions for Jenkins slaves to perform a particular activity. The Jenkins server can be on-premises or on the cloud(AWS/Azure/Google Cloud) as per your requirement. Master and slaves communicate using TCP/IP protocol.
Advantages of Jenkins
- Open source / You can use it free of cost.
- Easy to set and easy to use
- Highly reliable with built-in GUI.
- Rich Plugin ecosystem.Can be integrated with over 1700+ plugins.
- Great community support.
- Increase productivity by automating the entire SDLC.
Jenkins can be installed and configured in multiple ways. It can be installed on the local machine, Using Docker Image or using Cloud platforms.
1)Download and Install Java [ java 8 or java 11] and configure the path variable.
2)Download and Install Jenkins from here.
3)Download Maven and configure the path.
4)Download, Install and configure GIT
How to Run a Maven Project Using Jenkins?
Let’s see how to integrate Jenkins with Maven. The Maven project can be executed either by Executing a Windows batch command or Invoking top-level Maven targets in Jenkins. In this article, we will use Invoke top-level Maven target options.
- Install the Maven plugin in Jenkins.
Go to Dashboard -> Manage Jenkins -> System Configuration -> Manage Plugins.
Under the Plugin Manager search for the Maven Integration plugin and click on the Install without restart button.
2. Configure Maven
Go to Dashboard -> Global Tool configuration –>scroll down to the Maven section
Provide the name as ‘Maven’ and select the version that you need to install from the dropdown for example version 3.6.3. Click save.
3. Create a Freestyle Job
Go to the Source code management section and select Git. Provide git repo URL. Scroll and come to the build section, provide root POM as pom.xml and add maven goal as ‘clean test’ and click save.
4. Build the project
Configuration is complete, now click on build now. At first, the repo will be cloned, the desired build will be configured, the maven version will be installed irrespective of whether maven is installed on your system or not and then the maven goal will be executed.
That’s how Jenkins Maven configuration is done.
How to Schedule a Job in Jenkins?
You might prefer to execute a task at particular periods as opposed to triggering it depending on an event. For instance, you might only want to execute an application build overnight if it consumes a lot of system resources so that your development teams can have access to your build systems during the day. We can do that either by using Build periodically or using SCM polls.
Go to Dashboard and select the project for which you want to schedule a job. Click on Configure and open the build trigger section. This section has multiple options like trigger build remotely by using a script or creating dependent build triggers. Select Build periodically as we want to schedule the builds on the basis of date and time.
The format has 5 fields and by using these fields we can write our regular expressions.
MINUTE (0-59), HOUR (0-23), DAY (1-31), MONTH (1-12), DAY OF THE WEEK (0-6)
Example: Some Jenkins CRON examples are provided below to show you how to schedule a cron job to trigger the Jenkins build automatically.
1)To Build the project every hour
0 * * * *
2)To Build the project on even dates.
3)Trigger build on the 15th of every month.
2)Jenkins SCM or Poll SCM
This option is similar to the build periodically option. The only difference is that Poll SCM will trigger the build only when any changes are made to the source code while the build periodically option will trigger the build at the scheduled time irrespective of the changes in the source code.
Go to the freestyle job and navigate to the build triggers section. Select the Poll SCM checkbox and provide the schedule in the textbox. Suppose we want to Check the repository every 30 minutes for code changes and if changes are found then the build will trigger automatically.
Click apply and save. It will wait for 30 minutes and then check the repo for the changes. Once found new build will be triggered. You can check logs as well in the Polling log section.