15 ways to use Jenkins for Continuous Integration (CI) with examples
Kshitij
Posted on November 23, 2024
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.
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.
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'
}
}
}
}
• Outcome: Produces a JAR/WAR file for deployment.
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.
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.
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'
}
}
}
}
• Outcome: Tests are run, and results are published in Jenkins.
3) Code Quality Analysis
What is it?
• Jenkins uses tools like SonarQube to check your code for mistakes, bad practices, or inefficiencies.
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.
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'
}
}
}
}
}
• Outcome: Reports code quality metrics in SonarQube.
4) Artifact Management
What is it?
• Stores files created after building your program (called artifacts) so they can be used later.
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.
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'
]]
)
}
}
}
}
• Outcome: Artifacts are stored in Nexus.
5) Dockerized Pipelines
What is it?
• Runs the Jenkins job inside a lightweight, isolated container (like a mini-computer) to ensure consistency.
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.
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'
}
}
}
}
• Outcome: Builds run in Docker containers.
6) Multi-Branch Pipelines
What is it?
• Automatically creates separate pipelines for each Git branch. Each branch can have its own rules.
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.
Scenario: Branch-Specific Pipelines
• Objective: Automate pipelines for multiple branches.
• Setup: Use Multibranch Pipeline jobs.
• Pipeline Code: Same logic is applied per branch.
7) Notification and Reporting
What is it?
• Sends updates about your build (success or failure) to your team via email, Slack, etc.
Beginner-Friendly Example:
• If the build fails, Jenkins can send you a message:
“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!')
}
}
}
• Outcome: Teams are notified on Slack.
8) Continuous Delivery (CD)
What is it?
• Jenkins automatically deploys your application after building and testing it successfully.
Beginner-Friendly Example:
• Your program is uploaded to a Kubernetes cluster or server where users can access it instantly.
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'
}
}
}
}
9) Infrastructure as Code Validation
What is it?
• Validates the scripts used to set up servers (e.g., Terraform files).
Beginner-Friendly Example:
• Before creating a cloud server, Jenkins checks if your configuration file is correct to prevent errors during server setup.
Scenario: Terraform Validation
• Objective: Validate Terraform configurations.
• Pipeline Example:
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.
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.
Scenario: Using Flyway
• Objective: Automate database schema updates.
• Pipeline Example:
•. pipeline {
agent any
stages {
stage('Database Migration') {
steps {
sh 'flyway migrate'
}
}
}
}
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.
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.
Scenario: AWS Deployment
• Objective: Safely deploy without downtime.
• Pipeline Example:
•. pipeline {
agent any
stages {
stage('Deploy to Blue') {
steps {
sh 'aws deploy ...'
}
}
}
}
12) Security Scans
What is it?
• Jenkins checks your application for security vulnerabilities using tools like OWASP ZAP.
Beginner-Friendly Example:
• Jenkins might scan your website and warn:
“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'
}
}
}
}
13) Cross-Browser Testing
What is it?
• Tests your web app on different browsers (e.g., Chrome, Firefox) to ensure it works everywhere.
Beginner-Friendly Example:
• If your website looks good on Chrome but breaks on Firefox, Jenkins catches this issue by running tests on all browsers.
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'
}
}
}
}
14) Cross-Platform Builds
What is it?
• Builds your program for multiple operating systems (Windows, Mac, Linux) in one go.
Beginner-Friendly Example:
• You’re creating a game. Jenkins creates .exe (Windows), .dmg (Mac), and .appimage (Linux) files simultaneously.
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'
}
}
}
}
15) CI for Machine Learning
What is it?
• Automates tasks like validating datasets and training models for machine learning projects.
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.
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'
}
}
}
}.
Summary for Beginners:
• Think of Jenkins as your assistant.
- It automates repetitive tasks like building, testing, and deploying software, so you can focus on coding!
Posted on November 23, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.