0% found this document useful (0 votes)
31 views48 pages

Jenkins

The document provides instructions for installing and configuring Jenkins on AWS EC2 instances to set up a master-slave configuration. It describes how to install Jenkins on the master instance, then connect a slave node instance to the Jenkins master to enable parallel job execution. Key steps include installing Java and configuring the slave instance, then defining the slave node in Jenkins and configuring SSH credentials to allow the master to connect to and launch jobs on the slave. This allows distributing build work across multiple instances for improved performance.

Uploaded by

rahulchakri0369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views48 pages

Jenkins

The document provides instructions for installing and configuring Jenkins on AWS EC2 instances to set up a master-slave configuration. It describes how to install Jenkins on the master instance, then connect a slave node instance to the Jenkins master to enable parallel job execution. Key steps include installing Java and configuring the slave instance, then defining the slave node in Jenkins and configuring SSH credentials to allow the master to connect to and launch jobs on the slave. This allows distributing build work across multiple instances for improved performance.

Uploaded by

rahulchakri0369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 48

JENKINS

Jenkins- Dream city, Bamboo, TFS alternate tools


Difference between Jenkins workspace & Jobs: The jobs directory is the place where Jenkins
stores the job definitions, build artefacts, and build logs. Workspace is the directory that is used
to create the build. One can choose which files to store as build artefacts in the job
configuration.

=== > >copy this path


9. cont. = = = >>enter === >>encrypted password = =>>copy
JENKINS INSTALLATION LAB:
AWS EC2 Instance Login create InstanceName Instance as Jenkins
Sudo su
yum install -y java -1.8.0- openjdk-devel .x86_64 (or) sudo yum install java-1.8.0-openjdk-
devel
sudo rpm --import https://yum.corretto.aws/corretto.key
sudo curl -L -o /etc/yum.repos.d/corretto.repo
https://yum.corretto.aws/corretto.repo
sudo yum update
sudo yum install java-11-amazon-corretto-devel
java -version

yum install -y git

sudo wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-


maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo
sudo sed -i 's/\$releasever/6/g' /etc/yum.repos.d/epel-apache-maven.repo
sudo yum install -y apache-maven (or) sudo yum install maven

sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo


sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
sudo amazon-linux-extras install epel -y
yum install jenkins -y
jenkins --version

service jenkins start  To start Jenkins

Copy Jenkins instance Public ip example http://13.233.33.77:8080/ and paste in browser & copy
path showing in red colour & paste it in Linux
cat /var/lib/Jenkins/secrets/initialAdminPassword path copied  it will show encrypted format
password select the password & paste it In administrative password in the browser.

Select Install suggested plugins, it will get this page, enter details.
This will display Jenkins dashboard.
cd /var/lib/jenkins/
pwd  It will display var/lib/Jenkins
ls  it will display files, workspace is missing in jobs folder, users etc which are in blue

vi config.xml  it will show Jenkins version, security, workspace, nodes, slaves etc info

:q!
ls 

we can see how many items user has created

sample work: we will create a print file


select New itemEnter item namefreestyle projectit will show six options select
buildExecute shellEnter echo “Welcome to Jenkins world” in Command boxsave &
apply
Project Sample = Work name, check Workspace
ls -- to check file work space, it is not created as we have not run the file

I
If work space is not there, press Build now it will show console output open it
Next enter ls  it will show workspace.

Our sample project will be shown in jobs & workspace folder


cd jobs/
ls  it will display as sample
cd ..  to exit/back
cd workspace/
ls  it will display sample (sample is project name)
cd ..
ls
After configure it will display the below copy & paste Git hub Repository url

Apply & save changes--) build run & check the file is success or not?
Check workspace for Git hub files
cd sample/
ls  it will show display all files including Git files

Now Git Integration completed, we will integrate Maven


Go to Jenkins Dashboard Configure BuildAdd Build Stepselect Invoke Top Level Maven
TargetsGoalsclean packageApply & Save
Now Build run it will show 3rd file & if we open it will run maven filessampletargetIt
will display below if we click myweb-8.2.0.war it will be saved in local laptop

Ls it will display all files

Cd target/
ls  it will show war file

Create new EC2 Instance for Tomcat

Install Java & Tomcat

ls  it will show apache—tomcat-9.0.65.tar.gz

tar -xvf apache—tomcat-9.0.65.tar.gz

