Jenkins
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
I
If work space is not there, press Build now it will show console output open it
Next enter ls it will show workspace.
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
Cd target/
ls it will show war file
cd apache—tomcat-9.0.65/
ls
vi conf/tomcat-users.xml
. /startup.sh
Refresh the tomcat web page after build now is success, it will show myweb as belowif we enter it will
show GANA TECH
Build triggersselect the belowapply & 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
[Service]
Enviroinment=”JENKINS_PORT2020”
Now open server with new port number ex: jenkins publuc ip:2020id & password
Open Jenkins dashboard Manage Pluginsselect availablesearch git parameterselect Git
parameter & install
Create a job:
Open New item name it as parameterselect socurce code maangementSelect Git & paste
Github urlSelect Buildselect Invoke Top-level Maven targetsGoals: clean package select This
project is Parameterizedselect Git ParameterName: BranchParameter TypeParameter Type:
Branch Default Value${BRANCH}
Default Value${BRANCH} it will list out all branches in Git hubIt will show Build with Parameters
instead of Build nowselelct Build with parametersit 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 jenkinsmanage userscreate users
If we want to check go to home directory
cd /var/lib/jenkins/
ls
ls -1 |grep sonar*
12.78.964.78:8080/restart
Manage jenkins Global Tool ConfigurationScroll down & check MavenAdd MavenName:
Maven 3.6.1Version: select 3.6.1save & applyThis will be updated once we restart Jenkins
Select jobaConfigureselect Build triggersselect Build after othert project build Projects to
watch: jobb
Select jobbConfigureselect Build triggersselect Build after othert project build Projects to
watch: jobc
Select jobcConfigureselect Build triggersselect Build after othert project build Projects to
watch: joba
Click on +besides all where it shows Joba,jobb,jobcName=BP1select Build Pipeline view create
To do parallel jobs we need to remove loops created for joba, jobb,jobc by below process
Select jobaconfigureBuild TriggersProjects to watch=delete jobaapply & ok same for jobb &
jobc
Build now
MASTER/SLAVE CONCEPT
sudo su
sudo su
cd jenkins/
ls
tar -xvf apache-tomcat-9.0.65.tar.gz for unzipping
cd apache-tomcat-9.0.65/
ls
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
Tomcat URL: copy from browser & paste i.e., Public Ip:8080apply & save build
nowsuccess if we do refresh in browser it will display as below
We can also use Poll SCM concept here with below steps:
ConfigureBuild Triggers select Poll SCM=5 starts *****Apply & save
For changing developer modified code go to Git hub
projectSRCMainWebappIndexmodify 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
"""
}
ls
tar -xvf apache-tomcat-9.0.65.tar.gz
cd apache-tomcat-9.0.65/
ls
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
Select Pipeline syntaxwe 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=GitCredentials
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,)
"""
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