Table Of Contents

Jenkins Unleashed: Transforming Your CI/CD Workflow for Lightning-Fast Delivery

Why Use Jenkins for CI/CD?

Jenkins is not just a tool; it's a culture shifter. With its unparalleled flexibility, thousands of plugins, and vibrant community, Jenkins transforms how teams approach Continuous Integration and Continuous Delivery (CI/CD). Whether you’re a small startup racing to push your MVP or a large enterprise managing complex workflows, Jenkins empowers you to automate repetitive tasks, reduce errors, and accelerate delivery pipelines.

But why Jenkins? It’s open-source, highly customizable, and scales effortlessly with your team’s growing needs. It’s the bridge between developers and operations teams, breaking down silos and fostering collaboration in ways you’ve never experienced before.

In this labs, we’ll explore the magic of Jenkins and why it’s the go-to choice for CI/CD pipelines. Ready to revolutionize your development workflow? Let’s dive in!

Setting Up CI/CD in Jenkins

To start setting up CI/CD with Jenkins, the first step is to prepare your environment by installing Java. Jenkins requires Java to run, so you need to install OpenJDK 17. Update your system packages and install Java by running the following commands:

sudo apt update
sudo apt install fontconfig openjdk-17-jre
java -version

After installation, you can verify the version of Java to ensure everything is properly set up. The output should display the installed version, such as openJDK version 17. With Java in place, your system is now ready to host Jenkins.

Next, install Jenkins using its official package repository to ensure you're getting the latest stable release. First, add Jenkins' repository key and configuration to your system, update the package list, and install Jenkins by running these commands:

sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
  https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
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
sudo apt-get update
sudo apt-get install jenkins

Once Jenkins is installed, you need to enable and start the Jenkins service. This ensures Jenkins runs immediately and starts automatically whenever the system boots. Use the following commands to enable and start Jenkins:

sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins

After starting the service, you can access Jenkins through your browser. Open http://[VM_EXTERNAL_IP]:8080 and log in using the initial admin password. To retrieve this password, run:

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

Enter the password on the setup screen and follow the guided setup wizard. This process will include installing recommended plugins, such as Git, Pipeline, Blue Ocean, and GitHub, which are essential for building your CI/CD pipeline.

In addition to plugins, configure credentials for accessing your source code repository. Navigate to Manage Jenkins > Credentials, and add secure credentials for platforms like GitHub or GitLab. This setup ensures your pipeline can pull code from your repositories securely.

With Jenkins configured, you’re ready to create your first pipeline. The pipeline script can define all stages of your CI/CD process, such as fetching code, running tests, and deploying to production.

Setting Up Git Credentials for Private Repositories

If your project is hosted on a private Git platform, you’ll need to provide secure credentials for Jenkins to access the repository. For GitHub users, this involves creating a Personal Access Token (PAT) and adding it to Jenkins' credentials. This ensures seamless integration between Jenkins and your repository without compromising security.

Creating a Personal Access Token (PAT) on GitHub

To create a PAT in GitHub:

1. Go to your GitHub account settings.

2. Navigate to Developer settings > Personal Access Tokens > Tokens (classic).

3. Click Generate new token and specify the required permissions. For most CI/CD setups, select scopes like repo (for repository access) and workflow (if managing GitHub Actions).

4. Generate the token and copy it. Make sure to store it securely as you won’t be able to view it again.

Configuring Jenkins with GitHub Credentials

Once you’ve created your PAT, follow these steps to add it to Jenkins:

1. Log in to the Jenkins Dashboard.

2. Navigate to Manage Jenkins > Manage Credentials.

3. Under (global) credentials, click Add Credentials.

4. In the Kind dropdown, select Username with password.

Username: Enter your GitHub username (e.g., username).

Password: Paste the PAT you just created.

5. Give the credential a recognizable ID (e.g., github-creds) and click OK.

Writing Your First Jenkins Pipeline

With Jenkins configured, you’re ready to create your first pipeline. The pipeline script defines all stages of your CI/CD process, including fetching code, running tests, and deploying the application. Let’s walk through an example pipeline written using Jenkins' declarative syntax, which simplifies the process and ensures better readability.

Understanding the Pipeline Script

Below is an example pipeline that automates three key stages of the CI/CD process:

1. Fetching the code from GitHub using credentials for a secure connection.

2. Running basic testing commands to verify the environment setup.

3. Deploying the application to a server using SSH for remote commands

pipeline {
    agent any
    
    environment {
        GITHUB_CREDENTIALS = 'caea020d-a24e-4305-bdc2-d7e51d1c8171'  // ID of the GitHub credentials in Jenkins
    }
    
    stages {
        stage('Git Checkout') {
            steps {
                script {
                    // Cloning the GitHub repository using the provided credentials
                    git credentialsId: "${GITHUB_CREDENTIALS}", url: 'https://github.com/Barbarpotato/API-Registry.git', branch: 'main'
                }
            }
        }
        
        stage('Run Testing Commands') {
            steps {
                sh 'hostname'  // Outputs the hostname of the Jenkins agent
                sh 'pwd'       // Displays the current working directory in the agent's workspace
            }
        }
        
        stage('Deploy to Server') {
            steps {
                // Using SSH to deploy the application to the target server
                sshagent(['ssh-key-gateway']) {
                    sh '''
                    ssh -o StrictHostKeyChecking=no darmawanjr88@34.101.205.217 << EOF
                        cd API-Registry
                        git pull origin main
                        pm2 restart all
                        exit
                    EOF
                    '''
                }
            }
        }
    }
}

