Skip to main content
Logo

Build a Jenkins CI/CD Pipeline: Step-by-Step Guide

Posted On: 1 November 2024

Jenkins is an open-source automation server that tests CI/CD pipelines. The pipeline enables teams to automatically perform crucial software delivery lifecycle steps like building code changes, testing them, and deploying them, reducing human error and accelerating the software development process. With source control integration (Git is a popular choice) and a robust plugin ecosystem, Jenkins also caters to the varying needs of your project—from simple apps to distributed architecture.

To set up a Jenkins CI/CD pipeline, you first install and configure it to suit your development environment and the source control repository where your application will be hosted. After configuration, Jenkins monitors your repository, triggers the automated build, and tests for every change in code. By automating processes, Jenkins CI/CD pipelines improve code quality and foster a culture of openness and responsibility within development teams.

What are CI/CD pipelines?

The CI/CD pipeline refers to automation steps that move code from development to production. It consists of multiple stages to facilitate code integration and delivery while defining a structured process for testing, building, and releasing each change.

A CI/CD pipeline usually consists of testing your code, building a new version of your application, pushing the changes to a central repository, and deploying the app onto a server. Such an approach helps speed up the development cycles, enhance code quality, and deliver features and updates to end-users more reliably and efficiently.

  • Test code
  • Build application
  • Push it to the repository
  • Deploy this to the server

CI, or Continuous Integration, automates the integration of code changes from multiple developers into a single codebase and frequently commits their work to the central code repository (GitHub or Stash). It helps identify and resolve bugs quicker, ensures that integrating code across a team of developers is easy, and improves software quality at a reduced time. Some popular CI CD tools are Jenkins, TeamCity, and Bamboo.

CD or continuous delivery is carried out after continuous integration, which helps ensure that all new changes are released to end-users without errors. This also includes running integration and regression tests in the staging area (like in the production environment); thus, the final release is intact and is not broken in production. CI/CD also ensures that every change is validated via test automation.

SUGGESTED READ - What Is Jenkins in DevOps

What is Jenkins CI/CD?

Jenkins is a Java-based open-source server that allows developers to build, test, and deploy software. It is used for continuous integration of projects or end-to-endpoint test automation.

Jenkins started as a continuous integration tool, but now it covers the whole software delivery pipeline, including deployment. The Jenkins Pipeline is a user-made model for continuous delivery, and it incorporates various plugins that help define procedure steps from version control to the users. It integrates with popular version control systems and builds tools, testing frameworks, deployment platforms, and monitoring tools; thus, it provides a smooth automated workflow for software development.

Jenkins also defines the entire CI/CD pipeline as code, improving its versioning and reusability. The Jenkins CI/CD pipeline can automate tasks, improve collaboration, and enable efficient software releases.

Why use Jenkins for CI/CD?

Jenkins facilitates the automation of several stages of the software development lifecycle, such as application development, testing, and deployment.

  • It is a server-based application and requires a servlet container like Apache Tomcat to run on various platforms like Windows, Linux, macOS, Unix, etc.
  • To use Jenkins, pipelines or a series of steps must be set up for the Jenkins server.
  • The Jenkins CI Pipeline is a robust machine consisting of tools designed to host, monitor, and compile test codes /changes.
  • It uses a Continuous Integration Server (Jenkins, Bamboo, CruiseControl, TeamCity), a source control tool such as CVS, SVN, GIT, Mercurial, Perforce, ClearCase, a build tool such as Make, ANT, Maven, Ivy, Gradle, and finally an automation testing framework such as Selenium, Appium, TestComplete, UFT, etc.

How does the Jenkins CI/CD pipeline work?

CI/CD is an end-to-end automated process involving steps of frequent code integration, automated testing, and continuous deployment of software changes to production.

1. Code integration is when developers commit their code changes to a remote repository (such as GitHub, GitLab, or BitBucket) and integrate them with the main codebase. This helps ensure the code changes align with the rest of the codebase and keep up with the build.

2. Automated testing involves running a suite of tests to check if code changes are functional, pass through expected quality standards, and are free of defects. This can help identify issues early and get developers to fix them quickly and efficiently.

3. Continuous deployment: code changes are automatically deployed to a staging environment to test further. This ensures that software is continuously updated with changes, delivering new features and functionality quickly and efficiently.

4. Production deployment: software changes are released to end-users. Finally, the production environment is monitored, ensuring the software functions smoothly identifying and fixing issues.

These four steps of a CI/CD pipeline work in tandem to ensure that software changes are tested, integrated, and deployed to production seamlessly, helping development teams achieve faster release cycles, reduce risks of software defects, and improve user experience.

How to set up Jenkins CI/CD pipeline

Jenkins is distributed as a WAR (web archive) file or as installer packages for the major operating systems: a Homebrew package, a Docker image, and source code. The Jenkins file script is primarily Java-based and supports installation and scaling on Kubernetes. It can be executed on the Jenkins WAR standalone or as a servlet in a Java application server such as Tomcat.

In any way, it produces a web user interface and accepts calls to its REST API (Application Programming Interface). When running for the first time, Jenkins creates an administrative user with a long random password that can be pasted onto the initial web page to unlock the installation.

1. Download and install Jenkins and choose the platform to run it: Windows, Linux, macOS, or within a Docker container. Initial Setup: The password can be found in the Jenkins console log or in a file specified in the log (typically a secrets/initial AdminPassword file).

