Home Guide Tutorial on Continuous Integration with Jenkins

Tutorial on Continuous Integration with Jenkins

By Loveneesh Singh, Community Contributor and Pradeep Krishnakumar, Manager -

Continuous Integration is a software development process where a code is continuously tested after a commit, to ensure there are no bugs.

In large teams, many developers work on the same code base. Thus, any of the multiple commits can have a bug. With continuous integration, bugs can be identified early and fixed before pushing changes to production. Any new code is integrated into one executable form, termed as a build. If the build is green (i.e. all ok), then the executable artifact can be deployed. If not, the bug needs to be fixed, and the new build is tested again.

What is Jenkins?

Jenkins is an open-source implementation of a Continuous Integration server written in Java.

  • It works with multiple programming languages and can run on various platforms (Windows, Linux, and macOS). It is widely used as a CI (Continuous Integration) & CD (Continuous Delivery) tool.
  • It has vast community support, and there are many plugins available for integrating it with other tools like Slack, GitHub, Docker. Also, anyone can develop a Jenkins plugin and contribute to it.
  • By using Jenkins, software companies can accelerate their software development process, as Jenkins can automate a build and run tests to ensure the functionality is working fine.
  • Jenkins supports the entire software development life cycle that includes building, testing, documenting the software and deploying.

Get Started Free

Let’s see how to get started with Jenkins CI.

Jenkins Pipeline

How to create a CI pipeline in Jenkins

1. Declarative Pipeline

Jenkins pipeline can be written in a declarative manner with the “Jenkins syntax“. Jenkins syntax is a Groovy DSL (Domain Specific Language) which is used to write stages of the pipeline. The DSL internally gets converted into XML to make the Jenkins pipeline. A “stage” in a pipeline is a group of steps to be executed in the pipeline.

Jenkins -Stage View

Let us consider a stage – “git checkout” that can clone the git repo (step) and tag it with a version (step). A scripted pipeline is easy to create and with the help of the Jenkins syntax, it can also be effortlessly read.

Sample Jenkins scripted pipeline

pipeline {
agent any
stages {
stage('Git Checkout') {
steps {
echo 'Cloning repository'
sh 'git clone https://github.com/sample.git'
}
}
stage('Build') {
steps {
echo 'Building project'
sh 'gradle clean build'
}
}
}
}

2. Jenkins Job Builder

JJB (Jenkins job builder) is a tool to create a pipeline using YAML configuration. The Jenkins pipeline can be written in YAML and with the help of the JJB tool, it converts the YAML configuration in XML format and pushes it into Jenkins to create pipelines. It is comparatively easy to write YAML configuration than writing full-fleshed code in Jenkins syntax.

This tool also enables the use of “Pipeline as Code” so that a pipeline code can be pushed into a git repo and the pipeline gets created or updated in Jenkins. The tool also helps with the templatization of the pipeline code.

Sample Template

- project:
name: project-name
pyver:
- 26:
branch_name: old_branch
- 27:
branch_name: new_branch
jobs:
- '{name}-{pyver}'

Sample Job

- job-template:
name: '{name}-{pyver}'
builders:
- shell: 'git co {branch_name}'

Benefits of using Jenkins CI

  1. Reduced development cycle – Since every commit is getting built and tested, it allows releasing new features to the user faster and with fewer errors
  2. Shorter time to integrate code – Before the use of Jenkins CI, integration of code was done manually, thus taking a few days. In some cases, it might happen that the code is not in a running state, and it is hard to debug as it might have gone through various commits in the repository. Integrating code after every commit ensures that at least the functionality is not broken after a commit
  3. Faster feedback to developer teams – Whenever a test breaks during a commit, developers get feedback and hence improve the code there and then. Otherwise, debugging the issue can be very difficult given teams would not be sure which commit resulted in the bug
  4. Automated workflow – Teams do not have to worry about running a manual test for each commit. The Jenkins CI pipeline checks the latest code and builds the code along with the tests. If the test is green, it can deploy the project in a specific environment else it can notify the developer by breaking the build

Concerns using Jenkins CI

  1. Cost – Running a Jenkins CI server requires some infrastructure setup and its costly to deploy a Jenkins CI over the cloud
  2. Maintenance – Spinning up a Jenkins CI server is time-consuming. Jenkins CI also needs maintenance in case of adding a stage in the pipeline or upgrading the Jenkins CI server to incorporate new features

Key Takeaways

Continuous Integration is a practice of building the code with every commit. Jenkins is one of the popular CI tools that helps to implement a continuous integration pipeline. Jenkins pipeline can be created in two ways – Declarative Pipelines and Jenkins Job Builder. Jenkins CI helps in test automation, provides faster feedback, reduce the development cycle, and helps in continuous integration of code.

Teams can integrate their Selenium test suites with Jenkins CI on BrowserStack using a plugin. Teams can then test each build on 2000+ real devices and ship with confidence.

Start Automating For Free

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free