Mastering Jenkins: A Step-by-Step Guide to CI/CD Implementation

Jenkins is a powerful open-source automation tool designed for continuous integration (CI) and continuous deployment (CD). This guide will walk you through the process of installing, configuring, and setting up a robust CI/CD pipeline with Jenkins.

Step 1: Installing Jenkins

Installing Jenkins on Ubuntu :-

sudo apt update
sudo apt install openjdk-17-jdk -y
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io-2023.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 binary/" | sudo tee \
 /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt update
sudo apt install jenkins -y

Starting the Jenkins Service :-

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

Accessing the Jenkins Web Interface :-
1. Open your browser and navigate to:

http://<IP_ADDRESS>:8080

2. Retrieve the initial administrator password:

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

Step 2: Configuring Jenkins
1. Enter the initial administrator password.
2. Select Install Suggested Plugins.
3. Create an administrator account and complete the setup.

Step 3: Installing the Pipeline Plugin
1. Navigate to Manage Jenkins → Manage Plugins.
2. Search for the Pipeline plugin and install it.

Step 4: Configuring a Pipeline Project
1. In the Jenkins dashboard, select New Item.
2. Choose Pipeline, provide a project name, and click OK.
3. Under the Pipeline section:
* Select Pipeline script from SCM.
* Choose Git and provide the repository URL.
4. Generate SSH keys for secure repository access:

ssh-keygen -t rsa -b 4096
cat ~/.ssh/id_rsa.pub

5. Add the generated key to your repository’s Deploy Keys settings.

Step 5: Writing the Jenkins Pipeline (Jenkinsfile)
A Jenkinsfile is used to define automation workflows for the CI/CD pipeline.

Sample Jenkinsfile

pipeline {
    agent any

    environment {
        DOCKER_IMAGE = 'myapp:latest'
        REGISTRY = 'myregistry.com'
    }

    stages {
        stage('Checkout Code') {
            steps {
                git branch: 'main', url: 'git@github.com:user/repo.git'
            }
        }

        stage('Build') {
            steps {
                sh 'docker build -t $DOCKER_IMAGE .'
            }
        }

        stage('Test') {
            steps {
                sh 'pytest tests/'
            }
        }

        stage('Push Image') {
            steps {
                withDockerRegistry([credentialsId: 'dockerhub-cred', url: "https://$REGISTRY"]) {
                    sh 'docker push $REGISTRY/$DOCKER_IMAGE'
                }
            }
        }

        stage('Deploy') {
            steps {
                sshagent(['deploy-key']) {
                    sh '''
                    ssh user@server "docker pull $REGISTRY/$DOCKER_IMAGE && \
                    docker stop myapp || true && \
                    docker rm myapp || true && \
                    docker run -d --name myapp -p 80:80 $REGISTRY/$DOCKER_IMAGE"
                    '''
                }
            }
        }
    }

    post {
        success {
            echo 'Deployment Successful'
        }
        failure {
            echo 'Deployment Failed'
        }
    }
}

Step 6: Running the Jenkins Pipeline
1. Open the Jenkins Dashboard.
2. Select your project and click Build Now.
3. Track progress in the Pipeline Stages view.

With this setup, you have successfully built a Jenkins-driven CI/CD pipeline that automates your software deployment process. 🚀

Conclusion :-

Jenkins is a powerful and flexible automation tool that simplifies CI/CD workflows, enabling teams to build, test, and deploy applications efficiently. By following this guide, you have successfully installed Jenkins, configured it, and set up a complete CI/CD pipeline, from code checkout to deployment. With its extensive plugin ecosystem and seamless integration with various DevOps tools, Jenkins empowers developers to streamline their software delivery process.

0
Subscribe to my newsletter

Read articles from Mahira Technology Private Limited directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mahira Technology Private Limited
Mahira Technology Private Limited

A leading tech consulting firm specializing in innovative solutions. Experts in cloud, DevOps, automation, data analytics & more. Trusted technology partner.