Project: Basic CI Pipeline with Jenkins

Objective

To set up a Continuous Integration (CI) pipeline using Jenkins that will:

1. Prerequisites

2. Setting Up Jenkins

1.Install Jenkins (on AWS EC2 / local machine)

On Ubuntu:

sudo apt update
sudo apt install openjdk-11-jdk -y
wget -q -O - https://pkg.jenkins.io/debian-stable/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 -y
sudo systemctl start jenkins

2.Access Jenkins UI

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

Create a New Pipeline Job

3. Jenkins Pipeline Script

Go to "Pipeline" section → Choose "Pipeline Script" and enter the following code:

For a Java Application (Maven-based project)

pipeline {
    agent any

    environment {
        JAVA_HOME = "/usr/lib/jvm/java-11-openjdk-amd64"
        PATH = "$JAVA_HOME/bin:$PATH"
    }

    stages {
        stage('Clone Repository') {
            steps {
                git branch: 'main', url: 'https://github.com/your-username/your-java-repo.git'
            }
        }

        stage('Build Application') {
            steps {
                sh 'mvn clean package'
            }
        }

        stage('Run Tests') {
            steps {
                sh 'mvn test'
            }
        }

        stage('Archive Artifacts') {
            steps {
                archiveArtifacts artifacts: 'target/*.jar', fingerprint: true
            }
        }
    }

    post {
        success {
            echo 'Build was successful!'
        }
        failure {
            echo 'Build failed! Check the logs.'
        }
    }
}

For a Python Application

pipeline {
    agent any

    environment {
        PYTHON = "/usr/bin/python3"
    }

    stages {
        stage('Clone Repository') {
            steps {
                git branch: 'main', url: 'https://github.com/your-username/your-python-repo.git'
            }
        }

        stage('Setup Environment') {
            steps {
                sh 'python3 -m venv venv'
                sh 'source venv/bin/activate && pip install -r requirements.txt'
            }
        }

        stage('Run Tests') {
            steps {
                sh 'source venv/bin/activate && pytest tests/'
            }
        }

        stage('Linting') {
            steps {
                sh 'source venv/bin/activate && flake8 .'
            }
        }
    }

    post {
        success {
            echo 'Build was successful!'
        }
        failure {
            echo 'Build failed! Check the logs.'
        }
    }
}

4. Run & Monitor Pipeline

  1. Save the pipeline and click "Build Now".
  2. Check logs in "Console Output".
  3. If successful, you will see:

5. Enhancements (Optional)

Conclusion

This basic CI pipeline automates the build process for a Java or Python application. It clones the repository, builds, tests, and archives artifacts while providing logs for debugging.