Introduction
This is a simple CI/CD pipeline demo, we will be using the following tools to create of CI/CD pipeline.
EC2 instances are virtual machines provided by Amazon Web Services that can be rented to run applications and services in the cloud. They offer scalable computing capacity and flexible configuration options, with pricing based on usage.
GitHub is a web-based platform for version control and collaborative software development. It provides a centralized location for software developers to store, share, and manage code repositories and collaborate with other developers.
Apache Tomcat is an open-source web server and servlet container that is used to serve Java web applications. It provides a runtime environment for Java-based web applications and supports the Java Servlet, JavaServer Pages, and WebSocket technologies.
Jenkins is an open-source automation server that is used for continuous integration and continuous delivery (CI/CD) of software applications. It allows developers to automate the building, testing, and deployment of code changes across multiple environments.
Maven is a build automation tool that is used primarily for Java-based projects. It provides a way to manage project dependencies, build and package applications, and generate documentation. Maven uses a project object model (POM) file to describe the project structure and dependencies.
GitHub webhook is a mechanism for triggering automatic updates or actions in response to events that occur within a GitHub repository. It enables developers to integrate their GitHub workflows with external tools and services, such as continuous integration systems, chat platforms, or issue trackers, by sending a request to a specified URL when a repository event occurs.
What will we perform?
EC2 Instance Creation
Install Java on EC2 Instance
Install Jenkins on EC2 Instance
Install and Setup Apache on EC2 Instance
Install Maven on to EC2 Instance
Fork GitHub Repository
Configure Jenkins
Setup a .war file Job
Setup github webhook
Test auto build for multiple commit
EC2 Instance Creation
Create EC2 Instance
Note: I recommend increasing the CPU and memory of the EC2 Instance. For this demo I will choose “t2.Medium” and for the storage I will be using 30 GB.
Once EC2 Instance is created we will install Java.
We will connect to the Instance using our Mac Terminal
We will update of EC2 instance
Install Java on EC2 Instance
sudo apt update
sudo apt install default-jdk (this command to install latest java package)
java -version
Now we will install Jenkins
Install Jenkins on EC2 Instance
We will use the following commands to install the jenkins. We must run the commands prior to installing the jenkins.
sudo apt update
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt update
sudo apt install jenkins
Once the Jenkins is installed, we will start the service and check the status of the jenkins.
sudo systemctl start jenkins
service jenkins status
Install and Setup Apache on EC2 Instance
We will install the apache tomcat and configure the apache tomcat, but before that lets create a specific folder in the /opt directory
cd /opt
sudo mkdir demo-apache
We need to give the ubuntu user of EC2 instance ownership of the directory demo-apache so we the ubuntu user can start and stop the apache service.
sudo chown -R ubuntu:ubuntu demo-apache
cd demo-apache
We will now install the .tar file of apache to the demo-apache folder.
Note: Apache tend to have new .tar packages it is always recommended to go to the actual url to find out the most recent package to work with. If you do find the different package note there might be different versions and you will have the make the change according to the package you are working with.
Tomcat Url : https://downloads.apache.org/tomcat/
Click on the latest version.
Navigate to the latest version number
Navigate to the bin and locate file .tar.gz for ubuntu linux
wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.72/bin/apache-tomcat-9.0.72.tar.gz
We will unzip the apache file we installed from the server.
We need to get the file information by using
ls -la
tar zxvf apache-tomcat-9.0.72.tar.gz
cd apache-tomcat-9.0.72
Now will make the following adjustment to the some file in the apache to serve our demo
We will add first go about access the tomcat-users.xml file so we can access the tomcat users
vim conf/tomcat-users.xml
The below information should be added between two red arrows as shown in the image in the tomcat-users.xml file
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="admin" password="admin" roles="manager-gui, manager-script, manager-jmx, manager-status"/>
<user username="deployer" password="deployer" roles="manager-script"/>
<user username="tomcat" password="s3cret" roles="manager-gui"/>
We will link the tomcat showdown.sh to tomcatdown and tomcat startup.sh to tomcatup so we don’t have to navigate to the path to do so.
ln -s /opt/demo-apache/apache-tomcat-9.0.72/bin/startup.sh /usr/local/bin/tomcatup
ln -s /opt/demo-apache/apache-tomcat-9.0.72/bin/shutdown.sh /usr/local/bin/tomcatdown
Now we will make necessary changes to a couple more files to access the tomcat from the web.
We will navigate to the context.xml file and comment out the value part as shown in the image.
vim /opt/demo-apache/apache-tomcat-9.0.72/webapps/manager/META-INF/context.xml
vim /opt/demo-apache/apache-tomcat-9.0.72/webapps/host-manager/META-INF/context.xml
We also need to make changes in the server.xml file under the conf directory. We will be changing port from 8080 to 8090.
We will need to stop and restart the tomcat for changes to take places.
tomcatdown
tomcatup
Also after change the port we will have to add the inbound rules in out AWS security group as well
Port 8090 assigned to apache tomcat
Port 8080 assigned to jenkins
If we use the netstat command we can see the port 8080 and port 8090 being used.
netstat -tulpn
Login to the Tomcat Manager app
To access tomcat you will need EC2 Instance’s Public IP and port defined as 8090
For this demo it will be like this
You will be asked to provide credentials, the credentials I used were defined above when we made change into the tomcat-users.xml file
Username: tomcat
Password: s3cret
Install Maven on to EC2 Instance
To install maven on EC2 Instance. We will be using following commands.
We will need to extract maven in directory /opt/demo-apache
cd /opt/demo-apache
We need to install maven in it now.
Navigate to the maven website.
https://maven.apache.org/download.cgi
Get the bin.tar.gz file link
wget https://dlcdn.apache.org/maven/maven-3/3.9.0/binaries/apache-maven-3.9.0-bin.tar.gz
We will now need to extract the zip (tar) file now.
tar -xvzf apache-maven-3.9.0-bin.tar.gz
We jump in the directory we just extracted.
cd apache-maven-3.9.0
We now need to setup the path in jenkins for maven
pwd
Outout :/opt/demo-apache/apache-maven-3.9.0
We need to go to the root/ to make an entry for maven in the .profile file.
Save this is vim /root/.profile
M2_HOME='/opt/demo-apache/apache-maven-3.9.0'
PATH="$M2_HOME/bin:$PATH"
export PATH
Fork GitHub Repository
For this demo I will be using the following repository which I forked and made publicly available in my github account.
https://github.com/shivalkarrahul/Innovecture.git
Configure Jenkins
We will access the jenkins using EC2 Instance’s public ip address and port 8080 defined.
For this demo we will be using the following to access the jenkins
To get the password we will have to the path it shows on the initial webpage
cat /var/lib/jenkins/secrets/initalAdminPassword
If it doesn’t work do it one at a time.
cd /var/lib
cd jenkins
cd secrets
cat initialAdminPassword
Enter the secret key in the jenkin we “cat” just now
Skip installing the plugin and start using jenkins.
Change the password for the jenkins to make it easy on yourself
Navigate to top right corner
Click on Admin > Configure > scroll down to password and change the password
Once you change the password you will be logged out and log back in using new password.
Install Jenkins Plugin
Navigate to the Manage Jenkins in the left panel > Manage Plugins.
Click on Available plugin tab and type Git Authentication and install the plugin without restart.
We will do the same for the Maven Integration Plugin and Deploy to container plugin without restart as well.
Setup a .war file Job
We are ready to create our first sample job now.
To create the job we will click on “new Item” in jenkins web portal
Enter an item name and click on Freestyle project, than click Ok
We will enter the description of the job and select the Github project.
Enter the url or the git hub repository which we forked earlier
Forked Repository link: https://github.com/Arafique458/Innovecture.git
Under the Source Code Management in the Repository URL, we will enter the forked repository we copied earlier
We move to the github hook trigger
Select “GitHub hook trigger for GitSCM polling” under Build Triggers tab
We also have to add the build steps
Navigate to the build environment section click on add build step and select "Invoke top-level Maven targets"
Set the Goals “clean package”
Click Apply and save the change.
We now specifiy path for the maven aswell.
Go to Dashboard in jenkins
Click on Manage Jenkins, navigate to Global Tool Configuration and click on Add Maven.
Name = maven
MAVEN_HOME= /opt/demo-apache/apache-maven-3.9.0
Click Apply and Save.
Click on the Sample-Job and Click on Configure
Navigate to the Build Steps and click on drop down and select maven under maven version.
Click Apply and Save
We now go to the job and click on Build now to build our sample project.
Console output.
Post Deployment build Actions now
Go back to the job and click on Configure.
Scroll down to Post-Build Action
Select “Deploy war/ear to a container” from the drop down menu
Enter the default for files > **/*.war
Enter the Tomcat url > Your tomcat server url
Click Apply and Save.
We have to enter the apache deployer credentials
Navigate to dashboard > Admin > Credentials > User > Stores from parent User: admin > Global Credentials > create new credentials
Note: This was defined in the apache server.xml file as well
Username: deployer
Password: deployer
ID: tomcat_deployer
Description: tomcat_deployer
We come back to the Post-Build Actions
Select the credentials from the drop down menu.
Click on Build now to check if the file gets pushed to apache tomcat.
The manual build was successful too.
Go to the Job again and navigate to the configure option.
Scroll down to the Build Triggers
Select the Poll SCM option to add the schedule
For this demo we will setup for every 2 minutes
*/2 * * * *
Click Apply and Save
Setup github webhook
Now we need to login to our github account to set up the webhook for the repository we are working with.
Click on the Settings tab and navigate to the web hook in the left panel.
Enter the jenkins web address for the payload url just like so.
Note: I have an access key which I will be using to access my git repository through EC2 terminal.
Now we will head to our Terminal window and clone the specific git repository and make some changes in it.
We use the following command to clone the repository and make changes to the repository.
git clone https://github.com/Arafique458/Innovecture.git
cd Innovecture
vim README.md
We make change in readme file and save the changes
We now configure our git credentials
git config user.name “arafique458” # to configure username
git config user.email “arafique458@gmail.com” # to configure the email
git add . # to add the changes we made in the README.md
git commit -m “Comment” # to commit the changes
git push # to push the changes to the github repository
You will be prompted to add your username and password.
I added the username and the access token (I created this in the github)
Changes were made. The Jenkins job should detect the change and run the job.
Test auto build for multiple commit
Now will make changes to the code to reflect the change in the .war file and show the change on the webpage.
Before Changes.
We made the change to the index.jsp file now
vim src/main/webapp/index.jsp
After making the change we apply the changes
git add .
git commit -m "Added the Head and modified the code"
git push
We looked at the jenkins and jenkin job detected the change and made the change according
After the Changes
We now have working simple CI/CD pipeline
Comments
Post a Comment