Friday, May 16, 2025

Jenkins part5

 Jenkins part5

Class 25th Jenkins May 16th

Variable setup -pipeline

Build and deploy a simple java Project

                                                                  Practical

Prior you need install below softwares

[ec2-user@ip-10-0-1-158 ~]$ sudo -i

[root@ip-10-0-1-158 ~]# yum install -y java-17-amazon-corretto-devel git

[root@ip-10-0-1-158 ~]# sudo wget -O /etc/yum.repos.d/jenkins.repo \     https://pkg.jenkins.io/redhat-stable/jenkins.repo

[root@ip-10-0-1-158 ~]# sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
[root@ip-10-0-1-158 ~]# sudo yum install jenkins
[root@ip-10-0-1-158 ~]# systemctl start jenkins


Step1: Create Pipeline job in Jenkins >ccitpipelinejob

for build the java project required maven tool,just add below save.

Step2: Pipeline script given and execute

pipeline {
    agent any
   tools
   {
       maven "ccit-mvn"
   }
    stages {
        stage('git') {
            steps {
                git branch: 'main', url: 'https://github.com/Vakatisubbu/ccit-javaapp.git'
            }
        }
           stage('build') {
            steps {
                sh "mvn clean package"
            }
        }
    }
}
Step3:After build .jar file created in the target server 
[root@ip-10-0-1-158 target]#  pwd
/var/lib/jenkins/workspace/ccitpipelinejob/target
[root@ip-10-0-1-158 target]# ls -lrt
drwxr-xr-x 3 jenkins jenkins       25 May 26 09:30 generated-sources
drwxr-xr-x 3 jenkins jenkins       35 May 26 09:30 maven-status
drwxr-xr-x 4 jenkins jenkins       34 May 26 09:30 classes
drwxr-xr-x 2 jenkins jenkins       28 May 26 09:30 maven-archiver
-rw-r--r-- 1 jenkins jenkins     3691 May 26 09:30 ccit-0.0.1.jar.original
-rw-r--r-- 1 jenkins jenkins 20125827 May 26 09:30 ccit-0.0.1.jar

Step4: Deploying same server launch website run the below command 

java -jar target/ccit-0.0.1.jar.jar --server.address=0.0.0.0 --server.port=8081

[root@ip-10-0-1-158 target]# java -jar ccit-0.0.1.jar --server.address=0.0.0.0 --server.port=8081

2025-05-26T09:43:52.178Z  INFO 23804 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''

2025-05-26T09:43:52.230Z  INFO 23804 --- [           main] com.example.App                          : Started App in 4.855 seconds (process running for 5.727)

Step5: See below index html page opened with ,which is you have given port 8081

Step6:here is the drawback the java run timer always need to execute ,if you cancel that the page will not open ,to over come the issue need create one service

[root@ip-10-0-1-158 target]# java -jar ccit-0.0.1.jar --server.address=0.0.0.0 --server.port=8081

Step7:Service creations ,what version you have mentioned in the pom.xml with same version will create .jar file, if version will change you can also do same changes on version
Created Service below way and start 
[root@ip-10-0-1-158 ~]# vi /etc/systemd/system/cciwebsite.service
[Unit]
Description=My Soring Boot Web Applicationaws
After=network.target
[Service]
User=ec2-user
workingDirectory=/var/lib/jenkins/workspace/ccitpipelinejob
ExecStart=/usr/bin/java -jar /var/lib/jenkins/workspace/ccitpipelinejob/target/ccit-0.0.1.jar --server.address=0.0.0.0 --server.port=8081
SuccessExitStatus=143
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
[root@ip-10-0-1-158 ~]# sudo systemctl start cciwebsite.service
[root@ip-10-0-1-158 target]# ps -ef|grep workspace
ec2-user 10023     1  4 13:00 ?        00:00:06 /usr/bin/java -jar /var/lib/jenkins/workspace/ccitpipelinejob/target/ccit-0.0.1.jar --server.address=0.0.0.0 --server.port=8081
root     11864  8149  0 13:02 pts/1    00:00:00 grep --color=auto workspace
Step8:With out running the command webpage open ,using service

