Jenkins Made Simple: Your Step-by-Step Guide to Automating CI/CD Pipelines
Image generated with DALL-E

Jenkins Made Simple: Your Step-by-Step Guide to Automating CI/CD Pipelines

From Installation to Deployment, Master the Art of CI/CD with Jenkins

What is CI/CD and Why You Should Care About Jenkins?


Jenkins Page

Imagine you’re working on a project with a team, and each team member is contributing code to the same repository. One person’s code doesn’t work well with the other’s, leading to bugs, headaches, and hours of fixing things. This scenario can slow down the development process and make things messy—this is where CI/CD comes into play.

CI/CD in a Nutshell

  • Continuous Integration (CI): This is like having a daily routine. You wake up, freshen up, and check in on your work. Similarly, in CI, every time someone pushes code to a repository, it’s automatically tested and integrated into the existing codebase. This ensures that the code works with what’s already there. So, no surprises, no mess.
  • Continuous Delivery (CD): Now, imagine you’ve prepped a meal and all that’s left is serving it to your family. CD does the same—automatically packaging and deploying your application to the production environment so that it’s always ready for users. It saves time and ensures no unexpected issues.


CI/CD flow

Why Jenkins?

Jenkins is like the conductor in an orchestra, making sure everything happens at the right time and in harmony. It automates your entire development process, ensuring your code is tested, integrated, and deployed without human intervention. Best of all, Jenkins is free, open-source, and packed with plugins that can integrate with practically any tool you use.

Key Takeaways:

  • Jenkins automates your CI/CD process so you don’t have to worry about manually testing and deploying code.
  • It’s open-source, flexible, and integrates with a wide range of tools.
  • Once set up, Jenkins can run automated tests, build your application, and even deploy it to production.


Jenkins Dashboard

Setting Up Jenkins – Installation Made Simple

Let’s imagine you’ve just moved into a new house. The first thing you’d want to do is set up the essentials—electricity, water, internet. Similarly, setting up Jenkins is the first step before you can start automating your CI/CD pipelines.


Jenkins Installation Flow

Prerequisites:

Before we can install Jenkins, we need one essential thing: Java. Jenkins runs on Java, so let’s get that set up first.

Step 1: Installing Java (JDK 11)

Run the following commands to install Java on your system:

sudo apt update
sudo apt install openjdk-11-jdk        

Once installed, verify that Java is working:

java -version        

This should display something like:

java version "1.8.0_421"
Java(TM) SE Runtime Environment (build 1.8.0_421-b09)
Java HotSpot(TM) Client VM (build 25.421-b09, mixed mode, sharing)

# Or
openjdk version "11.0.11" 2021-04-20        

Step 2: Adding the Jenkins Repository and Installing Jenkins

Now that Java is ready, let’s install Jenkins. Just like you set up internet in your home by choosing a provider, here you’ll add Jenkins from its official repository.

1. Add the Jenkins Repository:

wget -q -O - https://pkg.jenkins.io/debian/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'        

2. Install Jenkins:

sudo apt update
sudo apt install jenkins        

Step 3: Starting Jenkins

Let’s start Jenkins, just like turning on the lights in your new home:

sudo systemctl start jenkins
sudo systemctl enable jenkins        

Step 4: Access Jenkins in Your Browser

Now that Jenkins is up and running, it’s time to access the Jenkins dashboard. Open your browser and navigate to:

https://<your-server-ip>:8080        

Jenkins will ask for an administrator password, which you can find by running this command:

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

Paste the password, complete the setup, and you’ll be ready to go!

Next Steps:

Once you’re in, Jenkins will ask if you want to install some plugins. Go ahead and choose the recommended plugins. They cover the most common features you’ll need to get started.

Key Takeaways:

  • Jenkins relies on Java, so installing the JDK is the first step.
  • Setting up Jenkins is as simple as adding its repository and running a few commands.
  • Access Jenkins from your browser and install essential plugins to get started quickly.

Creating Your First Jenkins Job – Start Simple with Freestyle Projects

Now that Jenkins is up and running, imagine it like a personal assistant waiting for instructions. Your first task is to teach this assistant what you want it to do by creating a job. In Jenkins, a job is a task that automates a part of your development workflow. For this first exercise, we’ll keep things simple and create a Freestyle Job.


Jenkins Creating Jobs

A Freestyle Job is like a one-off task that Jenkins can handle, such as compiling code or running a quick test. Let’s walk through how to set up your first job and see Jenkins in action!


Step 1: Creating a New Freestyle Job

  1. Go to Jenkins Dashboard:
  2. Name Your Job:


