Jenkins is a self-contained, open source automation server built in Java. It sets up a continuous integration/continuous delivery (CI/CD) environment for software projects using pipelines and automates routine development tasks like building, testing, and deploying software.
The History of Jenkins
Jenkins is a fork from the Hudson project developed by Kohsuke Kawaguchi, a Java developer working at Sun Microsystems. The change of name followed after Oracle forked the project and claimed its rights in 2011. Hudson, the Oracle fork, was later donated to Eclipse in 2017, where Eclipse continued developing it. After some time, it became obsolete.
Jenkins was a famously preferred alternative to Cruise Control and other build-servers, especially around 2007. The CD Foundation, a Linux Foundation organization, currently manages Jenkins as an open source project.
It’s also worth noting that significant advancements have occurred since the conception of continuous integration. Contrary to before, when making daily builds at a precise time was the norm, now developers commit their work more frequently whenever they make a build with many changes. This milestone comes with many benefits, such as access to constant feedback on the software’s status. Additionally, errors can easily be detected in the early stages of development through testing during continuous integration, making it easy to resolve them.
Uses of Jenkins
Jenkins implements continuous integration/continuous delivery (CI/CD) and deployment workflows called pipelines. It’s an essential tool for DevOps engineers because it helps them identify errors in the code during a project’s early stages.
As an automation server and a continuous integration tool, it continuously builds and tests the software, thus allowing the developers to more frequently commit and integrate their changes into the build. Jenkins compiles the changes in the code and notifies the developer if the build fails. Eventually, it deploys the code to production when the build succeeds.
Real-world Applications of Jenkins
Using Jenkins in the IT industry has become more popular than ever. This is because Jenkins enables you to easily configure and extend, thanks partly to several plugins that enhance its functionality.
Here are just some of Jenkins’ real-world applications:
- The continuous integration feature of Jenkins enables code testing against the current code-base state in a production-like setup. This removes possible failures or errors from inconsistencies between the code pushed and new changes in the existing code. This feature is what we call the manual test option.
- Jenkins also offers excellent data support for project management. Every project management activity is considered a Jenkins job. Jenkins identifies the success or failure of each job and can measure the job completion time, thus making the entire project management process efficient.
- Continuous integration prevents broken code from being shipped to the production environment. It does this by merging code only after it successfully passes code tests, ensuring the master builds are not broken. Jenkins triggers a warning to developers if the master build ever does break.
- Jenkins can integrate with the Slack communication platform to synchronize communication activities and share them with others. The communication activities include activity updates, the time these activities are triggered, and the usernames of the senders and recipients.
How Jenkins Works
Jenkins runs as a server across several operating systems, including Windows, macOS, and Linux, using the Java 8 virtual machine (VM). Alternatively, the Oracle Java Runtime Environment (JRE) runs it as a Java servlet within a Jetty application. Other Java applications — such as Apache Tomcat and the Docker container — can also run Jenkins.
Jenkins operates using pipelines made up of a suite of steps. The Jenkins server takes these steps to perform the required jobs of the CI/CD process. A plain text file called Jenkinsfile
(defined by a domain-specific language (DSL)) contains the necessary instructions (declared in JSON-like syntax) required to run the Jenkins pipeline. When the Jenkins server reads the Jenkinsfile
and executes its instructions, it pushes the code from the committed source code to the production runtime down the pipeline.
Jenkins Server (Administrator-Agent)
Jenkins has a distributed administrator-agent architecture where the Jenkins server is the administrator. This server controls the workflow of jobs through the pipelines to the agents. It also schedules the jobs and assigns builds to these agents to execute these jobs. The server also tracks the offline or online status of the agents and replays the build result responses to the console output.
Jenkins Pipelines
A Jenkins pipeline is a collection of interlinked jobs or events that use automation tools to support continuous delivery pipeline integration and implementation for software development workflow.
Continuous delivery pipelines deliver the software from version control systems like GitHub, GitLab, and Bitbucket directly to intended users and customers of the software.
Another item to note is that Jenkins pipelines are either declarative or scripted.
A Declarative Pipeline
A declarative pipeline uses the Groovy-compatible syntax to create Jenkins pipelines. It enables you to control a pipeline’s execution easily. This pipeline begins with a pipeline block that contains multiple executable steps defined within a stage block. It also defines an agent that specifies where to run the pipeline.
The mandatory outer user-defined block pipeline invokes the Jenkins pipeline plugin.
A “stage block” is a collection of executable steps that visualize the Jenkins pipeline process, and a “step” is a specific task executing a particular process precisely.
Let’s look at an example of multiple stages performing a precise task:
pipeline { agent any stages { stage('A') { steps { echo 'print alphabet A.' } } stage('B') { steps { echo 'print alphabet B.' } } stage('C') { steps { echo 'print alphabet C.' } } } }
In the above example code, any means you can use any available agent to run the pipeline or the stage.
Alternatively, you can specify the agent to run the pipeline by declaring the container to use, like in the example below:
pipeline { agent { docker { image 'new-windows' } } }
A Scripted Pipeline
With the help of a lightweight executor, the scripted pipeline syntax runs on the Jenkins master. It converts the pipeline into commands using the node block, where a node is a machine that runs Jenkins.
The following is an example of the scripted pipeline that implements a for
loop.
node { for (int x=4; x>0; x-=2){ stage "stage #"-2 print 'x value decreases by 2' if(x==2) { git branch: 'master', url: 'https://github.com/kimani/scriptedpipeline.git' echo 'Stage #2 running.' } else { echo 'Stage #4 running.' } } }
The conditional for loop creates Stage #2
and Stage #4
and prints out the x value decreases by 2
message afterward. Stage #2
clones the specified git
repo within the if-else
statement and executes the echo command to print the message Stage #2 running
when x is equal to 2. When x is not equal to 2, Stage #4
runs the echo statement Stage #4 running
.
Jenkins Plugins
Plugins enhance the Jenkins system functionalities for projects written in programming languages other than Java.
Currently, over 1,500 Jenkins plugins are available for installation on a Jenkins controller. They allow integration with several build tools, most version control systems, cloud providers, and analysis tools. For example, some plugins can add new functionality to Jenkins or revamp the Jenkins look. Other plugins like JUnit perform unit tests that generate test reports.
You can automatically download the plugins with their dependencies from the Update Center. This service provides an inventory of open source plugins that members of the Jenkins community develop and maintain.
More Examples of Existing Jenkins Plugins
Jenkins can also use the Mailer plugin, which configures email notifications. It sends an email to the specified recipient whenever a build fails, the build is unstable, or when the build succeeds after a failure.
The Credential plugin stores credentials in Jenkins by providing a standardized API to store and retrieve credentials by other plugins.
Advantages of Jenkins
- Jenkins is open source and free to use.
- It’s platform-independent since it’s built with Java, thus contributing to its wide deployment.
- The Jenkins community offers incredible support.
- Jenkins has over 1,500 plugins that enhance flexibility by allowing it to build, deploy, and automate projects over various platforms.
Disadvantages of Jenkins
- There’s a lack of regular plugin maintenance, making their use difficult since many are outdated.
- Many of these plugins don’t provide the final support required to complete the deployment cycle. This makes the tracking of the project deployment difficult.
- Jenkins forces you to maintain your infrastructure by yourself as a developer, which is not ideal, especially for novice developers.
Conclusion
Jenkins provides CI/CD functionality using pipelines to enhance the software development process by automating project build, test, and deployment. It enables teams to release high-quality software at a faster rate.
Its usefulness keeps gaining popularity across the globe. This is quite evident given its daily application within many organizations and its many plugins.
Though it has a few disadvantages (like anything else), Jenkins is the to-go tool if you want to accelerate the software development process for your project and improve your code quality.