Step9: we have problem , -if any have any changes on git script contact-123 added 


Step10
[root@ip-10-0-1-158 ~]# sudo systemctl daemon-reload
[root@ip-10-0-1-158 ~]# sudo systemctl enable cciwebsite.service
[root@ip-10-0-1-158 ~]# sudo systemctl restart cciwebsite.service
[root@ip-10-0-1-158 ~]# sudo systemctl start jenkins
Add Jenkins give permission to  sudo user
[root@ip-10-0-1-158 ~]# 
[root@ip-10-0-1-69 ~]#sudo -i
[root@ip-10-0-1-69 ~]# sudo visudo
jenkins ALL=(ALL) NOPASSWD: ALL

[root@ip-10-0-1-158 ~]# sudo systemctl daemon-reload
[root@ip-10-0-1-158 ~]# sudo systemctl restart cciwebsite.service

Step11:






Step12:
pipeline {
    agent any
   tools
   {
       maven "ccit-mvn"
   }
    stages {
        stage('git') {
            steps {
                git branch: 'main', url: 'https://github.com/Vakatisubbu/ccit-javaapp.git'
            }
        }
           stage('build') {
            steps {
                sh "mvn clean package"
                sh "sudo systemctl daemon-reload"
                sh "sudo systemctl restart cciwebsite.service"
                
            }
        }
    }
}
Step13: Git change  https://github.com/Vakatisubbu/ccit-javaapp.git,change contact-RAJA additional name for testing 
    <a class="nav-link" href="#">Contact-RAJA</a>

Step14:See after build automatically reflect the changes 



                                    How setup variables in Jenkins pipeline?
Variable in jenkins pipeline allow you to store and reuse values.You can define them at different levels.
->Environmet variables(Global & Custom)
->Pipeline parameters(User input)
->Local variables(inside pipeline)
 
Environment variable(Global & Custom)

Jenkins has built-in environment variables, but you can also define custom ones.
 Built-in Environment variables
Jenkins provides default environment variables like:
 $BULID_NUMBER -->Build number of the job.
$JOB_NAME-->Name of the current Jenkins job
$WORKSPACE-->Path of the workspace directory

Step1: Global Environment variable


 Step2: pipe line script
pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                echo "Hello World: ${env.ccit}"
            }
        }
    }
}

After build environment value displayed

Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/ccitpipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] echo
Hello World: AWS
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Step3: environment Gobally define mean you can use all stages,below build 
pipeline {
    agent any
 environment {
            x='20'
        }
    stages {
       
        stage('First') {
            steps {
                echo "First: $x"
            }
        }
          stage('Second') {
            steps {
                echo "Second: $x"
            }
        }
    }
}

Step4:Build success 
First: 20
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Second)
[Pipeline] echo
Second: 20

Parameterized Block 

Step1:


Step2: pipeline script 

pipeline {
    agent any
 parameters {
            string(name:'ccit' ,defaultValue:'AWS', description: 'enter your course name')
        }
    stages {
       
        stage('First') {
            steps {
                echo "The course name is : ${params.ccit}"
            }
        }
             }
}


Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/ccitpipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (First)
[Pipeline] echo
The course name is : AWS
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Step3: Environment variables using we can get know build count ,build name ,total sofar 11 build 

pipeline {
    agent any
    stages {
       
        stage('First') {
            steps {
                echo "Build number is : ${env.BUILD_NUMBER}"
            }
        }
         
    }
}

Below is the console output

Running on Jenkins in /var/lib/jenkins/workspace/ccitpipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (First)
[Pipeline] echo
Build number is : 11

--Thanks 




No comments:

Post a Comment