DEV Community

Cover image for 15 ways to use Jenkins for Continuous Integration (CI) with examples
Kshitij
Kshitij

Posted on

15 ways to use Jenkins for Continuous Integration (CI) with examples

1) Build Automation

What is it?

• Automating the process of compiling code to produce software (e.g., creating .exe files for Windows or .jar files for Java).
• Jenkins automatically performs this every time you make changes to your code.
Enter fullscreen mode Exit fullscreen mode

Example:

• Imagine you’re writing a Java program. Normally, you would run javac and java commands manually. Jenkins automates this by:
1.  Fetching your code from GitHub.
2.  Running a tool like Maven to compile it.
3.  Producing a ready-to-use software file.
Enter fullscreen mode Exit fullscreen mode

Scenario: Java Application Build

• Objective: Automatically compile and build a Java application whenever code is pushed.
• Setup:
1.  Install the Git plugin to pull the code.
2.  Use Maven or Gradle as a build tool.
• Pipeline Example:

    •.      pipeline {
agent any
stages {
    stage('Checkout') {
        steps {
            git branch: 'main', url: 'https://github.com/your-repo.git'
        }
    }
    stage('Build') {
        steps {
            sh 'mvn clean package'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}

• Outcome: Produces a JAR/WAR file for deployment.
Enter fullscreen mode Exit fullscreen mode

2) Automated Testing

What is it?

• Ensures your program works as intended by automatically running tests.
• These tests check if your program behaves correctly for different inputs.
Enter fullscreen mode Exit fullscreen mode

Beginner-Friendly Example:

• Suppose your program calculates discounts:
• Input: price = 100, discount = 10%
• Expected Output: 90
• Jenkins runs a script to check if your program calculates this correctly every time you make changes.
Enter fullscreen mode Exit fullscreen mode

Scenario: Running Unit Tests with JUnit

• Objective: Run tests automatically after every build.
• Setup:
1.  Add test execution and result publishing to your pipeline.
• Pipeline Example:

    •.      pipeline {
agent any
stages {
    stage('Build') {
        steps {
            sh 'mvn clean install'
        }
    }
    stage('Test') {
        steps {
            sh 'mvn test'
            junit '**/target/surefire-reports/*.xml'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}

• Outcome: Tests are run, and results are published in Jenkins.
Enter fullscreen mode Exit fullscreen mode

3) Code Quality Analysis

What is it?

• Jenkins uses tools like SonarQube to check your code for mistakes, bad practices, or inefficiencies.
Enter fullscreen mode Exit fullscreen mode

Beginner-Friendly Example:

• Imagine your code is full of unnecessary lines or errors. Jenkins will highlight:
• Unused variables.
• Functions that could be written better.
• This ensures you write clean, efficient code.
Enter fullscreen mode Exit fullscreen mode

Scenario: SonarQube Integration

• Objective: Perform static code analysis to ensure quality.
• Setup:
1.  Install the SonarQube Scanner plugin.
2.  Add a SonarQube analysis stage to your pipeline.
• Pipeline Example:

    •.      pipeline {
agent any
stages {
    stage('Code Analysis') {
        steps {
            withSonarQubeEnv('SonarQube') {
                sh 'mvn sonar:sonar'
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}

• Outcome: Reports code quality metrics in SonarQube.
Enter fullscreen mode Exit fullscreen mode

4) Artifact Management

What is it?

• Stores files created after building your program (called artifacts) so they can be used later.
Enter fullscreen mode Exit fullscreen mode

Beginner-Friendly Example:

• After Jenkins creates a .jar file (Java app), it uploads it to a storage service like Nexus. This makes it easy for others to download and use your program.
Enter fullscreen mode Exit fullscreen mode

Scenario: Uploading to Nexus

• Objective: Store build artifacts for future use.
• Setup:
1.  Use the Nexus Artifact Uploader plugin.
• Pipeline Example:

    •.      pipeline {
agent any
stages {
    stage('Build') {
        steps {
            sh 'mvn clean package'
        }
    }
    stage('Upload Artifact') {
        steps {
            nexusArtifactUploader(
                nexusVersion: 'nexus3',
                nexusUrl: 'http://nexus.example.com',
                repository: 'maven-releases',
                credentialsId: 'nexus-credentials',
                artifacts: [[
                    artifactId: 'app',
                    file: 'target/app.jar',
                    type: 'jar'
                ]]
            )
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}

• Outcome: Artifacts are stored in Nexus.
Enter fullscreen mode Exit fullscreen mode

5) Dockerized Pipelines

What is it?

• Runs the Jenkins job inside a lightweight, isolated container (like a mini-computer) to ensure consistency.
Enter fullscreen mode Exit fullscreen mode

Beginner-Friendly Example:

• If Jenkins runs your Java program on Windows, but your teammate uses Linux, the program may behave differently. Using Docker ensures it runs the same everywhere.
Enter fullscreen mode Exit fullscreen mode

Scenario: Build in a Docker Container

• Objective: Isolate build environments.
• Setup: Use the Docker Pipeline plugin.
• Pipeline Example:

    •.      pipeline {
agent {
    docker {
        image 'maven:3.8.1-jdk-11'
    }
}
stages {
    stage('Build') {
        steps {
            sh 'mvn clean install'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}

• Outcome: Builds run in Docker containers.
Enter fullscreen mode Exit fullscreen mode

6) Multi-Branch Pipelines

What is it?

• Automatically creates separate pipelines for each Git branch. Each branch can have its own rules.
Enter fullscreen mode Exit fullscreen mode

Beginner-Friendly Example:

• You might have two branches: main (stable) and dev (work-in-progress). Jenkins ensures code in dev doesn’t break the stable code in main.
Enter fullscreen mode Exit fullscreen mode

Scenario: Branch-Specific Pipelines

• Objective: Automate pipelines for multiple branches.
• Setup: Use Multibranch Pipeline jobs.
• Pipeline Code: Same logic is applied per branch.
Enter fullscreen mode Exit fullscreen mode

7) Notification and Reporting

What is it?

• Sends updates about your build (success or failure) to your team via email, Slack, etc.
Enter fullscreen mode Exit fullscreen mode

Beginner-Friendly Example:

• If the build fails, Jenkins can send you a message:
Enter fullscreen mode Exit fullscreen mode

“The build failed due to a missing file. Please fix it.”

Scenario: Slack Notifications

• Objective: Notify the team of build results.
• Setup:
1.  Install the Slack Notification plugin.
2.  Configure Slack webhooks.
• Pipeline Example:

    •.      pipeline {
agent any
stages {
    stage('Build') {
        steps {
            sh 'mvn clean install'
        }
    }
}
post {
    success {
        slackSend(channel: '#dev', message: 'Build succeeded!')
    }
    failure {
        slackSend(channel: '#dev', message: 'Build failed!')
    }
}
Enter fullscreen mode Exit fullscreen mode

}

• Outcome: Teams are notified on Slack.
Enter fullscreen mode Exit fullscreen mode

8) Continuous Delivery (CD)

What is it?

• Jenkins automatically deploys your application after building and testing it successfully.
Enter fullscreen mode Exit fullscreen mode

Beginner-Friendly Example:

• Your program is uploaded to a Kubernetes cluster or server where users can access it instantly.
Enter fullscreen mode Exit fullscreen mode

Scenario: Kubernetes Deployment

• Objective: Deploy applications to Kubernetes after successful builds.
• Pipeline Example:

    •.      pipeline {
agent any
stages {
    stage('Deploy') {
        steps {
            sh 'kubectl apply -f deployment.yaml'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}

9) Infrastructure as Code Validation

What is it?

• Validates the scripts used to set up servers (e.g., Terraform files).
Enter fullscreen mode Exit fullscreen mode

Beginner-Friendly Example:

• Before creating a cloud server, Jenkins checks if your configuration file is correct to prevent errors during server setup.
Enter fullscreen mode Exit fullscreen mode

Scenario: Terraform Validation

• Objective: Validate Terraform configurations.
• Pipeline Example:
Enter fullscreen mode Exit fullscreen mode

pipeline {
agent any
stages {
stage('Validate Terraform') {
steps {
sh 'terraform validate'
}
}
}
}

10) Database Migration Automation

What is it?

• Updates your database (e.g., adding a new column) automatically when you update your application.
Enter fullscreen mode Exit fullscreen mode

Beginner-Friendly Example:

• If you add a new feature that needs an extra database column, Jenkins uses tools like Flyway to add the column without breaking the existing database.
Enter fullscreen mode Exit fullscreen mode

Scenario: Using Flyway

• Objective: Automate database schema updates.
• Pipeline Example:

    •.      pipeline {
agent any
stages {
    stage('Database Migration') {
        steps {
            sh 'flyway migrate'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}

11) Blue/Green Deployment

What is it?

• Deploys your new application version to a small group of users while the rest continue using the old version.
Enter fullscreen mode Exit fullscreen mode

Beginner-Friendly Example:

• Imagine you’re updating a website. Only 10% of users see the new version. If it works fine, you roll it out to everyone.
Enter fullscreen mode Exit fullscreen mode

Scenario: AWS Deployment

• Objective: Safely deploy without downtime.
• Pipeline Example:

    •.      pipeline {
agent any
stages {
    stage('Deploy to Blue') {
        steps {
            sh 'aws deploy ...'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}

12) Security Scans

What is it?

• Jenkins checks your application for security vulnerabilities using tools like OWASP ZAP.
Enter fullscreen mode Exit fullscreen mode

Beginner-Friendly Example:

• Jenkins might scan your website and warn:
Enter fullscreen mode Exit fullscreen mode

“Your website allows users to enter malicious scripts. Fix this!”

Scenario: OWASP ZAP Integration

• Objective: Check for vulnerabilities.
• Pipeline Example:

    •.      pipeline {
agent any
stages {
    stage('Security Scan') {
        steps {
            sh 'zap-cli start'
            sh 'zap-cli scan http://app'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}

13) Cross-Browser Testing

What is it?

• Tests your web app on different browsers (e.g., Chrome, Firefox) to ensure it works everywhere.
Enter fullscreen mode Exit fullscreen mode

Beginner-Friendly Example:

• If your website looks good on Chrome but breaks on Firefox, Jenkins catches this issue by running tests on all browsers.
Enter fullscreen mode Exit fullscreen mode

Scenario: Selenium Testing

• Objective: Test web apps across browsers.
• Pipeline Example:

    •.      pipeline {
agent any
stages {
    stage('Cross-Browser Tests') {
        steps {
            sh 'selenium-side-runner test.side'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}

14) Cross-Platform Builds

What is it?

• Builds your program for multiple operating systems (Windows, Mac, Linux) in one go.
Enter fullscreen mode Exit fullscreen mode

Beginner-Friendly Example:

• You’re creating a game. Jenkins creates .exe (Windows), .dmg (Mac), and .appimage (Linux) files simultaneously.
Enter fullscreen mode Exit fullscreen mode

Scenario: Compile for Linux, Windows, and Mac

• Objective: Build for multiple OS environments.
• Pipeline Example:

    •.      pipeline {
agent any
stages {
    stage('Build Linux') {
        steps {
            sh './build-linux.sh'
        }
    }
    stage('Build Windows') {
        steps {
            bat 'build-windows.bat'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}

15) CI for Machine Learning

What is it?

• Automates tasks like validating datasets and training models for machine learning projects.
Enter fullscreen mode Exit fullscreen mode

Beginner-Friendly Example:

• If you’re building an AI model, Jenkins:
1.  Checks if the input data is valid.
2.  Automatically trains the model using this data.
Enter fullscreen mode Exit fullscreen mode

Scenario: Data Validation and Model Training

• Objective: Automate ML pipelines.
• Pipeline Example:

    •    pipeline {
agent any
stages {
    stage('Data Validation') {
        steps {
            sh 'python validate_data.py'
        }
    }
    stage('Train Model') {
        steps {
            sh 'python train_model.py'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}.

Summary for Beginners:

• Think of Jenkins as your assistant.
Enter fullscreen mode Exit fullscreen mode
  • It automates repetitive tasks like building, testing, and deploying software, so you can focus on coding!

Top comments (0)