Pravesh Sudha

Mastering Jenkins: A Comprehensive Guide to CI/CD Automation 🚀🚀💯

·

8 min read

Cover Image for Mastering Jenkins: A Comprehensive Guide to CI/CD Automation 🚀🚀💯

"Jenkins is the heart of your CI/CD pipeline, automating the repetitive tasks and enabling your team to focus on innovation."

đź’ˇ Introduction

According to official Jenkins Docs, Jenkins is a self-contained, open-source automation server that can be used to automate all sorts of tasks related to building, testing, and delivering or deploying software. Jenkins can be installed through native system packages, Docker, or even run standalone by any machine with a Java Runtime Environment (JRE) installed. In lame terms, it is like a helper for software teams. It does boring tasks, like checking if code works and putting it where it needs to be. It tells you if something's wrong and makes work faster.

đź’ˇJenkins Architecture

The following image shows the overall architecture of Jenkins:

Alt text

The following are the key components of Jenkins:

  • Jenkins Master Node

  • Jenkins Agent Nodes/Clouds

  • Jenkins Web Interface

đź’ˇJenkins Master (Server)

Jenkins’s server or master node holds all key configurations. It is like a control server that orchestrates all the workflow defined in the pipelines. For example, scheduling a job, monitoring the jobs, etc. Let’s have a look at the key Jenkins master components.

  • Jenkins Jobs: A job is a collection of steps that you can use to build your source code, test your code, run a shell script, run an Ansible role in a remote host or execute a terraform play, etc. We normally call it a Jenkins pipeline.

  • Jenkins Plugins: Plugins are community-developed modules that you can install on your Jenkins server. It helps you with more functionalities that are not natively available in Jenkins.

  • Jenkins Credentials: When you set up Jenkins pipelines, there are scenarios where it needs to connect to a cloud account, a server, a database, or an API endpoint using secrets.

    In Jenkins, you can save different types of secrets as a credential.

    • Secret text

    • Username & password

    • SSH keys

All credentials are encrypted (AES) by Jenkins. The secrets are stored in $JENKINS_HOME/secrets/ directory. It is very important to secure this directory and exclude it from Jenkins backups.

đź’ˇJenkins Agent

Jenkins agents are the worker nodes that execute all the steps mentioned in a Job. When you create a Jenkins job, you have to assign an agent to it. Every agent has a label as a unique identifier.

Alt text

You can have any number of Jenkins agents attached to a master with a combination of Windows, Linux servers, and even containers as build agents.

đź’ˇJenkins Web-Interface

Jenkins 2.0 introduced a very intuitive web interface called “Jenkins Blue Ocean“. It has a good visual representation of all the pipelines.

Alt text

đź’ˇInstall Jenkins on Ubuntu

  • Step 1: Log in to the server and update the package list.

sudo apt -y update

  • Step 2: Jenkins needs the Java runtime as it is based on Java. Install open JDK 11 package using the following command.

sudo apt install default-jdk -y

  • Step 3: Add the Jenkins Debian repo.
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
  • Step 4: Update the package list again.

sudo apt update -y

  • Step 5: Install the latest LTS Jenkins.

sudo apt-get install jenkins -y

  • Step 6: Start the Jenkins service & enable it for starting during bootup.
sudo systemctl start jenkins
sudo systemctl enable jenkins
  • You can check the status of the Jenkins service using the following command.

sudo systemctl status jenkins

  • Step 7: Now you will be able to access the Jenkins server on port 8080 using the IP address.

Alt text

  • Step 8: As you can see in the above image, you need to provide the administrative password. You can get the password using the following command.

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Copy the password and click continue.

  • Step 9: Next, you will be asked to configure plugins as shown below. Select the “Install Suggested Plugins” option. This will install all the required plugins for building your projects. It will take a few minutes to install the plugins.

đź’ˇJenkins Pipeline Example

