Two Approaches for Setting Up Jenkins for Continuous Integration
As the first phase of the CI/CD pipeline, Continuous Integration is a software development practice that helps DevOps teams speed up their releases of high-quality software. Continuous Integration is the first phase of the CI/CD pipeline, Continuous Delivery and Continuous Deployment make up the second and third phases.
Within Continuous Integration developers regularly merge their code changes into the main branch and then automated builds and tests run off these changes. Continuous Integration is the practice of building the code with every commit.
Many tools support Continuous Integration, but Jenkins has been a longstanding powerhouse. In this post, we will answer all of your questions on how to set up Continuous Integration with Jenkins, including:
- What is Jenkins?
- How does Jenkins help DevOps teams?
- How does Jenkins work?
- What is a Declarative Pipeline?
- What is Jenkins Job Builder?
- What are the benefits of using Jenkins to set up Continuous Integration?
- How to set up Continuous Integration with Jenkins?
- How to set up a Declarative Pipeline?
- How to set up a Jenkins Job Builder?
- How to ramp up your Continuous Integration practices
What is Jenkins?
Jenkins is one of the most popular CI tools that helps DevOps teams efficiently implement Continuous Integration. Jenkins is an open-source automation server that enables developers to reliably build, test, and deploy their software. Jenkins offers 1600+ plug-ins to support development processes and jobs like build management, source code management, platforms, UI, and administrative tasks.
Jenkins is one of the most popular tools to set up your CI/CD pipeline and implement Continuous Integration, because Jenkins works with almost any combination of languages and source code repositories, automates other routine development tasks, and is simple to learn and use.
How does Jenkins help DevOps teams?
Jenkins is a powerful tool for DevOps teams, as it enables developers to execute clean, consistent builds and deployments. Jenkins builds and tests software continuously, which makes the integration of changes easier over the lifetime of a project. It also allows teams to deliver software faster, thanks to automated testing and deployment technologies.
Jenkins helps DevOps to mitigate the risks of manual execution plans. DevOps teams using Jenkins can speed up their software development processes via automation without sacrificing quality.
How does Jenkins work?
Once Jenkins is configured, you’re ready to create projects for Jenkins to build. The best way to do this is to create a Jenkins Pipeline.
Jenkins Pipelines can be created in two ways:
- Declarative Pipelines - helps developers more efficiently write and read Pipeline code
- Jenkins Job Builder - takes simple descriptions of Jenkins jobs in the YAML or JSON formats and uses them to configure Jenkins
Both Declarative Pipelines and Jenkins Job Builder weave automation onto Jenkins and support the implementation of Continuous Integration. With each, the steps to build, test, and deliver each app become part of the application itself, stored as a Jenkinsfile.
What is a Declarative Pipeline?
A Declarative Pipeline provides a variety of functions that introduce simple conditional logic and use Groovy-compatible syntax. A Declarative Pipeline breaks down stages into individual stages. Each stage can also contain multiple steps.
The three main benefits of the Declarative Pipeline include the Pipeline supports simple conditional statement usage, allows for access to environment variables, and also provides options to add logging and error handling. Declarative Pipelines are also on the easier side to maintain and have a lower learning curve to adopt.
What is Jenkins Job Builder?
On the other hand, the Jenkins Job Builder (often abbreviated to JJB) is a python script that maintains and simplifies the configuration of Jenkins jobs. Jenkins Job Builder maintains jobs as simple descriptions in YAML or JSON formats. Then, jobs are expanded to XML and uploaded to Jenkins through its HTTP API.
The three main benefits of the Jenkins Job Builder are that the Jenkins configurations are stored as code, there are auditable version-controlled jobs, and job templates can be reused.
What are the benefits of using Jenkins to set up Continuous Integration?
There are several huge benefits to using Jenkins to set up Continuous Integration:
- Access and use of a widely adopted tool
- Multiple plug-ins for developer ease
- Supports multiple languages
- Simple to install and use
- Jenkins is free!
First and foremost, a big benefit of Jenkins is that it’s an open-source tool with more than 47,000 active installations and over one million users. This makes adopting Jenkins easier than ever for DevOps teams.
Additionally, the multitude of plug-ins for Jenkins ensures it can integrate with almost any software development, testing, or deployment tool.
Third, while Jenkins was based on Java, it supports multiple languages. For example, for C/C++, there are plug-ins to identify errors and warnings, generate build scripts with CMake, run unit tests, or perform code analysis.
Another benefit of Jenkins is that it is simple and quick to install. And even better than the simplicity and ease of use? Jenkins is free!
How to Set Up Continuous Integration with Jenkins
To set up Continuous Integration with Jenkins, there are five essential steps you will follow.
First, a developer commits the code to the main repository. Simultaneously the Jenkins server will regularly check the repository for any new changes.
Second, after the Jenkins server detects the new changes in the central repository, Jenkins pulls the changes and begins to prepare the new build.
Next, the build will either be a success or a failure. If the build fails, key stakeholders will be notified so the issue can be resolved. If the build succeeds, Jenkins will deploy the build in the test environment.
Following testing, Jenkins will generate feedback. Developers will be notified about the build and test results. Meanwhile, Jenkins will continue to regularly check the central repository for any changes. The process continues ad infinitum until a project is complete.
With Jenkins, Continuous Integration can be implemented more easily and efficiently. But, let’s go even deeper into specific steps, like how to set up a Declarative Pipeline and how to set up Jenkins Job Builder.
How to Set Up a Declarative Pipeline
To set up a Declarative Pipeline for Continuous Integration, you must first create a Jenkinsfile or the text file that contains the entire workflow as code. The Jenkinsfile can be checked into SCM/VCS and will trigger the pipeline, if already present. Otherwise, the pipeline will be created.
Next, head into the application and create a new file at the same level as the POM file.
You’re now writing your pipeline script, which will include:
- A pipeline - the tag that contains the entire process of the workflow
- The agent - specifies where the entire Pipeline, or a stage, will execute in the Jenkins environment
- The stages - include all the work to be carried out. This also dictates if one or more stages are executed sequentially.
Now it’s time to create the Pipeline. Go to Jenkins -> New item and select “Pipeline.” Select Pipeline script from the SCM. Be sure to provide the repository URL and credentials. Then, specify the script path and make sure the Jenkinsfile is at the root level.
Trigger the build. The pipeline will wait for an unspecified amount of time for user input, or you can introduce a timeout to avoid this. After 100 seconds, the Pipeline will be aborted without input. Or, the pipeline will continue, stop the build, and be marked as a Success.
Finally, it’s important to note that you can use Jenkins Pipelines to define steps to run after the Pipeline or stage's completion using a post block.
How to Set Up a Jenkins Job Builder
To set up a Jenkins Job Builder, you must first clone the source repository.
Second, navigate to the folder “etc,” which consists of the “.ini” sample file. Copy and paste the file, then rename it “jenkins_job.ini.”
Next, open up the “jenkins_job.ini” and fill out the three sections:
- User — Jenkins user name
- Password — Jenkins password
- URL — Jenkins instance URL
Now you’re configured, and it’s time to write the YAML files. YAML files are used for providing the configurations that create the Jenkins jobs. There are three kinds of YAML:
- singlejob.yaml — Creates the job per the configurations in the YAML
- singlejobtemplate.yaml — Creates the job as per the job template. This is reusable.
- multiplejobtemplate.yaml — Creates multiple jobs with the same configuration based on the job template.
Next, command to execute the YAML based on the specifically needed blocks, and observe the command line output. Now, the job will be created in Jenkins.
How to ramp up your Continuous Integration practices with Launchable
If you’re ready to superpower your CI/CD pipeline and implement a more effective Continuous Integration practice, it’s time to talk to Launchable.
Launchable offers a flexible architecture, which makes integration easy with favored languages and frameworks. And since Launchable uses machine learning to analyze test failures and source code, which means it doesn't rely on code syntax analysis. This means it's trivial for Launchable to add support for almost any file-based programming language and Launchable can scale across teams and projects with different languages and tools. Plus, Launchable is compatible with any CI server able to run shell commands.
Bottom line? Wherever your pipeline is, Launchable can help! Schedule your free 15-minute demo of Launchable today.