2. To Configure Jenkins, access it through a web browser at http://localhost:8080 (or the appropriate URL if not run locally). To install Plugins: Suggested or specific plugins can be chosen and installed during setup. For a CI/CD pipeline, plugins such as Git, Pipeline, and Docker can be used, and an admin user for Jenkins can be created to manage access.

3. To create a Job/Project, click "New Item" on the Jenkins dashboard and select project type. Choose 'Freestyle project' for a simple scenario or 'Pipeline' for more complex workflows. Under the Source Code Management tab, select Git (or another version control system) and provide the repository URL and credentials if required.

4. To automate Builds: Under the "Build Triggers" tab, triggering builds can be chosen on SCM (Source Control Management) change (like a Git push) or at regular intervals (using cron syntax).

5. To define the build steps, first add steps that Jenkins should execute, like compiling code, running tests, etc. This will depend on the specific project's language and framework.

6. To set up testing and reporting, automate steps in the build phase and include commands to run tests. For instance, for a Java project, this might be running a Maven test. Configure Jenkins to publish test reports. Many testing frameworks are supported with appropriate plugins.

7. To implement Continuous Deployment/Delivery, the first step is the deployment step; in the Post-build Actions, add steps for deployment. This could be deployed to a staging or production server or triggered by a Docker image build and pushed to a registry. Pipeline as Code (Optional): For more complex pipelines, use the Jenkinsfile to define the pipeline as code. This file is checked into the SCM.

8. To configure Notifications: Set up notifications (like email, Slack, etc.) for build success or failure.

9. Regularly check the build success/failure rates and optimize as needed to maintain and monitor builds. Keep Jenkins and its plugins updated for new features and security patches.

10. Ensure Jenkins is secured through proper authentication and authorization settings and regularly back up the Jenkins configuration and data.

The Jenkinsfile, which defines the pipeline as code, is written in Groovy and outlines the stages of the CI /CD pipeline, such as building, testing, and deploying a given application. Here is a basic example of this file for a Java-based project. This is given based on Git as the version control system and Maven for building the project. The script can be adjusted based on the project's language, tool, and deployment needs.

pipeline { agent any environment { // Define environment variables if needed MAVEN_HOME = '/path/to/maven' } stages { stage('Checkout') { steps { // Checking out the code from the Git repository checkout scm } } stage('Build') { steps { // Building the project with Maven sh "${MAVEN_HOME}/bin/mvn clean package" } } stage('Test') { steps { // Running tests using Maven sh "${MAVEN_HOME}/bin/mvn test" } post { // Handling test results and artifacts always { junit '**/target/surefire-reports/TEST-*.xml' archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true } } } stage('Deploy') { steps { // Add deployment steps here // For example, deploying to a staging or production environment echo 'Deploying application...' // sh 'deployment-script.sh' } } } post { always { // Actions to perform after the pipeline execution echo 'Pipeline execution is complete.' } } }
  • Pipeline Block: Defines the pipeline and contains all stages.
  • Agent: Specifies where the pipeline will run. any means it can run on any available agent.
  • Environment: Used to define environment variables.
  • Stages: The core part of the Jenkins pipeline. Each stage block can have several steps.
  • Checkout: Checks out the source code from the configured source control management (SCM), like Git.
  • Build: Compiles the code. Here, it is using Maven (mvn clean package).
  • Test: Runs tests and reports the results. This example assumes JUnit tests with Maven.
  • Deploy: Deploys the application. This is a placeholder and should be replaced with actual deployment steps, which vary depending on where and how the application is deployed.
  • Post: Defines actions that occur after the pipeline stages complete, such as cleanup, notifications, etc.

Conclusion

Remember, this is just a basic example to get started. Setting up a Jenkins CI/CD pipeline is a great way to get your hands on a quicker and more robust way to integrate your entire chain of build, test, and deployment tools. This ultimately leads to a powerful, extensible pipeline that helps your team spend less time on repetitive work and more on invention while increasing productivity and software quality. Jenkins's extensibility and its vast ecosystem of plugins ensure that your CI/CD pipeline is scalable with your project and fosters a CI/CD culture in your software development lifecycle.

Talk to us for more on understanding the role of CI/CD in test automation and how to get started with one.

Geosley Andrades

Director, Product Evangelist at ACCELQ

Geosley is a Test Automation Evangelist and Community builder at ACCELQ. Being passionate about continuous learning, Geosley helps ACCELQ with innovative solutions to transform test automation to be simpler, more reliable, and sustainable for the real world.

Discover More

Top continuous integration toolsBlogCI/CDTop 10 Continuous Integration Tools In 2024
11 July 2024

Top 10 Continuous Integration Tools In 2024

Discover the continuous integration tools tailored for DevOps teams. Compare the features and select the best CI/CD tool for your project.
What is CI/CD PipelineBlogCI/CDWhat Is CI/CD Pipeline? A Comprehensive Guide
25 March 2024

What Is CI/CD Pipeline? A Comprehensive Guide

Understanding what is CI/CD pipeline, can speed up software updates and smooth out your team’s work. Perfect for improving their software project.

Get started on your Codeless Test Automation journey

Talk to ACCELQ Team and see how you can get started.

Close Menu