0% found this document useful (0 votes)
75 views

End To End Deploy in Ubuntu

This document outlines steps to build a CI/CD pipeline with Jenkins using a Tomcat server. It involves creating a Git repository, generating a Spring Boot project, adding code to the repository, creating an EC2 instance, installing Tomcat on the instance, and creating Jenkins jobs for compilation, testing, and deployment. The jobs are configured to trigger builds based on changes to the Git repository and to publish test reports. Upon completion, the application will be automatically compiled, tested, and deployed to Tomcat.

Uploaded by

Raashid Shahab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

End To End Deploy in Ubuntu

This document outlines steps to build a CI/CD pipeline with Jenkins using a Tomcat server. It involves creating a Git repository, generating a Spring Boot project, adding code to the repository, creating an EC2 instance, installing Tomcat on the instance, and creating Jenkins jobs for compilation, testing, and deployment. The jobs are configured to trigger builds based on changes to the Git repository and to publish test reports. Upon completion, the application will be automatically compiled, tested, and deployed to Tomcat.

Uploaded by

Raashid Shahab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

1

Building a CI/CD Pipeline with Jenkins using Tomcat


server complete project

Step 1: Creating a Git repository for the webapp


● Log in to your Github account.
● Click on the plus icon next to the profile picture and select New repository from the
drop-down menu.

● Fill the required fields in the create repository form.


2

● Click on the Create Repository button.


● Click on the Clone or download button and copy the URL.

Step 2: Generating a spring boot project


● Go to start.spring.io/
● Select Maven as the project type.
● Fill Group and Artifact with appropriate values. For example, com.Bookzy and webapp.
3

● Click on Generate Project.

● The generated skeleton project should be downloaded as a zip file.

Step 3: Adding the code for the webapp to the repository


● Open the terminal and navigate to an appropriate location.
● Run git clone [URL] to clone the repository.
● Unzip the downloaded spring boot project to the cloned repository.

● Commit the changes to the remote SCM.


● Run git add.
● Run git commit -m "Add project skeleton"
● Run git push -u origin master.
4

Step 4: Creating an EC2 instance


● Log in to the AWS lab account provided. You will then be able to see the following
screen:

● Click on Services at the top left to view the drop-down list of resources.
● Click on EC2 under the Compute menu from the drop-down list.
5

● Click on the Launch Instance button and select Launch Instance from the menu.

● Choose an Amazon Machine Image (AMI) from the list of AMIs and click on Select.
6

● Choose an Instance Type and click Review and Launch.

● Click on Launch.
7

● In the pop-up menu, select Create a new key-value pair.


● Click on Download Key. You’ll need this key to SSH to the VM later.

● Click on Launch.
● Navigate to the security groups console.
8

● Add a rule to the security group to which the instance belongs to allow SSH, with the
following settings:

Type: SSH

Protocol: TCP

Port Range: 22

Source: Anywhere 0.0.0.0/0

● Add a rule to the security group to which the instance belongs to allow http traffic to
port 8081, with the following settings:

Type: Custom TCP Rule

Protocol: TCP

Port Range: 8081

Source: Anywhere 0.0.0.0/0


9

Step 5: Installing Tomcat on EC2


● Open the terminal.
● Navigate to the location where the AWS key is stored.
● Make the key file executable with the command chmod 400 <key-name>.pem
● SSH to the EC2 instance with the command sudo ssh -i <key-name>.pem
ubunutu@<public-dns>
10

● Run the following commands to install Java and Tomcat and grant permissions to the
Tomcat user:

sudo apt-get update

sudo apt-get install default-jdk

sudo groupadd tomcat

sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

cd /tmp

curl -O https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.64/bin/apache-tomcat-
9.0.64.zip

sudo mkdir /opt/tomcat

sudo apt-get install unzip

unzip apache-tomcat-*.zip

