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.
pipeline {
agent any
tools
{
maven "ccit-mvn"
}
stages {
stage('git') {
steps {
}
stage('build') {
steps {
sh "mvn clean package"
}
}
}
}
[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
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 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
[root@ip-10-0-1-158 ~]# sudo systemctl daemon-reload
Step14:See after build automatically reflect the changes
Step1: Global Environment variable
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
pipeline {
agent any
environment {
x='20'
}
stages {
stage('First') {
steps {
echo "First: $x"
}
}
stage('Second') {
steps {
echo "Second: $x"
}
}
}
}
First: 20 [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Second) [Pipeline] echo Second: 20
Parameterized Block
Step1:
Step2: pipeline script
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
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