How to Set Up a CI/CD Pipeline Using Jenkins

How to Set Up a CI/CD Pipeline Using Jenkins

Β·

3 min read

Setting up a CI/CD (Continuous Integration/Continuous Delivery) pipeline using Jenkins can transform how you develop and deploy software. It helps automate repetitive tasks, improves code quality, and reduces deployment time. In this guide, we'll walk you through the steps to set up a Jenkins CI/CD pipeline, using simple language and plenty of emojis to make things fun! πŸŽ‰


What is a CI/CD Pipeline? πŸ€”

A CI/CD pipeline is a series of automated steps that ensure your code is:

  1. Built πŸ—οΈ

  2. Tested βœ…

  3. Deployed πŸš€

It helps developers ship reliable and high-quality software quickly.


What is Jenkins?

Jenkins is an open-source automation server that makes building, testing, and deploying your projects simple. Think of it as your personal assistant for automating DevOps tasks. πŸ€–


Step 1: Install Jenkins πŸ–₯️

  1. Download Jenkins:
    Go to the Jenkins website and download the latest version.

  2. Install Jenkins:

    • For Linux:

        sudo apt update
        sudo apt install openjdk-11-jdk
        wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
        sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
        sudo apt update
        sudo apt install jenkins
      
    • For Windows/Mac: Use the installer provided on the Jenkins website.

  3. Start Jenkins:
    Access Jenkins at http://localhost:8080.


Step 2: Configure Jenkins βš™οΈ

  1. Unlock Jenkins:

    • Copy the password from the file shown during the installation process.

    • Paste it into the browser.

  2. Install Plugins:
    Choose "Install suggested plugins" to get started quickly.

  3. Set Up Admin User:
    Create an admin account to manage Jenkins.


Step 3: Create a New Pipeline πŸ› οΈ

  1. Install Git Plugin:
    Go to Manage Jenkins β†’ Plugins and install the Git plugin.

  2. Create a Job:

    • Go to Dashboard β†’ New Item.

    • Enter a name for your pipeline and select Pipeline.

  3. Configure Pipeline:

    • In the Pipeline section, choose Pipeline script from SCM.

    • Add the Git repository URL where your code resides.


Step 4: Write a Jenkinsfile πŸ“

The Jenkinsfile defines the stages of your pipeline. Here’s a basic example:

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                echo 'Building the application...'
                sh './build.sh'
            }
        }

        stage('Test') {
            steps {
                echo 'Running tests...'
                sh './test.sh'
            }
        }

        stage('Deploy') {
            steps {
                echo 'Deploying the application...'
                sh './deploy.sh'
            }
        }
    }
}

Step 5: Trigger the Pipeline πŸš€

  1. Set Up Webhooks (Optional):
    Connect Jenkins to your Git repository to trigger the pipeline automatically on code changes.

  2. Run the Pipeline:

    • Go to your job in Jenkins.

    • Click Build Now.

  3. Monitor Progress:

    • Check the console output for real-time logs.

    • Fix any errors that may occur.


Step 6: Success! πŸŽ‰

Congratulations, you’ve set up a CI/CD pipeline using Jenkins! Now, every time you push code, Jenkins will:

  • Build your application πŸ—οΈ

  • Test it βœ…

  • Deploy it πŸš€


Real-Life Example

Imagine you’re working on an e-commerce app. Whenever a developer pushes new features to GitHub:

  1. Jenkins builds the app to check for issues.

  2. It runs automated tests to ensure nothing is broken.

  3. If everything passes, Jenkins deploys the app to your staging or production environment.

This ensures faster development cycles and higher-quality releases. πŸ’―


Benefits of Using Jenkins CI/CD Pipeline

  • Automation: No more manual deployments. πŸŽ›οΈ

  • Speed: Faster time-to-market. πŸ•’

  • Reliability: Fewer errors, better code quality. βœ…

  • Scalability: Jenkins works for small teams and large enterprises alike. 🌐


Do you use Jenkins or any other CI/CD tool? Let me know in the comments!

Β