Tuesday, May 17, 2022

Setup pipeline to Test, Build and Deploy java project on Tomcat using Jenkins

Description:  Here I have explained, how to setup pipeline to Test, Build and Deploy project for Tomcat using Jenkins.

Setup:

  1. Jenkins [Installed on Localhost]
  2. Tomcat9 Prod server [44.202.240.14]  
  3. Tomcat9 Staging server [100.27.26.171]
Required Jenkins Plugins
  1. Maven Integration Plugin 
  2. Build Pipeline Plugin
  3. Copy Artifact Plugin 
  4. Deploy to container Plugin 


Procedure: 

First I am going to install required plugins in Jenkins. To install the plugin navigate to Manage Jenkins --> Manage Plugins ---> Available  then search for first Build Pipeline plugin and install with restart

















After installing Build Pipeline, Install Maven Integration plugin





















Install Deploy to container Plugin
















After installing all the require plugins, I am installing maven on jenkins. To install maven navigate to Manage Jenkins --> Global Tool Configuration, Under Maven installation --> click on add Maven as follow. If maven already installed we can defined path. But in our case we are going to install it. 


Install Copy Artifacts: Similar to other plugin install Copy Artifacts plugin



Setup Pipeline: After installing all the plugins and tool, Now I am going to create multiple pipeline for setup. Here I am going to create 4 Projects as follow:
  1. Project-Test                 == In this Project source code from git cloned and test using maven
  2. Project-Build               == In this Project build created after test
  3. Project-Deploy-Stage  == Here deploy build to staging environment with automatic
  4. Project-Deploy-Prod   == Here deploy build to Production environment by manual approval
Project-Test:  To create this project click on New Item give name Project-Test and select Maven Project and create as follow

Add Git source code link for maven testing 



Under Build Section goals and options put test apply and save




Project-Build: After test the project need to build the project, Creating maven project by copy setting from Project-Test build as follow























To build the project, I am using install command under build option as follow and apply- save the project




















To setup java project, We need to archive project to .war file and archive this project to Artifcats. To perform this open Project-Build project navigate to Post-Build Actions

Archive the Artifacts --> Files to archive --> fill as follow --> Apply and save

**/*.war








After creating Build Project, Add build project as child project for Project-Test. To run build after test.
To add child project open Project-Test -- Navigate to configure --> Build Other Project --> Set Project-Build



Deploy Project:
After creating build project, I am going to create 2 Project one for staging and another for Production with free style as follow.

To create Deployment Project, navigate to New Item --> Define Name [Project-Deploy-Stage] --> Free style project --> apply and save.

Perform same step for Project-Deploy-Prod  project to deploy on production.

After creating both the deploy project, I am going to define Project-Deploy-Stage project as child project for Project-Build.  So it will run once project build completed as follow.





















It looks as follow in Project-Deploy-stage.  It will automatically run after build project


Same way, I am going to define Project-Deploy-Prod project as child for Project-Deploy-Stage , but here I am adding manual approval for production. To perform this, open Project-Deploy-Stage project and navigate to configure --> Post-build Actions --> Build Other projects (manual step)






















Copy Artifacts from Build  to Deployment Project:  We have created 4 Projects, now we are going setup copy artifacts plugin which we have installed at early stage and give permission to Project-Deploy-Stage and Project-Deploy-Prod projects to copy  the build from Project-Build

Open Project-Build project, navigate to configure --> Tick on Permission to copy Artifacts -->  Add Project-Deploy-*  entry because both Prod and stage project name started with Project-Deploy-























After provide permission, set copy-artifcats from another project in Project-Deploy-Stage project. To perform it open the project and Add build step --> Select copy artifacts from another project 

Fill details as follow:
Project Name : Project-Build 
Which Build: Latest Build

Apply and save the changes as follow

























After copying artifacts, I am going to archive it to *.war for further [Project-Deploy-Prod] and deploy war file to container [deploy to staging tomcat instance] as http://100.27.26.171:8080 as http://100.27.26.171:8080/techserver application 

Note: Here /techserver is application name for java project in tomcat9 

To perform both the step, open Project-Deploy-Stage project and navigate to Post Build Actions

Select Files to Archive as follow

**/*.war















Add another post-deploy step --> Deploy to container fill details of tomcat environment and credentials


Add Container select Tomcat9  then fill below details 
War file : **/*.war
Context Path: /techserver   [ http://100.27.26.171:8080/techserver  it deployed app like this]
Add tomcat manager credentials as follow:
























Then select the credentials of tomcat 






Create Pipeline View:
Now I am going to create pipeline view to get more picture on above defined stages. To create pipeline view navigate to Dashboard page and click on + symbol 































Select Project-Test as initial Job



Once we save pipeline, it looks as follow
















Once you run the pipeline, it will start processing each linked project. First time it will take some time due to maven installation and configuration. After some time you can see the pipeline view as follow if any issue not found pipeline execution














We have setup upto Project-Deploy-stage Project, Now I am configuring tomcat server credentials  and war file copy from Project-Deploy-stage in Project-Deploy-Prod 

PROD Tomcat URL
: http://44.202.240.14:8080/ 








































Apply and save Production project, Now I am going to run whole pipeline again, For Production we need to approve manually as we have defined early














Prod Application URL:  http://44.202.240.14:8080/techserver/







 Staging Application URL: http://100.27.26.171:8080/techserver/



No comments:

Post a Comment