This is a basic example of a Jenkins pipeline defined in a Jenkinsfile. Jenkins pipelines are used to automate building, testing, and deploying code. In this example, we'll create a simple pipeline that builds and tests a hypothetical application.

  1. Create a Jenkinsfile in the root of your application repository. This file defines the steps of your Jenkins pipeline.
  •       pipeline {
              agent any
              stages {
                  stage('Build') {
                      steps {
                          sh 'echo "Building the application..."'
                      }
                  }
                  stage('Test') {
                      steps {
                          sh 'echo "Running tests..."'
                      }
                  }
                  stage('Deploy') {
                      steps {
                          sh 'echo "Deploying the application..."'
                      }
                  }
              }
          }
    

    Add your git credentials to your Jenkins Here, is a video for that purpose

  1. Create or Configure a Jenkins Job: Assuming you already have a Jenkins job set up, or you want to create a new one, follow these steps:
  • Click on "New Item" to create a new Jenkins job or open an existing one.

  • Configure your job as needed, such as specifying build triggers, build steps, and post-build actions.

  1. Specify Git Repository: In your Jenkins job configuration:

    • In the "Source Code Management" section, select "Git."

    • In the "Repository URL" field, enter the URL of your GitLab repository.

    • Under "Credentials," select the GitLab credentials you added earlier from the dropdown.

    • In the "Branches to build" field, specify the branch you want to build. You can enter the branch name directly (e.g., main or develop) or use a wildcard pattern like */main to match multiple branches.

  2. Save and Run: After configuring your job, click "Save" to save the changes. Now, Jenkins is set up to use your GitLab credentials and build the specified branch.

  3. Trigger the Job: You can manually trigger the job by clicking "Build Now" on the job's dashboard page, or you can set up triggers to automate builds based on your preferred criteria.

đź’ˇUnderstanding the Pipeline

  • pipeline: Defines the entire Jenkins pipeline.

  • agent any: Specifies that the pipeline can run on any available agent (Jenkins worker node).

  • stages: Contains a list of stages to be executed sequentially.

  • stage: Defines a specific stage within the pipeline.

  • steps: Contains the commands or steps to be executed within the stage.

  • sh: Executes shell commands.

  • The "Build," "Test," and "Deploy" stages are placeholders; customize them according to your project's needs.

đź’ˇ Pros and Cons of Scripted and Declarative Jenkins Files

Jenkins pipelines can be defined using two main syntaxes: Scripted and Declarative. Both have their advantages and disadvantages and choosing between them depends on your specific use case and preferences.

đź’ˇScripted Jenkinsfile

👉 Pros:

  1. Flexibility: Scripted pipelines are highly flexible. You have full control over the build process, which is written in Groovy scripting language. You can execute arbitrary code, integrate external tools, and handle complex build scenarios.

  2. Mature: Scripted pipelines have been around for a long time and are well-documented. You can find extensive resources, examples, and community support for Scripted pipelines.

👉 Cons:

  1. Complexity: Due to their flexibility, Scripted pipelines can become complex and harder to maintain, especially for large and intricate build processes. They may require a deeper understanding of Groovy.

  2. Readability: Scripted pipelines can be less readable than Declarative pipelines because they involve more Groovy scripting, which can make the pipeline less accessible to non-developers.

đź’ˇDeclarative Jenkinsfile

👉 Pros:

  1. Simplicity: Declarative pipelines are designed to be simple and human-readable. They use a structured syntax that makes it easy to define stages and steps without diving into scripting.

  2. Easy to Learn: Declarative pipelines are easier for beginners to understand. You don't need extensive Groovy knowledge to create them, making them more accessible to a wider audience.

  3. Built-in Syntax Checking: Jenkins provides built-in syntax checking for Declarative pipelines, helping catch errors early in the development process.

👉 Cons:

  1. Limited Flexibility: Declarative pipelines are more opinionated and may not cover all possible build scenarios. For complex or non-standard requirements, you might need to resort to Scripted pipelines.

  2. Less Control: Declarative pipelines provide less control over the build process. If you need custom logic or complex conditions, you might find it challenging to achieve with Declarative syntax alone.

  3. Limited Reusability: Reusing parts of a Declarative pipeline in other projects can be challenging, as they are designed to be self-contained


đź’ˇ Conclusion

👉 In this comprehensive guide to Jenkins for Software Engineers, we've covered a wide range of topics essential for mastering CI/CD in Jenkins. From understanding the basic concept of continuous integration to understanding Jenkins, and writing pipeline scripts, you now have a solid foundation to dive deeper into the world of CI/CD.

👉 Remember that practice makes perfect. The more you work with the commands, the more comfortable and proficient you'll become. Experiment with various commands, create Pipeline scripts for automation and explore the vast capabilities Jenkins offers.

👉 For a more in-depth discussion about CI/CD, stay tuned for my upcoming blog where I'll delve into a variety of DevOps tools.

👉 With the knowledge gained from this guide and continuous exploration, you'll be well-equipped to excel as a Software Engineer working in Jenkins environments. Happy coding and exploring the world of DevOps!