ls  It will display apache—tomcat-9.0.65 apache—tomcat-9.0.65.tar.gz

cd apache—tomcat-9.0.65/

ls

chmod 777 webapps/


vi webapps/manager/META-INF/context.xml  remove value tag & :wq!

vi conf/tomcat-users.xml

esc shif g esc o  to go to last column

cd bin  To start tomcat services

. /startup.sh

Tomcat Public IP:8080

Enter Username & Password

After this open Jenkins webserver


Search for any starting with D, Apply & save without changes

Installing Plug ins  Manage Jenkinsmanage plug ins Deploy to


Install without restart & In dashboard enter Projectconfigure Git & Maven BuildDeploy war
/ear to a containerPost-BuildWar/Ear file=target/* .war & career path=myweb
ContainersAdd containersselect Tomcat 9 (we have downloaded tomcat 9 because in jenkins it is
available)CredentialsaddTomcat Username & Passwordafter this select Tomcat
credentialTomcat URLcopy tomcat url & paste it http://13.115.95.191:8080/ apply & save

Enter build now

Refresh the tomcat web page after build now is success, it will show myweb as belowif we enter it will
show GANA TECH
Build triggersselect the belowapply & save

Now for example modify in myweb of github instead of GANA TECH 3500, modify it to 3400 & watch
Jenkins, it will update in 1 min (this is the use of pollscm)

For Web hook open git hub & edit as per above, payload url is jenkins url
After rhis open jenkins & do as below

Build run edit and check GANA TECH

Go to jenkins instance and coomnd below


systemct1 edit jenkins

Control +r control +opaste

[Service]

Enviroinment=”JENKINS_PORT2020”

Ctrl x enter ctrl c

Service jenkins stop

Service jenkins stop

Now open server with new port number ex: jenkins publuc ip:2020id & password
Open Jenkins dashboard Manage Pluginsselect availablesearch git parameterselect Git
parameter & install

Create a job:

Select Git hub url

Open New item name it as parameterselect socurce code maangementSelect Git & paste
Github urlSelect Buildselect Invoke Top-level Maven targetsGoals: clean package select This
project is Parameterizedselect Git ParameterName: BranchParameter TypeParameter Type:
Branch Default Value${BRANCH}
Default Value${BRANCH} it will list out all branches in Git hubIt will show Build with Parameters
instead of Build nowselelct Build with parametersit will show all branches: select origin/dev
branch Build If war file is generated it will be success, if not it will fail (this is for testing
purpose).-->we an check this by following steps
Manage jenkinsmanage userscreate users
If we want to check go to home directory

cd /var/lib/jenkins/

ls

ls -1 |grep sonar*

pwd  for 3rd method

How to safe restart Jenkins dashboard?

12.78.964.78:8080/restart

To check maven version: mvn –version


To update maven:

Manage jenkins Global Tool ConfigurationScroll down & check MavenAdd MavenName:
Maven 3.6.1Version: select 3.6.1save & applyThis will be updated once we restart Jenkins

Jenkins dashboardItem nameselect freestyleBuildadd build step: select Execute shell

Command : echo “joba”apply & save

Jenkins dashboardItem nameselect freestyleBuildadd build step: select Execute shell

Command : echo “jobb”apply & save

Jenkins dashboardItem nameselect freestyleBuildadd build step: select Execute shell

Command : echo “jobc”apply & save

Select jobaConfigureselect Build triggersselect Build after othert project build Projects to
watch: jobb

Select jobbConfigureselect Build triggersselect Build after othert project build Projects to
watch: jobc
Select jobcConfigureselect Build triggersselect Build after othert project build Projects to
watch: joba

If we run one job it will run all jobs sequentially.

Ex: select jobaBuild nowsuccess


Visualisation:

1.Build Pipeline plug in: Jenkins DashboardManage Jenkins Manage PluginsAvailable=Build


Pipelineselect Install without restart—Installation success

Click on +besides all where it shows Joba,jobb,jobcName=BP1select Build Pipeline view create

Scroll downPipeline FlowLayout=

Next select initial job=jobaapply & ok

We can see run, history etc..,


2) Deleivery Pipeline Plugin: Jenkins DashboardManage Jenkins Manage
PluginsAvailable=Delivery Pipeline Pluginselect Install without restart—Installation success

Click on +besides all where it shows Joba,jobb,jobcName=DP1select Delivery Pipeline view


createName=DP2scroll down for select PipelinesComponentsaddName=Delivery
PipInitial job=jobafinal job=jobc (it will not show jobc) Apply & okit will build automatically

Scroll downPipeline FlowLayout=


To overcome sequential which effects CPU Parallel jobs came into the picture.

To do parallel jobs we need to remove loops created for joba, jobb,jobc by below process

Select jobaconfigureBuild TriggersProjects to watch=delete jobaapply & ok same for jobb &
jobc

Install Multi job plugin: Jenkins DashboardManage Jenkins Manage PluginsAvailable=Multijob


Pluginselect Install without restart—Installation success

Jenkins dashboardItem name=multijobselect multijob projectokBuildadd build step: select


Multijob phasePhase name=multijob1Phse jobsJob name=jobajob=jobbjobcApply &
save
It will follow all jobs in downstream only.

Build now
MASTER/SLAVE CONCEPT

Create EC2 InstanceSlave 1/Node 1 in additional to Jenkins instance


Open Jenkins EC2 instance:

sudo su

service jenkins status

service jenkins start

44.455.66.78:8080  ctreate user name & password

Open Slave 1/Node1 EC2 Instance

sudo su

yum install -y java-1.8.0-openjdk-devel.x86_64

Now open Jenkins dashboardManage JenkinsManage nodes and Clouds

New NodeNode name=rahulSelect Permanent agentcreate

Name=node devDescription=any thingNumber of executors=3 Remote root


directory/home/ec2-user/jenkins Labels=dev1Usage=Only build jobs with label expressions
matching this nodelaunch method=Launch agent via SSHHost=Slave Private
IPCredentials:Add=JenkinsKind=SSH Username with private keyUser name=ec2-userselect
Enter directly key=copy pem file key & paste it addselect credential ec2-useHot key
verification stratergyAvailabilitySave it (it will display slave)
(check in slave instance: pwd it will display /home/ec2-user

ls not displayed anything

ls  after above all steps it will display Jenkins folder

cd jenkins/

ls  it will display remoting remoting.jar folder

Now slave is added we should create job as below steps

 Jobs should be created in master

Jenkins dashboardNew itemselect free style projectBuild:Execute Shell-Command=echo


“Welcome to Jenkins Master/Slave architecture!” (for printing)General: select Restrict where
the project can be rn Label Expression=Dev Apply & Save Build now
cd jenkins/
ls  It will display sample 2

Other Project for Example:


New ItemEnter Name=Project 2Free style projectCreate Select General=Select
Restrict where this project can be run, Label Expression=devSelect Git, Repository=copy &
paste https://github.com/Rahul0104Amb/myweb.git Try build now it will fail because
“Error cloning remote repo ‘origin’ “( This is failed because we have not installed git in slave)
Install git
Agin try build now success
Now try Maven: Configure Build, Invoke Top level Maven targets=clean packageApply &
save Build failed (Due to not installation of Maven)
Install Maven & Try build success
cd /home/ec2-user/
Install Tomcat by visiting official page 9.0.65

ls 
tar -xvf apache-tomcat-9.0.65.tar.gz  for unzipping
cd apache-tomcat-9.0.65/
ls

chmod 777 webapps/


vi webapps/manager/META-INF/context.xml

esc wq!
vi conf/tomcat-users.xml  for creating Tomcat users
scroll down for rolenames , enter with proper alignment

Esc wq!
cd bin  for starting & stoping services
. /startup.sh  Tomcat service started
Now copy Public IP of Slave EC2 Instance & paste in browser with port code as below
23.66.755.88.8080
Now enter User name & password created in Vi conf, tomcat
Now Build now will be success

Manage JenkinsManage PluginsAvailable search deploy to containerselect project


2Configure Select General=Select Restrict where this project can be run, Label
Expression=devSelect Git, Repository=copy & paste
https://github.com/Rahul0104Amb/myweb.git Build=Clean packagePost build
actions=select Deploy war/ear to a container=WAR/EAR file=target/*.war & context 
Path = my web Container=select Tomcat 9Add credentials,username with
passwordadd it will display as below

Tomcat URL: copy from browser & paste i.e., Public Ip:8080apply & save build
nowsuccess  if we do refresh in browser it will display as below

We can also use Poll SCM concept here with below steps:
ConfigureBuild Triggers select Poll SCM=5 starts *****Apply & save
For changing developer modified code go to Git hub
projectSRCMainWebappIndexmodify 37000 to 33000 or any commit changes
Now Build Now success If we do refresh we will get 33000
JENKINS PIPELINE
DECLARATIVE PIPELINE:
stage("Git Checkout"){
steps{
git credentialsId: 'javahome2', url: 'https://github.com/srinicloud87/myweb.git'
}
}
stage("Maven Build"){
steps{
sh "mvn clean package"
sh "mv target/*.war target/myweb.war"
}
}
stage("deploy-dev"){
steps{
sshagent(['tomcat-new']) {
sh """
scp -o StrictHostKeyChecking=no target/myweb.war
[email protected]:/home/ec2-user/apache-tomcat-9.0.73/webapps/

ssh [email protected]
/home/ec2-user/apache-tomcat-9.0.73/bin/shutdown.sh

ssh [email protected] /home/ec2-user/apache-tomcat-9.0.73/bin/startup.sh

"""
}

Edit git url with our url


3rd step IP should be Tomcat Private Ip
For this we need 2 instances
Create 2 instances
In Jenkins InstanceOpen Jenkins browser & In Tomcat Instance download Tomcat after
installing JAVA
after installing continue with below steps in the instance

ls 
tar -xvf apache-tomcat-9.0.65.tar.gz
cd apache-tomcat-9.0.65/
ls

chmod 777 webapps/  for giving full permissions


vi webapps/manager/META-INF/context.xml

esc wq!
vi conf/tomcat-users.xml  for creating Tomcat users
scroll down for rolenames , enter with proper alignment

Esc wq!
cd bin  for starting & stoping services
. /startup.sh  Tomcat service started
Now copy Public IP of Slave EC2 Instance & paste in browser with port code as below
23.66.755.88.8080
Now enter User name & password created in Vi conf, tomcat

Mange

Manage JenkinsManage PluginsAvailable search SSH agent Install without restart


New ItemProject 3Project type=Pipeline

Select Pipeline syntaxwe need 2 sytax (1 for Git Cloning & 2 nd for Building-SSH war
plugin)Click 2 times on Pipeline sytax and Git on other tab
Open 1st Pipeline syntax in browser Sample step=select git=GitCredentials

Addselect JenkinsUsername=Github usernamepassword=github


passwordId=javahome2Generate Pipeline script
Open 2nd Pipeline syntax in browser Sample step=sshagent SSH AgentAddJenkins

Kind=SSH username with private keyScope=GlobalID=tomcat-newusername=ec2-


userselect Enter directlykey=copy & paste pem file addGenerate Pipeline script

stage("Maven Build"){
steps{
sh "mvn clean package"
sh "mv target/*.war target/myweb.war"
}
}
stage("deploy-dev"){
steps{
sshagent(['tomcat-new']) {
sh """
scp -o StrictHostKeyChecking=no target/myweb.war
[email protected]:/home/ec2-user/apache-tomcat-9.0.73/webapps/ (mentioned no
because we have already created SSH key,)

ssh [email protected](change to tomcat ip address) /home/ec2-user/apache-


tomcat-9.0.73/bin/shutdown.sh

ssh [email protected] /home/ec2-user/apache-tomcat-9.0.73/bin/startup.sh

"""

After modifying IP address  commit changes in the Git hub file


delete syntaxes for avoiding confusion

Go to ProjectPipelineDefinition=Pipeline script from SCM


Build TriggersSCM=GitRepositoriesRepositories URL=copy & paste GIT
URLcredential=select userscriptpath=Jenkins-decnewsave & apply—Build nowFailed
in 3rd stage i.e., deployementbecause war file did not copied due to miss match of ec2 user,
give permissions to connect user

Go to ec2 Instance & do as below


cd /home/ec2-user/
ls -l

here tomcat is in root root, where as in Jenkins ec2-user , so connect by below command
chown -R ec2-user:ec2-user apache-tomcat-9.0.65
ls -l
Build now

If we refresh we will get as above


We can also do this by Poll SCM as below
Pipeline Project 3configureBuild triggersselecct Poll SCMschedule=*****apply &
savebuild success

UPGRADE JENKINS INSTANCE


AMI/IMAGE backupactionsimage & templetescreate imageimage
name=rahulcreate image

You might also like