The provided pipeline script is a clear example of how Jenkins orchestrates the CI/CD process, broken into multiple stages for ease of management. Let's break it down section by section and explain how each part works in a simplified, interactive way.

Pipeline Declaration

At the heart of any Jenkins pipeline is the pipeline block, which defines the entire process. Inside this block, the agent any directive tells Jenkins to run the pipeline on any available agent, whether it's a master or a worker node. This flexibility is useful when you have multiple agents configured and don’t want to restrict the execution to a specific one.

Environment Variables

Next, we have the environment block. This is where we can define variables that are reused across the pipeline. In this case, the variable GITHUB_CREDENTIALS holds the ID of the GitHub credentials that Jenkins uses to securely access the private repository. By defining it here, you ensure it’s easily reusable without hardcoding it into every step. Think of it as a centralized way to manage sensitive data like tokens and credentials, making the pipeline both secure and maintainable.

Stages

The pipeline is broken into logical steps called "stages," each representing a part of the CI/CD workflow.

1. Git Checkout

In the first stage, Jenkins clones the GitHub repository using the git step. The credentialsId points to the pre-configured GitHub credentials stored in Jenkins. This ensures secure and seamless access to the private repository without exposing sensitive information. This step lays the foundation for the entire pipeline, as it fetches the code that the remaining stages will process.

2. Run Testing Commands

This stage is simple but powerful. It runs shell commands such as hostname and pwd, which output the hostname of the Jenkins agent and the current working directory, respectively. While these commands are placeholders here, you can replace them with actual test scripts. For instance, if you’re running unit tests, you could include a command like npm test or pytest. The purpose of this stage is to ensure the environment is configured correctly and ready for further operations.

3. Deploy to Server

This stage demonstrates how Jenkins can deploy your application to a production or staging server. Using the sshagent block, Jenkins securely connects to the target server via SSH. It then pulls the latest changes from the repository and restarts the application using PM2, a popular process manager for Node.js. This setup ensures that the application is always up-to-date with the latest code changes, and the restart command ensures a smooth rollout of updates.

Continuing from the previous explanation of setting up a Jenkins pipeline, the next step to truly automate the CI/CD process is to set up a push trigger using webhooks. This ensures that every time you push changes to your GitHub repository, Jenkins automatically triggers the pipeline, saving you from the hassle of manually starting the build.

Let’s explore how to set up a webhook-based trigger between GitHub and Jenkins in an intuitive and straightforward way.

What Are Webhooks?

Think of webhooks as a way for GitHub to "talk" to Jenkins. Whenever you push code to your repository, GitHub sends a signal (HTTP POST request) to Jenkins, telling it to start the pipeline. This creates an automated, real-time link between your code changes and the build process.

Setting Up Push Trigger (Webhook)

First, Enable GitHub Integration in Jenkins

Before setting up the webhook, you need to make sure Jenkins can communicate with GitHub. To do this, open Jenkins and go to Manage Jenkins > Manage Plugins. From there, search for GitHub plugins and install them. This will allow Jenkins to recognize GitHub as a source and receive notifications from it.

Configure Jenkins to Listen for Webhooks

Next, open the Jenkins pipeline job you want to configure. In the job settings, go to Build Triggers and enable the option GitHub hook trigger for GITScm polling. This step is important because it tells Jenkins to "listen" for any push events from GitHub, ready to trigger the pipeline whenever changes are detected.

Set Up a Webhook in GitHub

Now, head over to your GitHub repository. Inside the Settings tab, navigate to Webhooks and click on Add webhook. You'll need to provide Jenkins with a specific endpoint where it can receive notifications from GitHub. The URL format is as follows:

http://<JENKINS_URL>/github-webhook/

Replace JENKINS_URL with your Jenkins server’s address. Choose application/json as the content type, and make sure the Push events option is selected. This ensures that the webhook triggers every time you push changes.

Verify the Webhook

Once everything is set up, push a commit to your GitHub repository. Then, return to Jenkins and check if the pipeline starts running automatically. If the job kicks off, the webhook is working properly.

How It All Comes Together

When you push changes to your repository, GitHub sends a webhook to Jenkins. Jenkins then triggers the pipeline to fetch the latest code, run tests, and deploy to your server. This creates a seamless CI/CD process, where every change is automatically tested and deployed.

Conclusion

In this Labs, we’ve taken a deep dive into how Jenkins can supercharge your CI/CD workflows, making it an essential tool for automating your development lifecycle. We started by setting up Jenkins, installing necessary dependencies like Java, and getting the Jenkins service up and running.

From there, we explored how to configure Jenkins to work with your GitHub repository, including managing credentials securely. With Jenkins set up and connected to GitHub, we moved on to creating a simple declarative pipeline, allowing Jenkins to automatically fetch the latest code, run tests, and deploy to your server.

We then enhanced the process by explaining how to set up push triggers using GitHub webhooks. With webhooks in place, Jenkins is able to automatically start the pipeline whenever new code is pushed to the repository, eliminating the need for manual intervention and ensuring continuous integration.

Through these steps, we’ve created a fully automated CI/CD pipeline that reacts to changes in your code, testing it and deploying it seamlessly. This not only saves time but also minimizes errors, providing faster and more reliable software delivery.

By mastering Jenkins, you’re empowering yourself to automate complex workflows, improve collaboration, and focus on building great software without worrying about the manual process of integration and deployment.