What is Jenkins and who should use it?

Updated: February 3, 2024 By: Guest Contributor Post a comment

Jenkins is an open-source automation server that provides an easy way to set up a continuous integration and continuous delivery (CI/CD) pipeline. In this tutorial, we’ll dive deep into what Jenkins is, its core features, and why it’s so popular among developers and teams of all sizes. We will walk through basic to advanced Jenkins concepts, complete with code examples where applicable. Whether you’re a solo developer or part of a larger team, Jenkins can significantly enhance your workflow. Let’s get started.

Introduction to Jenkins

Jenkins is a powerful automation tool used for automating the parts of software development related to building, testing, and deploying, facilitating continuous integration and continuous delivery. It is a server-based system running in a servlet container such as Apache Tomcat. Jenkins supports version control tools like Git, Subversion, and Mercurial and can execute Apache Ant, Apache Maven, and sbt based projects as well as arbitrary shell scripts and Windows batch commands.

The installation of Jenkins is straightforward. It can be installed on various operating systems, including Windows, Linux, and macOS. Here’s how you can install Jenkins on a Linux machine:

sudo wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins

Once installed, Jenkins can be started using:

sudo systemctl start jenkins

After starting Jenkins, you can access the Jenkins dashboard by navigating to http://localhost:8080.

Setting Up Your First Jenkins Job

Setting up your first Jenkins job is a simple process. Jobs can range from a simple task that executes a script to complex pipelines that orchestrate large-scale software deployments. To set up a basic build job, follow these steps:

  1. Navigate to the Jenkins dashboard.
  2. Click on ‘New Item’.
  3. Enter a name for your job, select ‘Freestyle project’, and click ‘OK’.
  4. In the job configuration, scroll down to the ‘Build’ section and click ‘Add build step’ -> ‘Execute shell’ (on Linux) or ‘Execute Windows batch command’ (on Windows).
  5. In the command box that appears, enter your build command. For example, for a simple Java project, you might use javac HelloWorld.java.
  6. Click ‘Save’.

You’ve just created your first Jenkins job! To test it, click ‘Build Now’. This will execute the defined build step and, if your build script was correct, should successfully compile HelloWorld.java.

Integrating Jenkins with Source Control

One of Jenkins’ most powerful features is its ability to integrate with source control systems like Git. This integration allows Jenkins to automatically trigger builds when changes are pushed to a repository. To set up a Jenkins job that builds a project from a Git repository, follow these steps:

  1. Assume you have a GitHub repository with a Java project. In Jenkins, create a new job as before, but this time select ‘Pipeline’.
  2. In the ‘Pipeline’ section, choose ‘Pipeline script from SCM’.
  3. Select ‘Git’ as the SCM, and enter your repository URL.
  4. Add your credentials if the repository is private.
  5. In the ‘Branches to build’ section, specify the branch that Jenkins should build from (e.g., ‘master’).
  6. Click ‘Save’ and then ‘Build Now’ to trigger the build.

That’s it! You’ve now configured Jenkins to build a project directly from a Git repository. Any changes pushed to the selected branch will automatically trigger a new build.

Deploying Your Application with Jenkins

Deployment is another critical aspect of CI/CD, and Jenkins excels here too. To deploy your application using Jenkins, you can use the ‘Deploy to container’ plugin or write custom scripts. For instance, if you have a simple web application that you want to deploy to a Tomcat server, you can follow these steps:

  1. Ensure the ‘Deploy to container’ plugin is installed in Jenkins.
  2. Configure your build job to archive the .war or .jar file as a build artifact.
  3. Create a new ‘Post-build action’ and select ‘Deploy war/ear to a container’.
  4. Enter your Tomcat server’s credentials and the path to your application.
  5. Click ‘Save’ and run your build.

After the build completes, Jenkins will automatically deploy your application to the specified Tomcat server.

Advanced Jenkins Features

While Jenkins is user-friendly for beginners, it’s also packed with advanced features for power users. Pipeline as Code with Jenkinsfile, parameterized builds, distributed builds across multiple nodes, and integration with Docker are just a few examples. For more complex projects, you can leverage these features to create a robust CI/CD pipeline tailored to your needs.

Here’s a simple example of a Jenkinsfile demonstrating a Pipeline as Code:

pipeline {
    agent any
    stages {
        stage('Build') { 
            steps { 
                echo 'Building..'
            }
        }
        stage('Test') { 
            steps { 
                echo 'Testing..'
            }
        }
        stage('Deploy') { 
            steps { 
                echo 'Deploying..'
            }
        }
    }
}

This Jenkinsfile describes a simple pipeline with three stages: Build, Test, and Deploy. By placing this file in your repository, Jenkins can automatically detect and execute it, providing a highly customizable and declarative pipeline configuration.

Conclusion

In this tutorial, we’ve covered the basics of Jenkins, from installation and creating your first job to integrating with source control and deploying applications. Jenkins’ flexibility and wide range of plugins make it a powerful tool for automating all aspects of software development, particularly for those looking to implement CI/CD in their workflow. Whether you’re a beginner or an experienced developer, Jenkins has something to offer everyone.