Monday 11 March 2019

Devops Project-1 -Create build artifacts using Jenkins and deploy those using also by Jenkins by Raj Gupta


So both CI(build) and CD(deploy) part are done by using Jenkins

Take two AWS EC2 REHL server
1st for Webserver in which install tomcat
2nd for jenking server in which install jenking, git, ,maven and java

Now install jenking on Jenking ec2 server after installation done logging into webpage with public IP of server
After that do configuration of git, ,maven and java path in Jenking

Setup Git on jenkins console

·         Install git plugin without restart
    • Manage Jenkins > Manage Plugins > available > GitHub (search for it in Filter and click on it then click on Install without restart)
After installation go back to Manage Jenkins once again and do below

·         Configure git path
    • Manage Jenkins > Global Tool Configuration > Git
Left the value as default value
Name                        Default
Path to Git executable       git
Then click on apply and save





Setup Java on jenkins console

·         Configure JDK path
    • Manage Jenkins > Global Tool Configuration > JDK
Then click on Add JDK and unclick on Install automatically
And fill below row


JDK Name                java
JAVA_HOME           /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64 (this address
you can get by running command echo $JAVA_HOME    this will give path of java where it installs)

Then click on apply and save

Setup maven on jenkins console
  • Install maven plugin without restart
    • Manage Jenkins > Manage Plugins > available > Maven Invoker(search for it in Filter and click on it then click on Install without restart)
After installation go back to Manage Jenkins once again and do below
  • Configure java path
    • Manage Jenkins > Global Tool Configuration > Maven
Then click on Add Maven and unclick on Install automatically
And fill below row

Name                            Maven
MAVEN_HOME            /opt/maven/apache-maven-3.6.0 (this address
you can get by running command echo $ M2_HOME    this will give path of java where it installs)
Then click on apply and save
If you are not able to see Maven project tab after clicking New Item, then
►To add Maven Project in New Item Page,
just go to Manage Jenkins >> Manage Plugin,
click on Available Tab,
In the filter box enter "Maven plugin" and you will get search result as "Unleash Maven Plugin",
√ enable the check-box, click on "Download now and install after restart"

After Maven install on jenkin reboot the ec2 server

reboot

                 Real project starts from here
Now go to New Item then enter the Item name and click on Maven project after that click ok 

In next tab under Source code management select git and for Repository URL give the github URL where your code is there like below
Repository URL           https://github.com/rajkumargupta14/hello-world.git
Credentials                 None (because only for private github Credentials need to give)

In Build tab
Root POM                       pom.xml ( this file need to keep in github)
Goals and options          clean install package    ( give this option in this)

After that apply and save
Then Click on Build Now radio button

[root@ip-172-31-84-214 ~]# cd /var/lib/jenkins/workspace/
[root@ip-172-31-84-214 workspace]# ls
Test
[root@ip-172-31-84-214 workspace]# cd Test/
[root@ip-172-31-84-214 Test]# ls
Dockerfile  pom.xml  README.md  regapp-deploy.yml  regapp-service.yml  server  webapp
[root@ip-172-31-84-214 Test]# cd webapp/
[root@ip-172-31-84-214 webapp]# ll
total 4
-rw-r--r-- 1 jenkins jenkins 1141 Sep 12 07:51 pom.xml
drwxr-xr-x 3 jenkins jenkins   18 Sep 12 07:51 src
drwxr-xr-x 5 jenkins jenkins   76 Sep 12 07:51 target
[root@ip-172-31-84-214 webapp]# cd target/
[root@ip-172-31-84-214 target]# ll
total 4
drwxr-xr-x 2 jenkins jenkins   28 Sep 12 07:51 maven-archiver
drwxr-xr-x 2 jenkins jenkins    6 Sep 12 07:51 surefire
drwxr-xr-x 4 jenkins jenkins   54 Sep 12 07:51 webapp
-rw-r--r-- 1 jenkins jenkins 2868 Sep 12 07:51 webapp.war
[root@ip-172-31-84-214 target]#


Till now all JAR and WAR files are created for java project, so our CI part are done …. Now time for CD part for that go to second ec2 server (web server)
Install Tomcat on the second server after installation we need to do below two work in Jenking


1.     Install the pugging Deploy to container in jenking 

    • Manage Jenkins > Manage Plugins > available > Deploy to container (search for it in Filter and click on it then click on Install without restart)




2.     Second give tomcat id and password to give go to below path


Then fill the below….





Username and password you can give username of user you have created on tomcatcat server

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

For ID and Description you can give anything..

Now in Post-build Actions select Deploy war/ear to a container   then fill the below row
WAR/EAR files-     **/*.war
Context path-     leave it empty
Containers-        select Tomcat 8.x
Credentials:-     select from dropdown whatever you have crated
Tomcat URL:    http://3.91.193.216:8080/

Then click on apply and save

Now in Build Triggers tick the box Poll SCM   then give below to run ever minute
 Schedule-       */1 * * * *
Then click on apply and save.

Now do same changes on github then commit it so that save thing will be show in tomcat server browser

Go to github account where you have kept you code


then click on edit this file and do some changes in the index.jsp file

then enter save value in Commit changes like updated file or any other value you want

then you can click on commit changes button
when you click on commit changes button in github then after 1m automatically build will be start in jankins and code changes will be deploy in tomcat sever.

Now in broser give the below URL
http://3.91.193.216:8080/webapp/      then you will see the same output whatever changes you have done in code for app.
We need to give webapp in last because this app will be deploy in webapps folder of tomcat server with name webapp see the below details.


[ec2-user@ip-172-31-85-242 ~]$ sudo -i
[root@ip-172-31-85-242 ~]# ls
anaconda-ks.cfg  app  original-ks.cfg
[root@ip-172-31-85-242 ~]# cd app
[root@ip-172-31-85-242 app]# ls
apache-tomcat-8.5.37  apache-tomcat-8.5.37.tar.gz
[root@ip-172-31-85-242 app]# cd apache-tomcat-8.5.37
[root@ip-172-31-85-242 apache-tomcat-8.5.37]# ls
bin  BUILDING.txt  conf  CONTRIBUTING.md  lib  LICENSE  logs  NOTICE  README.md  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@ip-172-31-85-242 apache-tomcat-8.5.37]# cd webapps/
[root@ip-172-31-85-242 webapps]# ls
docs  examples  host-manager  manager  ROOT  webapp  webapp.war
[root@ip-172-31-85-242 webapps]# ls -ltr
total 12
drwxr-x---.  3 root root 4096 Jan 25 08:49 ROOT
drwxr-x---. 14 root root 4096 Jan 25 08:49 docs
drwxr-x---.  6 root root   83 Jan 25 08:49 examples
drwxr-x---.  5 root root   87 Jan 25 08:49 host-manager
drwxr-x---.  5 root root  103 Jan 25 08:49 manager
-rw-r-----.  1 root root 2476 Jan 25 10:08 webapp.war
drwxr-x---.  4 root root   54 Jan 25 10:08 webapp
[root@ip-172-31-85-242 webapps]#










No comments:

Post a Comment