? CICD DevOps Project
? CICD DevOps Project
Agenda:
•Setup Jenkins
•Setup & Configure Maven and Git
•Setup Tomcat Server
•Integrating GitHub, Maven, and Tomcat Server with Jenkins
•Create a CI and CD job
•Test the deployment
Prerequisites:
•AWS Account
•Git/ Github Account with the Source Code
•A local machine with CLI Access
Step 1: Setup Jenkins Server on AWS EC2 Instance
For this demo, we will select Amazon Linux 2 AMI which is free tier eligible.
Choose an Instance Type. Here you can select the type of machine, number of
vCPUs, and memory that you want to have. Select t2.micro which is free-tier
eligible.
For this demo, we will select an already existing key pair. You can create new
key pair if you don’t have:
Now under Network Settings, Choose the default VPC with Auto-assign public
IP in enable mode. Create a new Security Group, provide a name for your
security group, allow ssh traffic, and custom default TCP port of 8080 which is
used by Jenkins.
Rest of the settings we will keep them at default and go ahead and click
on Launch Instance
On the next screen you can see a success message after the successful
creation of the EC2 instance, click on Connect to instance button:
Now connect to instance wizard will open, go to SSH client tab and copy the
provided chmod and SSH command:
Open any SSH Client in your local machine, take the public IP of your EC2
Instance, and add the pem key and you will be able to access your EC2
machine in my case I am using MobaXterm on Windows:
After logging in to our EC2 machine we will install Jenkins following the
instructions from the official Jenkins website:
https://pkg.jenkins.io/redhat-stable/
Output:
Now let's install Jenkins with the below command as shown in the output:
After successful installation Let's enable and start Jenkins service in our EC2
Instance:
Now let’s try to access the Jenkins server through our browser. For that take the
public IP of your EC2 instance and paste it into your favorite browser and
should see something like this:
To unlock Jenkins we need to go to the path
/var/lib/jenkins/secrets/initialAdminPassword and fetch the admin password to
proceed further:
Now on the Customize Jenkins page, we can go ahead and install the suggested
plugins:
Now we can create our first Admin user, provide all the required data and
proceed to save and continue.
To install the GitHub plugin lets go to our Jenkins Dashboard and click on
manage Jenkins as shown:
On the next page, click on manage plugins:
Now in order to install any plugin we need to select Available Plugins, search
for Github Integration, select the plugin, and finally click on Install without
restart as shown below:
Now we will set up Environment Variables for our root user in bash_profile in
order to access Maven from any location in our Server Go to the home directory
of your Jenkins server and edit the bash_profile file as shown in the below
steps:
In the .bash_profile file, we need to add Maven and Java paths and load these
values.
With this setup, we can execute maven commands from anywhere on the
server:
Now we need to update the paths where Java and Maven have been installed in
the Jenkins UI. We will first install the Maven Integration Plugin as shown below:
After clicking on Install without restart, go again to manage Jenkins and
select Global Tool configuration to set the paths for Java and Maven.
For JAVA:
For MAVEN:
Click on save and hence we have successfully Integrated Java and Maven with
Jenkins.
Now let’s first download the Tomcat Server and then install it in the /opt
directory:
mv apache-tomcat-9.0.74 tomcat
Now move into the tomcat directory, then to /bin directory there we need to
run the startup.sh script to run the tomcat services on our Server.
Now in order to make sure that tomcat server Manager App is accessible from
anywhere we need to make some changes in a couple of files as initially after
the installation the tomcat service Manager App service would be accessible
only on the local host on which it is installed:
Now we will update the context.xml file to allow access to Tomcat Server from
anywhere apart from the localhost.
First, we will search for the context.xml file in the tomcat directory which is
present twice as shown below:
Then after opening the files, we need to comment out a line as shown in the
below screenshot:
Update the user’s information in the tomcat-users.xml file goto tomcat home
directory and Add the below users to conf/tomcat-users.xml file:
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
Output:
Now we again to restart the services, to make things easier let’s create link
files for tomcat startup.sh and shutdown.sh
For that add the below lines and restart the tomcat service:
ln -s /opt/tomcat/bin/startup.sh /usr/local/bin/tomcatup
ln -s /opt/tomcat/bin/shutdown.sh /usr/local/bin/tomcatdown
Output:
Now we can access the Manager App on the tomcat server by providing a
username: tomcat and password as s3cret
After clicking on the Manager App, provide the credentials and we would be
able to see the page below:
Click on System:
Then select Global credentials (unrestricted):
In this step we would first create a new job in Jenkins, provide the URL of our
GitHub repository from where the source code would be pulled, then Maven will
be used to build the project and finally, we will deploy the project on the
tomcat server all using the CI server named Jenkins.
Now let’s configure our new job. Under General provide the description of your
choice:
Under Source Code Management, paste the URL of your GitHub code
repository, you can leave the credentials as blank as this is a public repository
and mention the branch of your repo, in my case it’s Master.
Under Build Settings, under Root POM mention the pom.xml which should be
present in our Git code repository. Under Goals and Options. provide clean
install package name which will install the necessary packages and install
them in our local repository.
Now we need to deploy our code, so under Build Settings, select Deploy
war/ear to a container, and then under Post-build Actions provide the
necessary details like a path to the war file, tomcat server credentials, and
URL, as shown in the screenshot:
Finally, click on Apply and Save.
Now let’s finally Build our code which would eventually copy the Artifacts to the
tomcat server.
After clicking on Build Now if we check the console output we can notice that
the Build is successful and Jenkins was successfully able to deploy the WAR file
onto the tomcat server as shown below:
If we access our tomcat server Manager App we can notice the presence of a
new directory named /webapp:
and if we click on the webapp link, it will display the below page:
Hence we have successfully built and deployed our code. However, in this
scenario, we have manually built the code and in case there is any change in
our code we need to again manually click on the Build Now option in Jenkins
and start the process all over again.
Jenkins has provided many options to automate the build trigger process and
one of them is Poll SCM.
What is polling the SCM?
“Poll SCM” polls the SCM periodically for checking if any changes/ new commits
were made and shall build the project if any new commits were pushed since
the last build.
Go to the previous job we created in the previous steps and click on configure:
Under Build Triggers, Select the Poll SCM option and set the schedule for the
poll to happen. Here we will select the poll that should check every minute,
every hour, every day of the month, month, and every day of the week. Click
on Apply and Save to proceed.
Now do some changes in your source code and without our intervention the
Jenkins build process should be triggered automatically and build the code.
After making some changes in my code the build got triggered and checking
the console output shows the build has been started by SCM and is successful
rather than by the Admin (in an earlier case):
If we access our Tomcat server from our browser we should see the new
changes we did:
Conclusion
In this blog, we learned how to build a Java web app using GitHub as our SCM,
Jenkins as our CI tool, Maven as our build tool, and finally deploying on a
remote Tomcat Server.