Continuous Delivery with Jenkins

Continuous Delivery with Jenkins
We are going to see now how deployment happens from Jenkins to tomcat server. This is going to be a full-fledged Continuous delivery project by using Jenkins. We will need to setup two tomcat servers where we will deploy our artefact.
Tomcat setup
Login as root user
sudo -i
apt-get update
apt-get install default-jre
apt-get install zip
wget  https://www-eu.apache.org/dist/tomcat/tomcat-8/v8.5.15/bin/apache-tomcat-8.5.15.zip
mv apache-tomcat-8.5.15.zip /opt
cd /opt
unzip apache-tomcat-8.5.15.zip
cd apache-tomcat-8.5.15
Give tomcat start & stop script executable permission
cd /opt/apache-tomcat-8.5.15/bin
chmod u+x startup.sh
chmod u+x shutdown.sh
chmod u+x catalina.sh

Open browser and verify tomcat service  http://tomcatIP:8080

Set user, password & role for tomcat server
cd /opt/apache-tomcat-8.5.15/
vi conf/tomcat-users.xml  

Replace below mentioned content
with

Comment anti Resource Locking values
Replace
with
Stop and start tomcat

Verify the settings

http://tomcatIP:8080/manager/
Username: tomcat
password: tomcat
You should see below mentioned screen.

Project Setup
This project is a complete build pipeline which will build java SC, create war package, run static code analysis, deploy package to staging tomcat server & deploy to prod tomcat server.
Pipeline consist of below mentioned Jenkins job working together to create entire continuous delivery pipeline.
1. Package job – This job will checkout source code from git repository, use Maven to build the code, archive the artefacts, trigger Static code analysis job & staging deploy job
2. Static code analysis – This job will checkout source code from git repository, use Maven to run checktyle code analysis and publish graph for STA.
3. Deploy to staging Tomcat server – This job will copy the war artefact from package job to deploy job, Deploy the artefacts to Staging tomcat server & trigger Prod deploy job.
4. Deploy to Prod Tomcat Server – This job will copy the war artefact from package job to deploy job, Deploy the artefacts to Staging tomcat server & trigger Prod deploy job. 

Prerequisites:

1. Jenkins server: - Jenkins server should have openjdk 1.7, Git, Maven (Follow Jenkins setup doc) 2. Plugins: - git, maven, copy artifacts, deploy to container, checkstyle, build pipeline 3. Staging tomcat server: - Create a vm or ec2 instance with ubuntu OS & Follow tomcat setup doc. 4. Prod tomcat server:- Create a vm or ec2 instance with ubuntu OS & Follow tomcat setup doc. 
Create Jenkins jobs:
1. CJP-package: - Create a new job with CJP-package name, freestyle project.
Refer below mentioned screenshots to setup the job

2. CJP-code-analysis: - Create a new job with CJP-code-analysis name, freestyle project.


Refer below mentioned screenshots to setup the job
3. CJP-Deploy-Stage:- Create a new job with CJP-Deploy-Stage name, freestyle project. Refer below mentioned screenshots to setup the job

Deploy-Prod: - Create a new job with CJP-Deploy-Prod name, freestyle project.
Refer below mentioned screenshots to setup the job 

Setup build pipeline view

Go to Jenkins main dashboard
Click on the plus symbol 
Select Build Pipeline View, give pipeline view a name and click OK


Select initial Job – CJP-Package & click OK


Click run and verify if the entire pipeline works


Jenkins Build Triggers

So far we have seen executing Jenkins jobs manually by clicking on the Build Now button.
But there are other ways by which the jobs can be executed those are called as Build Triggers. 

Trigger build remotely
We can use this trigger If we want to run the job from a remote location like from a script or from Github or any other tool that can hit the URL. One typical example for this feature would be to trigger new build from the source control system's hook script, when somebody has just committed a change into the repository, or from a script that parses your source control email notifications. You'll need to provide an authorization token in the form of a string so that only those who know it would be able to remotely trigger this project's builds .


Now you can use the URL with the token JENKINS_URL/job/beandeploy/build?token=TOKEN_NAME to call this job from
1. CLI like curl command
1. curl http://username:password@192.168.1.9:8080/job/beandeploy/build?token=electric
2. Through a script
3. From a browser
4. Github webhooks
1. In github repo go to Settings => Webhooks => Enter Jenkins URL and Token => Add Webhook.
Now whenever there is a commit in the git repo it will trigger the Jenkins job by hitting Jenkins remote URL. 
Build after other projects are build.
This is to set the job as downstream for another job. Mention the upstream job name, once the upstream job gets completed successfully our job will get triggered.



Build Periodically
If we want to schedule our job to get executed for example every night 8 pm then we can use a cronjob format to specify the time.


This field follows the syntax of cron (with minor differences). Specifically, each line consists of 5 fields separated by TAB or whitespace:
MINUTE HOUR DOM MONTH DOW
MINUTE Minutes within the hour (0–59)
HOUR The hour of the day (0–23)
DOM The day of the month (1–31)
MONTH The month (1–12)
DOW The day of the week (0–7) where 0 and 7 are Sunday. 
Poll SCM
It’s similar to cronjob but instead of running the job at the interval it will go and check if there is any new commit in the VCS repo like github repo’s and then execute the job.








Comments