0% found this document useful (0 votes)
3 views9 pages

CI with jenkins

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

What is CI?

According to Martin Fowler,

Continuous Integration is a software development practice where members of a team integrate their
work frequently, usually each person integrates at least daily - leading to multiple integrations per day.
Each integration is verified by an automated build (including test) to detect integration errors as quickly
as possible.

The term Continuous Integration was first coined by Grady Booch.

You need a tool to create a CI enabled environment. Jenkins, Travis CI, Bamboo,Buildbot are different
tools available to enable CI.

In this course, you will be learning about Jenkins, a widely used open source CI tool, written in Java.
Jenkins was earlier referred to as Hudson. Jenkins was primarily developed by Kohsuke Kawaguchi.

What does Jenkins do?


Compiles and builds the code

Runs an internal shell or command line script

Starts execution of the integration tests

Monitor execution of tasks

Stops build in case of failure

Notify user on the build status

Deploy in test or production environments

Features of Jenkins

Easy to install

Easy to configure various tasks

Rich plugin ecosystem - Integrates with a variety of build, test,deploy,reporting tools

Permanent links - Jenkins provides direct links to the latest or failed build, which can be used for easy
communication

Extensibility - Customize Jenkins to suit your needs


Distributed builds - Jenkins can distribute build, test jobs to multiple computers with different operating
systems

File fingerprinting - Manages dependencies

Email integration- Emails the build status

Pre Jenkins:

Source code was completely built and then tested

Bugs identified during testing in the source code, should be fixed and then re-tested

Slows the software delivery, as the entire process is manual

Post Jenkins :

Once code change is committed,Jenkins automatically takes care of the build , test and reporting of
results.

Extending Jenkins Functionality

Once Jenkins is installed, it is time to configure it, to fit your needs.


Jenkins has relatively few abilities, but it aids the s/w developers by providing a variety of plugins.
Plugins are add-ons that allow Jenkins to interact with many other softwares The exact plugins you install
depends on the nature of your project.

Jenkins Plugins Index provides you the various plugin options such as:

Source Control: Git, SVN, Mercurial

Testing : Selenium, Windmill

Triggers: Jabber, Directory watchers

Artifact: To copy components between projects like Amazon S3, SCP

Code Analysis: To parse the code with tools like CheckStyle,Findbugs,PMD

Build Tools: In large projects use a build manager such as Maven or Ant.

Reporting: Jenkins provides its own reports. It can be extended using tools like Static Analysis Collector
that collects the different analysis results and shows it in a combined trend graph.

Plugins can be configured via the Manage Plugins under Manage Jenkins.

For setting up a new project in Jenkins , following sections are to be planned and configured as required:

SCM - Associate with a version control server

Triggering Build - Control when Jenkins will perform builds by Polling, Periodic or Build based on other
projects

Execution of scripts, Ant and Maven targets

Archiving the artifacts

Recording and publishing build and test results

Email notifications

How to define CRON expression?

Jenkins schedules are configured using the CRON syntax.

It consists of five fields separated by white space, indicating respectively the minute (0–59), hour (0–23),
day of the month (1–31), month (1–12) and the day of the week (0–7, with 0 and 7 being Sunday).
star is a wildcard character which accepts any valid value for that field.

“* * * * *” means every minute of every hour of every day.

“* 9-17 * * *” means every minute of every day, between 9am and 5pm.

There are other convenient short-hands, such as “@daily” and “@hourly”.

Build Status is indicated in two ways :

A weather icon (on the home page dashboard) - shows you a record of multiple builds

A colored ball (on the individual project page) - shows you status of a single build

Status has corresponding tooltips with explanations , when you hover over it.
Setting up Jenkins CI Pipeline

In the Katacoda exercise, you would have learnt how to use jenkins and docker in the CI pipeline.

Below are steps to setup Jenkins CI pipeline from Git to Tomcat.

Pre-Requisites