Step 2: Add Source Code to the Job

In real-world scenarios, Jenkins typically works with code stored in a repository like GitHub. For now, we’ll assume you have a GitHub repository that contains a project you'd like to build.

  1. In your new job, scroll down to the Source Code Management section.
  2. Select Git and paste your repository URL.
  3. If your repository requires credentials (like a password or token), click Add Credentials to provide them.


Step 3: Define the Build Step

Once Jenkins knows where your source code lives, it needs to know what to do with it. This is where build steps come in. We’ll set up Jenkins to use Maven, a popular tool for building Java applications. You can follow the same approach for other tools.

  1. Scroll down to the Build section.
  2. Click Add Build Step and choose Invoke top-level Maven targets.
  3. In the Goals field, type the following command:

clean package        

This tells Maven to clean any previous build files and then package the project (usually into a .jar or .war file).


Jenkins Creating And Building Jobs

Step 4: Save and Build

Once you’ve added the build step, you’re ready to roll:

  1. Click Save to finalize the job configuration.
  2. At the left of the screen, click Build Now.

You’ll see a build pop up in the Build History section. Click on the build number to follow along with Jenkins as it runs your job. You can also check the Console Output to see what’s happening behind the scenes.


Freestyle Job Flow

What Happens Under the Hood?

When you trigger the build:

  • Jenkins fetches the code from your GitHub repository.
  • It uses Maven to clean and package the project.
  • The result (a .jar or .war file) is saved in Jenkins for you to use or deploy.

Key Takeaways:

  • Freestyle Jobs are a great way to get started with Jenkins.
  • You can configure Jenkins to automatically fetch code from a repository and build it.
  • The Build Now button allows you to manually trigger jobs and watch Jenkins do the heavy lifting.


Jenkins Pipeline Flow

Moving to Jenkins Pipelines – Automate and Scale with Ease

Now that you’ve dipped your toes into Jenkins with a Freestyle job, it’s time to step up the game with Jenkins Pipelines. Pipelines are like giving Jenkins a well-thought-out recipe. Instead of just telling Jenkins, “Build this once,” you’re giving it a step-by-step guide to follow every time someone pushes code.

With Jenkins Pipelines, you can:

  • Automate repetitive tasks (e.g., test, build, and deploy).
  • Scale your pipeline to handle different environments or branches.
  • Easily manage multiple steps, conditions, and actions in one place.

Think of it as leveling up from a simple task list to a full project plan, but for your code.

What’s a Jenkinsfile?

In Jenkins Pipelines, the instructions live in a special file called a Jenkinsfile. This file is placed in the root of your repository and tells Jenkins exactly what to do with your code—whether that’s testing, building, or deploying it.

A Jenkinsfile contains multiple steps organized into stages. It looks something like this:

