Ad Code

✨🎆 Diwali Dhamaka Offer! 🎆✨

Get 20% OFF on All Courses at Shiva Concept Solution click

What is Jenkins? How to create pipeline using jenkins

 

What is Jenkins? (Simple Explanation for Students)

🔧 Jenkins is a free tool used by developers to automate tasks like:

  • Building code

  • Testing code

  • Deploying applications

💡 Think of Jenkins as a Robot Assistant:

Whenever you write code and push it to GitHub, Jenkins can:

  • Automatically test if the code works

  • Automatically build the app

  • Automatically deploy it to a server

🔁 This process is called CI/CD:

  • CI – Continuous Integration (build + test automatically)

  • CD – Continuous Deployment (deploy automatically)


🚀 Jenkins Installation: Key Steps and Important Options

🔷 Windows Installation Steps:

  1. Install Java First (JDK)
    Jenkins needs Java to run.

    • Download JDK from Oracle or OpenJDK

    • Set JAVA_HOME environment variable

    • Add Java to system PATH

  2. Download Jenkins

  3. Run Installer

    • Double-click and follow steps

    • It will install Jenkins as a Windows Service

  4. Initial Setup

    • Go to http://localhost:8080

    • Unlock Jenkins using the password in the file shown on screen

      • Path: C:\Program Files\Jenkins\secrets\initialAdminPassword

    • Install Suggested Plugins (Recommended)

    • Create Admin User

  5. Ready to Use!


🔷 Linux Installation Steps (Ubuntu/Debian Example):

  1. Install Java


    sudo apt update sudo apt install openjdk-17-jdk -y
  2. Add Jenkins Repository

    bash

    curl -fsSL 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
  3. Install Jenkins


    sudo apt update sudo apt install jenkins -y
  4. Start Jenkins


    sudo systemctl start jenkins sudo systemctl enable jenkins
  5. Open in Browser
    Visit http://your-server-ip:8080

    • Unlock Jenkins with password:


      sudo cat /var/lib/jenkins/secrets/initialAdminPassword
    • Install Suggested Plugins

    • Create Admin User


⭐ Most Important Options in Jenkins (For Beginners):

FeatureWhat It Does (In Simple Words)
Freestyle ProjectManually configure tasks like build/test/deploy
PipelineWrite scripts to automate build + deploy
Build TriggersRun job automatically (e.g. when code is pushed)
Source Code Management (SCM)Connect with GitHub, GitLab, Bitbucket
Post-build ActionsSend emails, deploy code, etc. after build
PluginsAdd extra features (GitHub, Docker, Slack, etc.)

Task1 create free style project to print hello world under windows machine

Step1 : Open Jenkins

Step2:   NEW ITEM

Step3    Click on Free Style Project

Step4:  Write Description if required

Step5:  Direct go into build steps

Step6:  Select Windows Batch Command if using Windows or Select Linux command

Step7: Write command  echo HelloWorld

step8: Save

Step9: Click on Build if green then pass of red then fail

Step 10: show console output


Task2:  Create Java Program and push into github using git , compile and execute this code under Jenkins using Free Style Project


Note:  Java Environment Path Must be Set in your PC check from command prompt to write java --version

Step1 : Open Jenkins

Step2:   NEW ITEM

Step3    Click on Free Style Project

Step4:  Write Description if required

Step5: Go into settings --> Manage Jenkins ---> Tools---> Configure Tools--> Set git path

Name: Default
Path    C:\Program Files\Git\bin\git.exe

Step6: Again come on Free Style Project

Step7:  go into source code management and set git public repository path

Step8 Come into Build Step Option and write following command

          echo Java Program compilation
         javac HelloWorld.java
         echo Java Program Execution
        java HelloWorld

step9: Click Build now

step 10: Click on Build and Check Console Output and show Java Code Result


Task3:  Create Python Program and push into github using git ,  execute this code under Jenkins using Free Style Project


Note:  Python Environment Path Must be Set in  System Path and Full path like this (C:\Users\Shiva-PC\AppData\Local\Programs\Python\Python313\) your PC check from command prompt to write python --version

Step1 : Open Jenkins

Step2:   NEW ITEM

Step3    Click on Free Style Project

Step4:  Write Description if required

Step5: Go into settings --> Manage Jenkins ---> Tools---> Configure Tools--> Set git path if it is not 

Name: Default
Path    C:\Program Files\Git\bin\git.exe

Step6: Again come on Free Style Project