sudo mv apache-tomcat-8.5.54/*   /opt/tomcat/

sudo ln -s /opt/tomcat /opt/tomcat/latest

sudo chown -R tomcat: /opt/tomcat

sudo sh -c 'chmod +x /opt/tomcat/latest/bin/*.sh'

echo "export CATALINA_HOME="/opt/tomcat"" >> ~/.bashrc

source ~/.bashrc

sudo vi /etc/systemd/system/tomcat.service

--------------------------------------------------------------------------------

[Unit]

Description=Tomcat 8.5 servlet container


11

After=network.target

[Service]

Type=forking

User=tomcat

Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/default-java"

Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"

Environment="CATALINA_BASE=/opt/tomcat/latest"

Environment="CATALINA_HOME=/opt/tomcat/latest"

Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"

Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

ExecStart=/opt/tomcat/latest/bin/startup.sh

ExecStop=/opt/tomcat/latest/bin/shutdown.sh

[Install]

WantedBy=multi-user.target

sudo systemctl daemon-reload

sudo systemctl start tomcat

sudo systemctl status tomcat

sudo systemctl enable tomcat

sudo ufw allow 8080/tcp

###NOW CHECK THE PUBLICIP:8080 ####

sudo systemctl start tomcat

sudo systemctl restart tomcat


12

sudo systemctl stop tomcat

● Find the path to Java with the following command:

sudo update-java-alternatives -l

● Open the tomcat.service file with the command sudo nano


/etc/systemd/system/tomcat.service
● Add the following content to the file. Replace the JAVA_HOME value with the value
obtained in the previous step:

Description=Apache Tomcat Web Application Container

After=network.target

[Service]

Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64/jre
13

Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid

Environment=CATALINA_HOME=/opt/tomcat

Environment=CATALINA_BASE=/opt/tomcat

Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'

Environment='JAVA_OPTS=-Djava.awt.headless=true
-Djava.security.egd=file:/dev/./urandom'

ExecStart=/opt/tomcat/bin/startup.sh

ExecStop=/opt/tomcat/bin/shutdown.sh

User=tomcat

Group=tomcat

UMask=0007

RestartSec=10

Restart=always

[Install]

WantedBy=multi-user.target
14

● Open the server.xml file with the command sudo nano conf/server.xml.
● Add address="0.0.0.0" to the connector and save the file.

● Open the user’s file with the following command: sudo nano
/opt/tomcat/conf/tomcat-users.xml
● Add the following lines right before the last line and save the file:

<user username="tomcatmanager" password="password" roles="manager-gui"/>

<user username="deployer" password="password" roles="manager-script"/>


15

● By default, newer versions of Tomcat restrict access to the Manager and Host Manager
apps to connections coming from the server itself. Since we are installing on a remote
machine, you will need to remove or alter this restriction. To change the IP address
16

restrictions on these, open the appropriate context.xml files with the following
commands:

sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml and

sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml

● Comment out the IP address restriction to allow connections from anywhere.

● Start and verify the Tomcat server with the following commands:

sudo systemctl daemon-reload

sudo /opt/tomcat/bin/startup.sh

sudo systemctl status tomcat

● Allow traffic at 8081 with the following command:

sudo ufw allow 8081


17

● Navigate to http://<Public DNS (IPv4)>:8081 to view the tomcat server.

When you click on manage app you will get an error

Cd /opt/tomcat
the file <tomcat>/webapps/manager/META-INF/context.xml has been adjusted:

<Context antiResourceLocking="false" privileged="true" >


<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
</Context>
18

Change this file to comment the Valve:

<Context antiResourceLocking="false" privileged="true" >


<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
-->
</Context>

Step 6: Creating a freestyle job for compilation in Jenkins


● Go to the Jenkins dashboard.
● Click on New Item.
● Enter a name for your build job.
● Select Freestyle project as the build job type.

● Click OK.
● On the configuration page, scroll down to the Source Code Management section.
● Select Git in SCM.
● Add the repository URL.
19

● Scroll down to the Build Trigger and pick the poll SCM option. Enter the expression for
nightly builds.

● Scroll down to the Build section.


● Click on the Add build step button.
● On the available options, click on the Invoke top level Maven target and enter compile
as goal.
20

● Click on Save.

● Click on Build Now in the project window to test the job.


21

Step 7: Creating a freestyle job for testing in Jenkins


● Go to the Jenkins dashboard.
● Click on New Item.
● Enter a name for your build job.
● Select Freestyle project as the build job type.
22

● Click OK.
● On the configuration page, scroll down to the Source Code Management section.
● Select Git in SCM.
● Add the repository URL.

● In the Build Trigger tab, click on build after other projects are built.
● Type the name of the previous project and select the Trigger only if the build is stable
option.
23

● Scroll down to the Build section.


● Click on the Add build step button.
● On the available options, click on the Invoke top level Maven target and enter test as the
goal.

● Scroll down to the Post-build Actions section.


24

● Click on Add post-build action and select Publish JUnit test result report. Fill the fields
with appropriate values.

● Click on Save.
● Click on Build Now in the project window.
● Jenkins will now build your pipeline and output the logs.

● The JUnit test reports can be viewed under the Test Result tab.
25

Step 8: Creating a freestyle job for deployment in Jenkins


● Go to the Jenkins dashboard.
● Click on Manage Jenkins and select Manage Plugins.

● From the available plugins, install Deploy to container.


26

● Click on Install without restart.

● Go to the Jenkins dashboard.


● Click on New Item.
● Enter a name for your build job.
● Select Freestyle project as the build job type.
27

● Click OK.
● On the configuration page, scroll down to the Source Code Management section.
● Select Git in SCM.
● Add the repository URL.
28

● In the Build Trigger tab click on the build after other projects are built.
● Type the name of the previous project and select the Trigger only if the build is stable
option.

● Scroll down to the Build section.


● Click on the Add build step button.
● On the available options, click on the Execute shell and enter mvn package as command.
29

● Scroll down to the Post-build Actions section.


● Click on the Add post-build action button.
● On the available options, click on the Deploy war/ear to container.

● Add the credentials.


30

● Fill the required parameters for the plugin. Use the following screenshot as a reference:

● Choose the Context Path in which the application should be installed. It would rename
the WAR file before deploying to the server and thereby the application context root
would be changed.

● Click on the Add post-build action and select Sent Email Notification. Fill the fields with
appropriate values.
31

● Click on Save.
● Click on Build Now in the project window.
● Jenkins will now build your job and output the logs.
● Navigate to the URL on your browser to view your webapp.
● Trigger the first freestyle job to start the pipeline.

You might also like