pipeline {
    agent any
    tools {
        jdk 'jdk11'
        maven 'maven3'
    }
    stages {
        stage('Checkout Code') {
            steps {
                git url: 'https://github.com/your-repo.git', branch: 'main'
            }
        }
        stage('Build') {
            steps {
                sh 'mvn clean compile'
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }
        stage('Package') {
            steps {
                sh 'mvn package'
            }
        }
    }
}        

Breaking Down the Jenkinsfile:

  • Pipeline: This is the main block that holds the whole pipeline.
  • Agent: This defines where the pipeline will run. agent any means it can run on any available Jenkins node.
  • Tools: Here we tell Jenkins what tools are required (in this case, Java and Maven).
  • Stages: These are the different steps Jenkins will follow. Each stage represents a key part of the process.Checkout Code: Jenkins will pull the code from the GitHub repository.Build: Maven will compile the source code.Test: Jenkins will run tests using Maven.Package: Finally, Maven will package the application (typically into a .jar or .war file).


Step 1: Creating a New Pipeline Job

  1. Go to the Jenkins Dashboard.
  2. Click “New Item”.
  3. Name your project, something like My_First_Pipeline.
  4. Choose Pipeline and click OK.


Step 2: Configuring the Pipeline

  1. In the Pipeline section, you’ll have the option to either write the pipeline directly in Jenkins or link it to a Jenkinsfile in your GitHub repository. We’ll use the Jenkinsfile method because it allows for better version control.
  2. Select Pipeline script from SCM.
  3. Choose Git as the source, and enter the repository URL.
  4. Specify the branch where your Jenkinsfile resides (usually main or master).
  5. Jenkins will automatically look for the Jenkinsfile in the root of the repository.


Step 3: Save and Run Your Pipeline

Once you’ve set up the pipeline, click Save and then Build Now.

Jenkins will:

  1. Fetch your code from GitHub.
  2. Run the stages (Checkout, Build, Test, Package).
  3. Display the progress in the Build History and Console Output.

What’s Different from Freestyle Jobs?

  • Automated and Repeatable: Pipelines are designed to be automated for every push. They’re written as code (in the Jenkinsfile) and live with your project in version control.
  • Multi-Step: Instead of a single build step, you can define multiple stages that run in order.
  • Scalable: Pipelines can grow with your project, adding more stages, environments, or parallel builds.

Key Takeaways:

  • Pipelines use Jenkinsfiles to define the steps for building, testing, and deploying code.
  • You can create pipelines directly in Jenkins or link them to a Jenkinsfile in your repository.
  • Pipelines are flexible, scalable, and ideal for automating CI/CD processes across different environments.

Securing Your Code with SonarQube and OWASP Dependency Check

You’ve got Jenkins building and testing your code now, but let’s be honest—shipping buggy, insecure code is never a good idea. You want your software to not only function but to be clean and safe from vulnerabilities. This is where tools like SonarQube and OWASP Dependency Check come in handy.

Think of it like this: when building a house, you don’t just build the walls and roof; you inspect the structure to ensure it’s strong and stable. In the world of software development, SonarQube and OWASP act like inspectors, analyzing your code for potential issues before it goes live.


Jenkins Security Pipeline Flow

Step 1: Integrating SonarQube for Code Quality and Bug Detection

SonarQube is a powerful tool for code quality analysis. It helps detect code smells, bugs, and vulnerabilities early in the development process. It’s like having a second pair of eyes on your code, checking for issues that might have slipped through your tests.

Installing SonarQube Plugin in Jenkins

  1. In Jenkins, go to Manage JenkinsManage Plugins.
  2. In the Available tab, search for SonarQube Scanner and install the plugin.
  3. Once installed, go to Manage JenkinsConfigure System.
  4. Scroll down to SonarQube Servers and click Add SonarQube. Enter your SonarQube server URL and authentication token.

Adding SonarQube to the Pipeline

Now, let's add a SonarQube analysis stage to your Jenkinsfile. Here's how you would update the Jenkinsfile to integrate SonarQube analysis:

pipeline {
    agent any
    tools {
        jdk 'jdk11'
        maven 'maven3'
    }
    stages {
        stage('Checkout Code') {
            steps {
                git url: 'https://github.com/your-repo.git', branch: 'main'
            }
        }
        stage('Build') {
            steps {
                sh 'mvn clean compile'
            }
        }
        stage('SonarQube Analysis') {
            steps {
                script {
                    def scannerHome = tool 'SonarQube Scanner';
                    withSonarQubeEnv('SonarQube Server') {
                        sh "${scannerHome}/bin/sonar-scanner"
                    }
                }
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }
        stage('Package') {
            steps {
                sh 'mvn package'
            }
        }
    }
}        

What This Does:

  • SonarQube Scanner runs during the SonarQube Analysis stage, scanning your code for issues like bugs, code smells, and vulnerabilities.
  • Once the scan is complete, you can view the results directly in your SonarQube dashboard.


Step 2: Running OWASP Dependency Check for Vulnerabilities

Even if your code looks perfect, there might be issues lurking in the libraries or packages you use. This is where OWASP Dependency Check comes in. It scans your project’s dependencies to make sure you’re not using any libraries with known vulnerabilities.

Installing OWASP Dependency Check in Jenkins

  1. In Jenkins, go to Manage JenkinsManage Plugins.
  2. In the Available tab, search for OWASP Dependency Check and install the plugin.
  3. Once installed, go to Manage JenkinsConfigure System.
  4. Scroll down to OWASP Dependency Check and configure it by adding a new installation (the default settings usually work fine).

Adding OWASP Dependency Check to the Pipeline

Now, add an OWASP Dependency Check stage to your Jenkinsfile:

pipeline {
    agent any
    tools {
        jdk 'jdk11'
        maven 'maven3'
    }
    stages {
        stage('Checkout Code') {
            steps {
                git url: 'https://github.com/your-repo.git', branch: 'main'
            }
        }
        stage('Build') {
            steps {
                sh 'mvn clean compile'
            }
        }
        stage('SonarQube Analysis') {
            steps {
                script {
                    def scannerHome = tool 'SonarQube Scanner';
                    withSonarQubeEnv('SonarQube Server') {
                        sh "${scannerHome}/bin/sonar-scanner"
                    }
                }
            }
        }
        stage('Dependency Check') {
            steps {
                dependencyCheck additionalArguments: '--scan .'
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }
        stage('Package') {
            steps {
                sh 'mvn package'
            }
        }
    }
}        

What This Does:

  • OWASP Dependency Check scans the project for vulnerable libraries.
  • If any issues are detected, you’ll see them in the Jenkins console output and can investigate further.


Step 3: Reviewing Results and Fixing Issues

Once Jenkins runs the pipeline with both SonarQube and OWASP Dependency Check, you’ll be able to see:

  1. Code quality reports (from SonarQube) that highlight bugs, vulnerabilities, and code smells.
  2. Dependency vulnerability reports (from OWASP) that show any third-party libraries with known security risks.

By addressing these issues early, you’ll reduce the risk of deploying insecure or unstable software.

Key Takeaways:

  • SonarQube helps you maintain high code quality by identifying bugs, code smells, and vulnerabilities.
  • OWASP Dependency Check ensures that the libraries and packages you use are free from known vulnerabilities.
  • Integrating these tools into your Jenkins pipeline adds an important layer of security and quality assurance to your CI/CD process.

Deploying Your Application with Docker – The Final Step

By now, Jenkins is doing the heavy lifting—automating your builds, running tests, and ensuring your code quality and security. But, there’s one more crucial piece to the puzzle: deploying your application. This is where Docker comes in.

Docker allows you to package your application, along with everything it needs, into a single container that can be deployed anywhere. Think of Docker as a portable shipping container for your app. Whether you're deploying it on a local server, the cloud, or a colleague’s machine, it will run exactly the same.


Final Jenkins Pipeline

In this section, we’ll set up Jenkins to use Docker to build, push, and deploy your application in a Docker container.

Step 1: Setting Up Docker in Jenkins

First, we need to make sure that Jenkins can work with Docker.

Install Docker on Your Jenkins Server

If Docker isn’t installed on your Jenkins server yet, you can do so with the following commands:

sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker        

To allow Jenkins to use Docker commands without root permissions, add the jenkins user to the docker group:

sudo usermod -aG docker jenkins        

Restart Jenkins after this change:

sudo systemctl restart jenkins        

Step 2: Create a Dockerfile for Your Application

To deploy an application with Docker, we first need a Dockerfile. This file tells Docker how to build an image of your app. Here’s a simple example of a Dockerfile for a Java application:

# Use OpenJDK as the base image
FROM openjdk:11-jre-slim

# Add the JAR file to the Docker container
COPY target/myapp.jar /app.jar

# Set the command to run the app
ENTRYPOINT ["java", "-jar", "/app.jar"]

# Expose port 8080
EXPOSE 8080        

What’s Happening Here?

  • Base Image: We’re using an OpenJDK base image to run the app.
  • COPY: The JAR file (your application) is copied into the container.
  • ENTRYPOINT: Defines the command Docker runs when the container starts, in this case, running the JAR file.
  • EXPOSE: Exposes port 8080 so that the app can be accessed.


Step 3: Build and Push the Docker Image in Jenkins

Now that you’ve got your Dockerfile, let’s update your Jenkinsfile to build the Docker image, push it to a Docker registry (like Docker Hub), and then deploy the container.

Add Docker Stages to Your Jenkinsfile

Here’s how your Jenkinsfile might look after adding Docker steps:

pipeline {
    agent any
    tools {
        jdk 'jdk11'
        maven 'maven3'
    }
    stages {
        stage('Checkout Code') {
            steps {
                git url: 'https://github.com/your-repo.git', branch: 'main'
            }
        }
        stage('Build') {
            steps {
                sh 'mvn clean package'
            }
        }
        stage('Build Docker Image') {
            steps {
                script {
                    docker.build('myapp-image')
                }
            }
        }
        stage('Push Docker Image') {
            steps {
                script {
                    docker.withRegistry('https://index.docker.io/v1/', 'dockerhub-credentials') {
                        docker.image('myapp-image').push('latest')
                    }
                }
            }
        }
        stage('Deploy Docker Container') {
            steps {
                sh 'docker run -d -p 8080:8080 myapp-image:latest'
            }
        }
    }
}        

Breaking It Down:

  • Build Docker Image: Jenkins uses the Dockerfile to build a Docker image of your application.
  • Push Docker Image: Jenkins pushes the Docker image to your Docker Hub account. You’ll need to create Docker Hub credentials in Jenkins for this to work.
  • Deploy Docker Container: Jenkins starts the Docker container, making your app accessible on port 8080.


Step 4: Setting Up Docker Hub Credentials in Jenkins

To push the Docker image to Docker Hub, you need to store your Docker Hub credentials in Jenkins.

  1. Go to Manage JenkinsManage Credentials.
  2. Add a new set of credentials for Docker Hub:Kind: Username with password.Username: Your Docker Hub username.Password: Your Docker Hub password.ID: dockerhub-credentials (or another ID you prefer).

This allows Jenkins to authenticate with Docker Hub and push the image.


Step 5: Trigger the Pipeline

Once your Jenkinsfile is set up and Docker is ready, you can trigger the pipeline:

  1. Jenkins will checkout the code, build the JAR, and package it.
  2. It will then use Docker to build the image, push it to Docker Hub, and finally deploy the app by running it in a Docker container.

You can access the app by navigating to https://<your-server-ip>:8080 in your browser.


What’s the End Result?

  • Portable Application: Your app, packaged into a Docker image, can now be deployed anywhere—whether on your server, in the cloud, or on another machine.
  • Automation: Jenkins automates the entire process, from building the app to deploying it in a container.

Key Takeaways:

  • Docker makes your application portable and easy to deploy across different environments.
  • Jenkins can automate the process of building Docker images and deploying containers.
  • By integrating Docker with Jenkins, you’ve created a full CI/CD pipeline that tests, builds, and deploys your application automatically.

Wrapping Up Your Jenkins Journey

Congratulations! You’ve just completed an entire journey through Jenkins, covering everything from basic job setup to advanced deployment strategies using Docker. By now, Jenkins is doing much more than just handling your builds. It’s working as your personal assistant—automating everything from testing and code quality checks to pushing out Docker containers for deployment.


The final CI/CD pipeline

Here’s what you’ve learned:

  • What Jenkins Is: You started by understanding how Jenkins fits into the CI/CD pipeline, helping teams integrate and deliver code more effectively.
  • Setting Up Jenkins: You installed Jenkins, configured it to work with GitHub, and got familiar with how to manage your Jenkins instance.
  • Freestyle Jobs: You created your first Freestyle Job to see how Jenkins can handle simple tasks like compiling code.
  • Jenkins Pipelines: You moved on to the power of Jenkins Pipelines with Jenkinsfiles, enabling you to script multi-stage processes that include testing, building, and packaging your code.
  • Security with SonarQube and OWASP Dependency Check: You integrated tools that checked your code quality and scanned for vulnerabilities, adding an extra layer of security to your pipeline.
  • Deploying with Docker: Finally, you used Docker to containerize your application, automate the image building, and deploy it all within Jenkins. This gives you the ability to deploy consistent environments anywhere, from a local server to the cloud.

What’s Next?

Now that you’ve got Jenkins under your belt, where should you go from here?

1. Expand Your Pipeline:

  • Add more stages for things like staging environments or manual approvals for production releases.
  • Consider using Blue Ocean, a graphical interface for Jenkins that makes pipeline visualization even easier.

2. Explore More Jenkins Plugins:

  • Jenkins has a huge library of plugins, from Slack notifications to Kubernetes integration. Explore plugins that can make your pipeline more robust and integrated with your team's workflow.

3. Kubernetes & Jenkins:

  • If you’re dealing with containerized applications at scale, it might be worth looking into Jenkins integration with Kubernetes to manage your deployments across clusters.

4. Jenkins Best Practices:

  • Set up monitoring and alerting for your Jenkins instance to catch any pipeline failures early.
  • Use Declarative Pipelines and Jenkinsfiles for version control and pipeline standardization.
  • Implement backup strategies to ensure you don’t lose important job configurations and credentials.

A Few Final Tips:

  • Automate Everything: The beauty of Jenkins is in automation. The more you automate, the more time you save. Don’t be afraid to add new tools, hooks, and stages to your pipeline.
  • Start Small, Then Scale: If you’re working in a larger team or with complex applications, start with basic jobs and pipelines, then scale up. You’ll find that Jenkins can handle quite a lot once you understand the basics.
  • Stay Updated: Jenkins is constantly evolving. Keep your Jenkins instance updated and always check for new features and plugins that might make your life easier.

Thank you for following along with this Jenkins tutorial! You now have a strong foundation to build out complex, fully automated CI/CD pipelines that can scale with your projects. Keep experimenting, and don't hesitate to explore new tools and technologies to further enhance your automation workflows.

Happy automating with Jenkins!









要查看或添加评论,请登录

Nayeem Islam的更多文章