👋 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
- Install Tomcat server
- Using Jenkins CICD job deploy
Install Tomcat Server on a VM/Cloud instance
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 -versionStep 2: Installing the latest version of Tomcat
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 extractionStep 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.
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 Description : Generate 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
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
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:
Post a Comment