Jenkins, Maven, Tomcat should be up and running in your machine

Source code should be made available in Git

Plugins to install

Deploy to Container , Git

Jenkins Jobs to be created-

Create a Pull job that detects code changes in Git

Create a Build job that is triggered after the Pull job

Create a Deploy job to push results to tomcat.

Setting up Deploy Job

You should be able to create the first two jobs easily. Below steps will help you to create the deploy job.

In the Post-build Actions section:

Select deploy war/ear to a container

Mention the *.war file

Mention Context path- where your application will be published in Tomcat

Select Tomcat version in the Container dropdown

Enter credentials of user who can access Tomcat

Mention Tomcat URL to reach your tomcat instance


On changing your code in Git, you will see these jobs will trigger automatically on Jenkins.

Jenkins provides a host of plugins for unit testing such as Junit and Mstest for .Net unit tests..

If you choose to use the Junit test

Select project and choose the configure option

In Add Build step , choose action to invoke Ant.

Select Advanced and enter the location of build.xml file

In Add post-build, select Publish Junit test results.

In Test Report XMLs field, enter location of result xml files produced by executing Junit test cases

Save and then build.

Jenkins is open to every one. So, anyone can access Jenkins and perform all available tasks that were
discussed previously.

Hence it is recommended to secure Jenkins.

It provides settings for security and role management - useful for controlling access and defining user
actions.

Select Configure Global Security under Manage Jenkins.

In the next window - select Enable Security.

Choose jenkins own user database to maintain your own user database.

For a central administrator to define other users in the system, unselect Allow users to signup and save.

Then, set up the admin user.

Configuring User Access

As an admin user, you can now set up other users .

Select Manage Users under Manage Jenkins to add other users

Now you need to setup authorizations

Select Matrix based Security under Configure Global Security


If the user is not in the user group list, enter the user and add to the list

Define the relevant authorizations and save

Jenkins security setup is now complete.

How to distribute work load ?

What if, as part of a large project, you need to build codes and report results regularly ?

Running all the builds on a centralised machine would not be the best option.

What if, you need to automatically test the code in various environments ?

Different environments are to be set up in separate machines.

Jenkins distributed architecture is the solution in such scenarios.

Master-Slave architecture

Jenkins uses Master - Slave architecture for managing distributed builds. In master node, you need to
install Jenkins. While it can also execute build jobs, it handles all related tasks for build system -

Schedule build job

Dispatch build job to slaves

Monitor the slave

Report the build results

Master-Slave Communication

Slave node offloads work from the master.

It can run on various operating systems.

Executes build jobs dispatched by master.

Jenkins need not be fully installed on slave.

To operate, master and slave will establish bi-directional communication link like TCP/IP

To setup slave node:


Select Manage Node under Mange Jenkinsand Click New Node

Select the dumb slave or permanent agent option and Click Ok

In the next window, enter details like the IP address and the user credentials

Select the launch method and then Save.

On completion of the steps mentioned in the previous card, the new node machine will be online and
ready .

To execute the project in the slave node:

Select 'Restrict where this job can be run' option under your 'project configuration`

Mention the slave name in Label expression field.

Taking project backups are crucial in software delivery process.

In Jenkins, all build logs, archives, configuration settings are stored under the Jenkins home directory.

To backup the home directory :

You can manually copy it to another location OR Install backup plugin

If you choose to use the backup plugin, you need to trigger backup manually

If full backup of home directory is not needed, use ThinBackup plugin to backup specific details, like jobs,
configuration, build history.

Using Jenkins - Best Practices

Few recommendations for effective utilization of Jenkins

Secure Jenkins.

Choose the correct plugins based on your project need.

Schedule jobs ensuring not all jobs are executed at the same time.

Monitor disk space and clear or archive data to maintain space.

Break down jobs to allow reuse of generic jobs.

Backup is required for configurations and activity logs.

You might also like