Thursday, May 15, 2025

Jenkins part4

 Jenkins part4

Class 24th Jenkins May 15th
  • Variables & Parameters in Jenkins project 
  • Pipeline structure & setup in Jenkins
(Some time we have to pass some inputs to jenkins job at runtime)
In Jenkins, parameters allow you to customize and control how a build run by  providing input values at runtime.There are several types of parameters you can use in a Jenkins project
Types of parameters in Jenkins
  • String parameter
  • Multi-line string parameter
  • Choice parameter
  • File parameter
  • Password parameter
  • Boolean parameter
Practical
Create one job Free style project 
Step1:  String parameter
Create Job >ccitvar >Freestyle project > Check This project is parameterized 


[root@ip-10-0-1-137 ccitvar]# pwd
/var/lib/jenkins/workspace/ccitvar
[root@ip-10-0-1-137 ccitvar]# ls -lrt
total 0
-rw-r--r-- 1 jenkins jenkins 0 May 24 12:49 ccit.txt
-rw-r--r-- 1 jenkins jenkins 0 May 24 12:54 aws.txt
-rw-r--r-- 1 jenkins jenkins 0 May 24 12:54 Devops.txt

Step2:  Multi-line string parameter
[root@ip-10-0-1-137 ccitvar]# ls -rlt
total 0
-rw-r--r-- 1 jenkins jenkins 0 May 24 13:10 GCP.txt
-rw-r--r-- 1 jenkins jenkins 0 May 24 13:10 Devops
-rw-r--r-- 1 jenkins jenkins 0 May 24 13:10 Azure
-rw-r--r-- 1 jenkins jenkins 0 May 24 13:10 AWS
Step3:Choice parameter
Build parameter list 
[root@ip-10-0-1-137 ccitvar]# ls -lrt
total 0
-rw-r--r-- 1 jenkins jenkins 0 May 24 13:17 Azure.txt

Step4: File parameter This Upload your files
Choose you file in you laptop and build upload some text file ,it will created with given file name
Placed 
[root@ip-10-0-1-137 ccitvar]# ls -rlt
total 0
-rw-r--r-- 1 jenkins jenkins 0 May 24 13:21 script.sh

Pipeline setup in Jenkins 
Jenkins pipeline is a combination of plugins that Support integration and implementation of continuous delivery pipelines
A pipeline is a group of events interlinked with each other in sequence 
There are two types of Jenkins pipeline syntax used for defining you Jenkins file:
  •  Declarative pipe line (Recommeded)
Easier to read and maintained 
Uses as structure pipe line {} block
Best for simple CI/CD pipline 
Why we use declaration (Simple and structured,built-in error handling, Best for CI/CD use cases)
  •  Scripted (Most Felxible)
Uses node {} instead of pipeline{}
Provide more control (eg. loops,Conditionals)
suitable for complex automation workflows
why user scripted(more control with loops & conditionals,Supports custom logic (id-else,try-catch)
,Best for advanced users & dynamic workflows)

Free style Project : only select save Command and build
Pipeline  : Here we write scripts 
Code->Build->Test->Deploy
Declarative (PASSS)
  Newitem ->Name ->Pipeline ->Save
 pipeline {
  agent any (Where you build need perform,your server or any other server Slave concept)
     stages {
               stage ("code"){
                            steps {
                                           Action (Git)
                          
                                      }
                           
                          }
                    }
            }
P-Pipeline A- Agent S -Stages -Stage -Steps

Step5: create job in  ccitpipelinejob

pipeline {
    agent any

    stages {
        stage('dev') {
            steps {
               sh "sudo yum install -y httpd"
               sh "sudo systemctl start httpd"
               sh "sudo git clone https://github.com/cloudcomputingintelugu/ccitwebsite.git"
               sh "sudo cp -r ccitwebsite/* /var/www/html"
            }
        }
    }
}

Build will failed ,Due to Jenkins don't have direct root user permissions install the softwares

Started by user admin [Pipeline] Start of Pipeline [Pipeline] node Running on Jenkins in /var/lib/jenkins/workspace/ccitpipelinejob [Pipeline] { [Pipeline] stage [Pipeline] { (dev) [Pipeline] sh + sudo yum install -y httpd We trust you have received the usual lecture from the local System Administrator. It usually boils down to these three things:ERROR: script returned exit code 1 Finished: FAILURE
Step6: Add Jenkins give permission to 
[root@ip-10-0-1-69 ~]#sudo -i
[root@ip-10-0-1-69 ~]# sudo visudo
## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
jenkins ALL=(ALL) NOPASSWD: ALL
#includedir /etc/sudoers.d
Step7:Build Success

Step8: Git simple web page file hosted successfully to the server 
[root@ip-10-0-1-69 ccitwebsite]# ls -lrt
total 44
-rw-r--r-- 1 root root   789 May 25 07:21 readme.txt
-rw-r--r-- 1 root root 40878 May 25 07:21 index.html
drwxr-xr-x 8 root root    77 May 25 07:21 assets

Step9:Web page open successfully hosted 


--Thanks




No comments:

Post a Comment