Step7:  go into source code management and set git public repository path where python code exist

Step8 Come into Build Step Option and write following command

         python filename

step9: Click Build now

step 10: Click on Build and Check Console Output and show Java Code Result



Task4:  Build Execute by Github Webhooks


step1st:  Configure build and select Triggers GitHub hook trigger for GITScm polling
?
step2nd: go into github setting ---> Webhooks Option ---->  and try http://localhost:8080/webhooks

step3: it provide error localhost path not supported

step4:  install ngrok to map localpath to remote path

step5th install and dowload ngrok

1. Install ngrok

Download from: https://ngrok.com/download

Extract and place ngrok.exe in a known location (e.g., C:\ngrok\ngrok.exe)

2. Run ngrok to Expose Jenkins

Open Command Prompt and run:


ngrok http 8080

You’ll see output like:


Forwarding http://abc123.ngrok.io -> http://localhost:8080 Forwarding https://abc123.ngrok.io -> http://localhost:8080

✅ Copy the HTTPS URL (e.g., https://abc123.ngrok.io)

3) Manage ngrok authentication with login and authenticate token


step6:  replace ngrok url under payload url like this https://d3d61da557f0.ngrok-free.app/github-webhook/


4) choose webhooks triggers 

i choose commit and push both

5) build automatically created when you push or commit in jenkins




Task5:  Build Java Project using Maven from github (Free Style Project)


Before this task first study about maven Maven Tutorials

1)  Upload Maven Project Under github

2)  Create Free Style Project

3)  add github repsoitory and set branch

4) set windows batch command

cd MyFirstMavenProject
mvn clean package
cd target
java -cp MyFirstMavenProject-1.0-SNAPSHOT.jar com.example.App


for execution 

run this 

cd target
java -cp MyFirstMavenProject-1.0-SNAPSHOT.jar com.test.App


📌 What is a Pipeline in Jenkins?

A Jenkins Pipeline is a way to define the steps of your build process as code (written in Groovy).
Instead of clicking around in the Jenkins UI to configure a job, you put all your build steps inside a file called Jenkinsfile.

This gives you:

  • Automation (every build runs exactly the same way)

  • Version control (the pipeline code lives with your project)

  • Flexibility (build, test, deploy in sequence)


🛠 Simple Jenkins Pipeline Example

groovy

pipeline { agent any stages { stage('Hello') { steps { echo 'Hello from Jenkins Pipeline!' } } } }

 Another Jenkins Pipeline Example

pipeline { agent any stages { stage('BUILD') { steps { echo 'Hello This is for Build' } } stage('TEST') { steps { echo 'Hello This is for Test!' } } stage('DEPLOY') { steps { echo 'Hello This is Deploy !' } } } }

Task6:  Build Java Project using Maven from github (Pipeline Style Project)

1)  create project using maven
2)  push into github repository
3) create jenkinspipelinebuild
4) write this pipleine script

pipeline {
    agent any

    tools {
        maven 'Maven_3.9.6'  // name from Global Tool Config
        jdk 'JDK17'          // name from Global Tool Config
    }

    stages {
        stage('Checkout') {
            steps {
                git branch: 'main', url: 'https://github.com/shivaconceptsolution/MyFirstMavenProject.git'
            }
        }

        stage('Build') {
            steps {
                bat 'mvn clean package'
            }
        }

        stage('Run Application') {
            steps {
                bat 'java -cp target/MyFirstMavenProject-1.0-SNAPSHOT.jar com.test.App'
            }
        }
    }
}
Create complete pipeline to clone code from github, create separate stage to compile , execute java code , execute python code.


pipeline {
    agent any

    stages {
        stage('Clone') {
            steps {
                echo 'Cloning repository...'
                git branch: 'main', url: 'https://github.com/shivaconceptsolution/javasample.git'
            }
        }

        stage('Build') {
            steps {
                echo 'Compiling Java source files...'
                // Use bat for Windows commands
                bat 'javac Hello.java'
            }
        }

        stage('Execute') {
            steps {
                echo 'Running Java program...'
                bat 'java Hello'
            }
        }

        stage('Python Execute') {
            steps {
                echo 'Execute Python Code'
                bat 'python Hello.py'
            }
        }

        stage('Deploy') {
            steps {
                echo 'Hello from Jenkins Pipeline Deployment!'
            }
        }
    }

    post {
        always {
            echo 'Pipeline execution completed.'
        }
    }
}


إرسال تعليق

0 تعليقات