Jenkins is a self-contained automation server that allows continuous integration and continuous delivery of projects, regardless of the platform you are working on. It is a free source that can handle any kind of build or continuous integration. You can integrate Jenkins with a number of testing and deployment technologies.
Jenkins is a software that allows continuous integration. Jenkins will be installed on a server where the central build will take place. The following flowchart demonstrates a very simple workflow of how Jenkins works.
-
Go to: https://jenkins.io/
-
Click on Download.
-
Select Jenkins for your suitable Operating System.
-
Jenkins will start downloading.
-
Extract the ZIP file and start the installer.
-
Follow the instructions to complete Jenkins installation.
-
After the installation is complete, the installer launches the browser which opens localhost:8080. localhost:8080 is the default port in which the Jenkins runs on.
-
Copy the initial Administrator password from the given location and paste it to the input box.
-
Click on continue.
-
Jenkins suggests us to install plugins.
-
Click start using Jenkins.
These two terms are used interchangeably. They refer to runnable tasks that are controlled/monitored by Jenkins.
####2. Slave/Node:
Slaves are computers that are set up to build projects for a master. Jenkins runs a separate program called “Slave agent” on slaves. When slaves are registered to a master, a master starts distributing loads to slaves. Node is used to refer to all machines that are part of Jenkins grid, slaves and master.
Executor is a separate stream of builds to be run on a node in parallel. A node can have one or more executors.
A build is a result of one of the projects.
A plugin is a piece of software that extends the core functionality of the core Jenkins server.
Jenkins uses a master and slave architecture to manage distributed builds.
-
Schedule build jobs.
-
Dispatch builds to the slaves for the actual job execution.
-
Monitor the slaves and record the build results.
-
Execute build jobs directly.
- Execute build jobs dispatched by the master.
It is possible to configure a job to always run on a particular slave machine or a particular type of slave machine
-
Click on ‘Manage Jenkins’ option on the left-hand side menu.
-
In the next screen, click on ‘Manage plugin’ option.
-
In the next screen, click on the ‘Available’ tab. (This tab will give a list of plugins which are available for downloading.
-
In the ‘Filter’ tab type ‘Git Plugin’.
-
Check the ‘Git Plugin’ option and click on ‘Install without restart’.
-
The plugin will then be installed.
-
From the Jenkins dashboard, click on ‘Manage Jenkins’ from the left-hand side menu.
-
Click on ‘Configure System’ from the right-hand side menu.
-
In the ‘Configure System’ screen, scroll down to the ‘Maven’ section and click on ‘add Maven’.
-
Uncheck ‘Install automatically’ option.
-
Add any name for the settings.
-
Add location of the MAVEN_HOME.
-
Click on save.
From the Jenkins dashboard, click on ‘Manage Jenkins’. Now we can get various configuration options to manage Jenkins.
The management options are:
This is where one can manage paths to the various tools to use in builds, such as the JDKs, the versions of Ant and Maven, as well as security options, email servers, and other system-wide configuration details. When plugins are installed. Jenkins will add the required configuration fields dynamically after the plugins are installed.
This where we can configure the security of Jenkins. We can define who is allowed to access or use the system.
This is where we can configure the credential providers and types.
This is where we can configure tools, their locations and automatic installers.
Jenkins stores all its system and build job configuration details as XML files which is stored in the Jenkins home directory. Here also all of the build history is stored. Migrating build jobs from one Jenkins instance to another, or archiving old build jobs, we will need to add or remove the corresponding build job directories to Jenkins’s builds directory. We don’t need to take Jenkins offline to do this—we can simply use the “Reload Configuration from Disk” option to reload the Jenkins system and build job configurations directly.
Plugins can be installed, updated and removed through the Manage Plugins screen.
This screen displays a list of all the current Java system properties and system environment variables. Here one can check exactly what version of Java Jenkins is running in, what user it is running under, and so forth. We can also see the plugins and thread dumps.
The System Log screen is a convenient way to view the Jenkins log files in real time. The main use of this screen is for troubleshooting.
This page displays graphical data on how busy the Jenkins instance is in terms of the number of concurrent builds and the length of the build queue which gives an idea of how long the builds need to wait before being executed. These statistics can give a good idea of whether extra capacity or extra build nodes is required from an infrastructure perspective.
This screen lets you run Groovy scripts on the server. It is useful for advanced troubleshooting since it requires a strong knowledge of the internal Jenkins architecture.
Jenkins is capable of handling parallel and distributed builds. In this screen, we can configure how many builds we want. Jenkins runs simultaneously, and, if we are using distributed builds, set up build nodes. A build node is another machine that Jenkins can use to execute its builds.
Scrub configuration files to remove remnants from old plugins and earlier versions.
We can create, delete and modify users that can log into Jenkins.
If there is a need to shut down Jenkins, or the server Jenkins is running on, it is best not to do so when a build is being executed. To shut down Jenkins cleanly, you can use the Prepare for Shutdown link, which prevents any new builds from being started. Eventually, when all of the current builds have finished, one will be able to shut down Jenkins cleanly.
-
Go to the Jenkins Dashboard.
-
Click on ‘New Item’.
-
Enter an Item Name (Job name).
-
Select the type of project.
-
Click on OK.
(Next steps are for freestyle project.)
-
Enter the Project Description.
-
Select how and where the build job will be executed below the ‘Description’ tab (not necessary).
-
Select the Source Code Management (e.g. Git, Subversion).
-
Specify the Build Triggers (not necessary).
-
In the ‘Build’ section define the actual Build steps.
-
Click on ‘Add build step’ and add the steps.
-
We can add post-build action in the ‘Add post-build action’ tab (not necessary).
-
Click on save.
-
Jenkins job will be created and we can see the job in the dashboard.
-
Go the dashboard.
-
Click on the build now button on the last column of the project entry.
-
Click on the job name to enter the main page of the project.
-
We can see the build being scheduled.
-
We can see the state of build under the Build History on the left-hand side menu.
In Jenkins the blue ball indicates success and a red ball indicates failure.
-
If we click on the ball, it will take us to the main page of the build.
-
Click on ‘Console Output’ on the left-hand side to view the console output of the job.
If we go to the dashboard now we can see the status of the job represented in terms of blue or red ball (Blue ball meaning success and red ball meaning failure). This is the shown in the first column.
The second column is the weather. This shows an aggregated result of all the recent builds. Sunshine means that all the recent builds are successful.
We can also see the Last success and last failure and the duration of the latest build.
-
Click on New Item.
-
Enter an Item name.
-
Click on Freestyle project.
-
Click on OK.
-
Give the description.
-
Select Source Code Management as Git.
-
Supply the Repository URL for Git.
-
Select the branches to build.
-
Add Build Step: Invoke top-level Maven targets.
-
Select Maven version as local Maven.
-
Select the Goals.
-
Click on Save.
-
Click on the Maven Project created from the dashboard.
-
Select Build Now from the Left-hand side.
-
The build will then be triggered.
-
Click on the Build number of the Maven project from the left-hand side.
-
Now clicking on the Console Output from the left-hand side, Jenkins will view the console output of the Build.
We can go to the Workspace of the project to view our project.
Using the Source Control Polling we can use Jenkins to trigger a build periodically.
-
Select the project created.
-
Select Configure from the left-hand side panel.
-
Scroll down to the Build Triggers section and select Poll SCM option.
-
Then in Schedule field we can enter how often we want to trigger a build. The schedule field follows the cron format.
Cron format consists of five fields separated by white spaces:
<Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week>
The following graph shows what it consists of:
* * * * *
| | | | |
| | | | |
| | | | +---- Day of the Week (range: 1-7, 1 standing for Monday)
| | | +------ Month of the Year (range: 1-12)
| | +-------- Day of the Month (range: 1-31)
| +---------- Hour (range: 0-23)
+------------ Minute (range: 0-59)
Any of these 6 fields may be an asterisk (*). This would mean the entire range of possible values, i.e. each minute, each hour, etc.
Any field may contain a list of values separated by commas, (e.g. 1,3,7) or a range of values (two integers separated by a hyphen, e.g. 1-5).
After an asterisk () or a range of values, you can use character / to specify that values are repeated over and over with a certain interval between them. For example, you can write "0-23/2" in Hour field to specify that some action should be performed every two hours (it will have the same effect as "0,2,4,6,8,10,12,14,16,18,20,22"); value "/4" in Minute field means that the action should be performed every 4 minutes, "1-30/3"means the same as "1,4,7,10,13,16,19,22,25,28".
In Month and Day of Week fields, you can use names of months or days of weeks abbreviated to first three letters ("Jan, Feb,…,Dec" or "Mon,Tue,...,Sun") instead of their numeric values.
-
We want to build trigger as frequently as possible. So, we use * * * * * in the schedule field. This will trigger a build every minute.
-
Click on Save.
-
Go to the project and a new item is added to the left-hand side panel which is “Git Polling Log”.
-
Click on the Git Polling Log from the left-hand side panel.
-
Now Jenkins will trigger a Build every minute and view the changes in the Git Polling Log.
- Trigger builds remotely (e.g., from scripts):
Jenkins provides machine consumable remote access APIs to its functionalities.
i) Supply the token name in Authentication Token field.
ii) Copy the URL given below. (The URL is used to trigger a build.)
iii) Save the configuration.
iv) Open a new tab in the web browser.
v) Paste the URL in the address bar.
vi) Replace JENKINS_URL with localhost:8080 and TOKEN_NAME with the token name given.
vii) Press Enter to fire the request.
viii) Jenkins page will then view that a build has been triggered.
This remote execution function is very handy. We can use it to trigger a Jenkins build either from a script or from other application.
- Build after other projects are build:
This option will let us set up a build that will be run whenever another build is finished. We just need to add the preceding build job in the Projects to watch field. We can add multiple projects to watch separated by commas.
- Build periodically:
We can use the Build periodically option to create scheduled builds. In the schedule field we can enter how often we want to trigger a build or when to trigger a build. The schedule field follows the cron format.
- Build when a change is pushed to GitHub:
This feature enables builds to be triggered when a change is pushed to the GitHub repository. Basically, instead of Jenkins pulls SCM periodically what happens here is whenever a commit is made to GitHub a web hook will be triggered which will notify Jenkins to trigger a build right away. In order to enable this build trigger, we need to set up the hook between Jenkins and GitHub. We have to use the GitHub plugin to setup the hook between GitHub and Jenkins.
Deploying Artifact to staging Environment:
We need to archive the artifact which is generated from a project and then in the next job we will pick up the artifact and deploy it to the staging environment.
-
Go to the configuration page of the project.
-
Scroll down to the post build action section.
-
Click “Add post-build action”.
-
Select “Archive the artifacts”.
-
Enter files to archive in the “Files to archive” tab.
-
Click on save.
-
Now triggering a build will archive the artifact.
-
Install “copy artifact” plugin and “deploy to container” plugin.
-
Go to dashboard and select New Item.
-
Enter the Job name.
-
Select Freestyle project.
-
Click on OK.
-
Scroll down to the Build section.
-
Click on Add build step.
-
Choose copy artifact from another project.
-
Enter the project name to copy artifact from.
-
Enter the artifacts to copy.
-
In the Post-Build Actions select Deploy war/ear to a container.
-
Enter WAR/EAR files.
-
Select the container.
-
Fill in the Container credentials and URL.
-
Click on Save.
Now the job we created (downstream job) will take the artifacts created from upstream job and deploy it to the staging environment. The upstream job is the project which archives the artifacts. This downstream job should be automatically triggered if the upstream job completes successfully. We can achieve this by adding a post-build action to the upstream job.
-
Go to the configuration page of the upstream job.
-
Scroll down to the post-build action section.
-
Select Add post-build action and choose Build other projects.
-
Enter the name of the job which is the downstream job.
-
Click on Save.
Now if we trigger the build of the upstream job then the artifact will be deployed to the staging environment.
The Jenkins Build Pipeline gives the ability to form a chain of jobs based on their upstream\downstream dependencies. Downstream jobs may, as per the default behaviors, be triggered automatically, or by a suitable authorized user manually triggering it. We can view the Jenkins Build pipeline by installing “Build Pipeline” plugin.
-
Go to Jenkins dashboard.
-
Click on the “+” sign beside the “ALL” tab to create a new view.
-
Enter a view name and select “Build Pipeline View”.
-
Click on OK.
-
Select the initial job which marks the starting point of the build pipeline.
-
Click on OK.
-
Now we can see the Build Pipeline View.
-
We can start the jobs in the pipeline by clicking on the run button.