Wednesday, June 22, 2022

Jenkins Continuous Delivery

👋 Warm Welcome 🤗 to my dear DevOps DevSecOps /SRE engineers here in this post we will exploring about Jenkins configured to run a full fledged CI/CD flow.

Preparation for CI/CD

The full length end-to-end automation can be done with the Jenkins Jobs. We need to identify how the dependencies forms the Job chain, what job become upstream and what all jobs can be downstream to which job also you need to plan as per your project needs. Here I'm taking the scenario where Java based web application package and deployment to different environments such as QA, Staging and for Production with approval to run the Build.

The sequence of steps involved in this process are:
  • Install Tomcat server 
  • Using Jenkins CICD job deploy 

Install Tomcat Server on a VM/Cloud instance

Here the VM/Cloud instance assuming as Ubuntu Linux machine. d


Step 1: Install JRE/Java

To install Tomcat on any Platform there should be JRE available. To have JRE we do install JDK which is suitable to the Tomcat. As an example I'm installing JDK8 with the following command.

sudo apt-get install openjdk8 -y; java -version
Step 2: Installing the latest version of Tomcat
The following are not exact commands you need to visit Tomcat Office site to download mirror link.
 
sudo apt install -y unzip wget
wget mirrorlink to download the latest version of tomcat.zip file 
sudo mkdir -p /opt/tomcat 
sudo unzip -d /opt/tomcat tomcat.zip 
ls -l /opt/tomcat  #confirm the extraction
Step 3: Change Tomcat server port, if your Tomcat is installed on the same machine where Jenkins runs. This is to avoid port conflict. Port value change by searching 'Connector' inside the /opt/tomcat/conf/server.xml file. 

Step 4: Change file permission of scripts in the 'bin' folder, that is
cd /opt/tomcat/bin 
sudo chmod +x *
Step 5: start tomcat server from the bin directory run the following
./startup.sh
less -f ../logs/catalina.out # output show Tomcat server started log message.

 Jenkins Setup for Continuous Deployment(CD)

Jenkins automations makes lord of windows in DevOps world. If you want to enjoy that automations in your project please follow this post, here we are going to have the objective as Jenkins continuous integration and continuous deployment to a target environment. The target environment could be built based on the Tomcat server.  Usually the environment can be for development, QA staging and production. if the automation flow begin from the development  environment test success to QA and then staging we can do job chain or a pipeline automatic deployment stages including steps usage in a DSL file that is Jenkinsfile.

Install Jenkins plugins

On the Jenkins console in the dashboard navigate to the Manage Jenkins, then Manage plugins, go to the availability tab and search for the following plugins install them without restart 

 1. Copy Artifact 

 2. Deploy to Containers 

Create Jenkins Jobs

In the first phase of this experiment you will Create Job to produce a Tomcat Deployable Artifacts. Meaning a servlet or JSP containing Java Web applications inside the deployable file. And it's extension can be w a r (web archive) file.

In the view page click on the plus + and  Create a list View and name it as "QA Deploy".

 1. Project name: package_app (Maven build)

a. Please enter in the DescriptionGenerate artifacts to deploy on Java tomcat server 

 b. Under SCM  section enter your own git URL : https://github.com/BhavaniShekhar/Jenkins_Integrations.git

c. Build Environment section - Please Check "Delete workspace before build starts" and "Add timestamps to the Console Output".

 d. Build section - select "Invoke top-level Maven targets" from the dropdown options.

 Choose Maven Version value as you configured it earlier in the Global tool configure name (in my example maven3 used)

 Goals value can be choosen as - clean package

 If pom.xml is not in the project start directory then go for "Advanced" option then provide the POM value as file path : java-tomcat-sample/pom.xml 

e.  In the Post-Build actions Add "Archive the artifacts"

 Files to archive  **/*.war

Now all the settings are done for the package_app job so now Save it, and run it by using "Build Now". Go to the Console Output which shows the full log build execution after confirming that "Build Successful". 

Navigate to last build number for me it is first build so Build #1 there we can see the "Build Artifacts" shows the war file downloadable link.

Automated Deployment

2. Create Deploy application job in the same "QA deploy" view.

Select a Freestyle project.

Project Name: deploy_app

Description: This job will be used to deploy the artifact to QA Tomcat server.

Build logs settings to Max as : 3 

Build Environment section 

Check the "Delete workspace before build starts" and "Add timestamps to the Console Output".

Build Section

Select from the dropdown of Add build Step as "Copy Artifacts from another project"

a. Project Name: Package_app

b. Which build : Latest successful build and also select "Stable build only 

c. Artifacts to copy : **/*war 

Post build Actions section 

Select "Deploy war/ear to a container" from the "Add post-build action.

a. WAR/EAR Files: 

b. context path: /

c. Add container: Tomcat 10 [if that is not available you can use Tomcat 9.x also fine]

  i. Add Jenkins credentials: tomcat/tomcat_password which was configured on Tomcat server.

  ii. Tomcat URL: http://PUBLICIP:8080 

Now everything configured for this job. Save the "deploy_app" project configuration. 


Now you can go to the dashboard and see the "QA Deploy" view where we added two Jobs that is package_app, deploy_app.

Trigger the "Build Now" for each Job for now do it for "deploy_app" job to test it to confirm it is working as expected.


Setup for Continuous Deployment

We can create upstream and downstream when they have dependency. 


Go to the package_app project and 

1. Poll SCM in the Build Trigger 

Schedule : * * * * *


in the Post-build Actions section Add post-build action as "Build other projects"

a. Projects to build "Deploy_app" 

b. Tick the choice - Trigger only if build is stable 

You can observe the Downstream job to the "package_app" as "deploy_app" job.

Developer code push simulation - Do some line change in the GitHub repo then Continuous Integration and Continuous deployment will happen automatically. This is the complete solution we can provide to a Java based web application project.

Troubleshoot points

When you run the 'deploy_app' job you can observe that there are some issues where Tomcat don't allow you to deploy the application with strange error messages. Read the Error messages first understand there could be tomcat server text help link. Do the changes as suggested in that text link.

1. Tomcat user file access denied 

cd TOMCAT_HOME/conf; vi tomcat-users.xml 


change mange-gui if it is admin-gui.

after configuration changes you need to restart tomcat (stop and start).


2. text based tomcat error

Inside the TOMCAT_HOME/webapps/manger/META-INF/ there is a file context.xml comment out the 2 lines which have manger value.


 After this action, restart the Tomcat (sudo /opt/tomcat/bin/shutdown.sh; sleep 3; sudo /opt/tomcat/bin/startup.sh)


No comments:

Categories

Kubernetes (24) Docker (20) git (13) Jenkins (12) AWS (7) Jenkins CI (5) Vagrant (5) K8s (4) VirtualBox (4) CentOS7 (3) docker registry (3) docker-ee (3) ucp (3) Jenkins Automation (2) Jenkins Master Slave (2) Jenkins Project (2) containers (2) create deployment (2) docker EE (2) docker private registry (2) dockers (2) dtr (2) kubeadm (2) kubectl (2) kubelet (2) openssl (2) Alert Manager CLI (1) AlertManager (1) Apache Maven (1) Best DevOps interview questions (1) CentOS (1) Container as a Service (1) DevOps Interview Questions (1) Docker 19 CE on Ubuntu 19.04 (1) Docker Tutorial (1) Docker UCP (1) Docker installation on Ubunutu (1) Docker interview questions (1) Docker on PowerShell (1) Docker on Windows (1) Docker version (1) Docker-ee installation on CentOS (1) DockerHub (1) Features of DTR (1) Fedora (1) Freestyle Project (1) Git Install on CentOS (1) Git Install on Oracle Linux (1) Git Install on RHEL (1) Git Source based installation (1) Git line ending setup (1) Git migration (1) Grafana on Windows (1) Install DTR (1) Install Docker on Windows Server (1) Install Maven on CentOS (1) Issues (1) Jenkins CI server on AWS instance (1) Jenkins First Job (1) Jenkins Installation on CentOS7 (1) Jenkins Master (1) Jenkins automatic build (1) Jenkins installation on Ubuntu 18.04 (1) Jenkins integration with GitHub server (1) Jenkins on AWS Ubuntu (1) Kubernetes Cluster provisioning (1) Kubernetes interview questions (1) Kuberntes Installation (1) Maven (1) Maven installation on Unix (1) Operations interview Questions (1) Oracle Linux (1) Personal access tokens on GitHub (1) Problem in Docker (1) Prometheus (1) Prometheus CLI (1) RHEL (1) SCM (1) SCM Poll (1) SRE interview questions (1) Troubleshooting (1) Uninstall Git (1) Uninstall Git on CentOS7 (1) Universal Control Plane (1) Vagrantfile (1) amtool (1) aws IAM Role (1) aws policy (1) caas (1) chef installation (1) create organization on UCP (1) create team on UCP (1) docker CE (1) docker UCP console (1) docker command line (1) docker commands (1) docker community edition (1) docker container (1) docker editions (1) docker enterprise edition (1) docker enterprise edition deep dive (1) docker for windows (1) docker hub (1) docker installation (1) docker node (1) docker releases (1) docker secure registry (1) docker service (1) docker swarm init (1) docker swarm join (1) docker trusted registry (1) elasticBeanStalk (1) global configurations (1) helm installation issue (1) mvn (1) namespaces (1) promtool (1) service creation (1) slack (1)