jenkins+Github+tomcat 配置实例

安装git

[root@git-server ~]# yum -y install git 

创建密钥

[root@git-server ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:YGXQZ06y7IUBJDaMGm+n+7whYvXM93ecquwBCRdbiNw root@git-server
The key's randomart image is:
+---[RSA 2048]----+
|   o=.*=+.       |
|. ...= EB +      |
| +   .o+ X       |
|. o ..o.= o      |
| . +   +S.       |
|  o +   o        |
|.....+ . .  . .  |
|...o .. o .. +   |
|   .+.  .=o.o    |
+----[SHA256]-----+

给后端tomcat发送公钥

[root@git-server ~]# ssh-copy-id -i 192.168.234.102

上传安装包

[root@git-server ~]# ls
apache-maven-3.5.4-bin.tar.gz  apache-tomcat-8.5.45.tar.gz  jdk-8u211-linux-x64.tar.gz  plugins.tar.gz

配置JDK

[root@git-server ~]# tar xf jdk-8u211-linux-x64.tar.gz -C /usr/local/
[root@git-server ~]# cd /usr/local/
[root@git-server local]# mv jdk1.8.0_211/ java
[root@git-server local]# ls
bin  etc  games  include  java  lib  lib64  libexec  sbin  share  src

配置tomcat

[root@git-server ~]# tar xf apache-tomcat-8.5.45.tar.gz -C /usr/local/
[root@git-server local]# mv apache-tomcat-8.5.45 /usr/local/
[root@git-server ~]# cd /usr/local/
[root@git-server local]# mv apache-tomcat-8.5.45/ tomcat
[root@git-server local]# ls
bin  etc  games  include  java  lib  lib64  libexec  sbin  share  src  tomcat

配置maven

[root@git-server ~]# tar xf apache-maven-3.5.4-bin.tar.gz -C /usr/local/java/
[root@git-server ~]# cd /usr/local/java/
[root@git-server java]# mv apache-maven-3.5.4/ maven 
[root@git-server java]# ls
bin        include         jre  LICENSE  maven        release  THIRDPARTYLICENSEREADME-JAVAFX.txt
COPYRIGHT  javafx-src.zip  lib  man      README.html  src.zip  THIRDPARTYLICENSEREADME.txt

配置环境变量

[root@git-server ~]# vim /etc/profile
JAVA_HOME=/usr/local/java
MAVEN_HOME=/usr/local/java/maven
PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL JAVA_HOME MAVEN_HOME
[root@git-server ~]# source /etc/profile

测试jdk和maven

[root@git-server ~]# java -version 
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
[root@git-server ~]# mvn -v 
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-18T02:33:14+08:00)
Maven home: /usr/local/java/maven
Java version: 1.8.0_211, vendor: Oracle Corporation, runtime: /usr/local/java/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-693.el7.x86_64", arch: "amd64", family: "unix"

上传jenkins

[root@git-server ~]# cd /usr/local/tomcat/webapps/
[root@git-server webapps]# rm -rf * 
[root@git-server webapps]# rz
[root@git-server webapps]# ls
jenkins.war
[root@git-server webapps]# /usr/local/tomcat/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

页面配置

jenkins+Github+tomcat 配置实例_第1张图片

[root@git-server ~]# cat /root/.jenkins/secrets/initialAdminPassword
c6ae6324061e4c978691f2fe9aa19f3d

要查看自己目录密码文件
jenkins+Github+tomcat 配置实例_第2张图片
选择安装插件
jenkins+Github+tomcat 配置实例_第3张图片
这一步可以用自带得到插件 这一步会很慢

[root@git-server .jenkins]# rm -rf plugins/
[root@git-server ~]# tar xf plugins.tar.gz -C /root/.jenkins/

重启tomcat
jenkins+Github+tomcat 配置实例_第4张图片
jenkins+Github+tomcat 配置实例_第5张图片

jenkins+Github+tomcat 配置实例_第6张图片
jenkins+Github+tomcat 配置实例_第7张图片
jenkins+Github+tomcat 配置实例_第8张图片
系统设置
jenkins+Github+tomcat 配置实例_第9张图片
jenkins+Github+tomcat 配置实例_第10张图片
jenkins+Github+tomcat 配置实例_第11张图片
jenkins+Github+tomcat 配置实例_第12张图片
jenkins+Github+tomcat 配置实例_第13张图片
jenkins+Github+tomcat 配置实例_第14张图片

jenkins+Github+tomcat 配置实例_第15张图片
jenkins+Github+tomcat 配置实例_第16张图片
jenkins+Github+tomcat 配置实例_第17张图片
jenkins+Github+tomcat 配置实例_第18张图片
jenkins+Github+tomcat 配置实例_第19张图片
jenkins+Github+tomcat 配置实例_第20张图片
https://github.com/bingyue/easy-springmvc-maven
jenkins+Github+tomcat 配置实例_第21张图片
jenkins+Github+tomcat 配置实例_第22张图片
调用maven命令
jenkins+Github+tomcat 配置实例_第23张图片
jenkins+Github+tomcat 配置实例_第24张图片
配置后端他omcat

[root@localhost ~]# mkdir -p /data/application 
[root@localhost ~]# rz
[root@localhost ~]# ls
anaconda-ks.cfg  apache-tomcat-8.5.45.tar.gz  ip_clean.sh  jdk-8u211-linux-x64.tar.gz
[root@localhost ~]# tar xf apache-tomcat-8.5.45.tar.gz -C /data/application/
[root@localhost ~]# cd /data/application/
[root@localhost application]# mv apache-tomcat-8.5.45/ tomcat 
[root@localhost application]# cd 
[root@localhost ~]# tar xf jdk-8u211-linux-x64.tar.gz -C /usr/local/
[root@localhost ~]# ls
anaconda-ks.cfg  apache-tomcat-8.5.45.tar.gz  ip_clean.sh  jdk-8u211-linux-x64.tar.gz
[root@localhost ~]# cd /usr/local/
[root@localhost local]# mv jdk1.8.0_211/ java 
[root@localhost ~]# vim /etc/profile
export JAVA_HOME=/usr/local/java
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
export TOMCAT_HOME=/data/application/tomcat
[root@localhost ~]# source /etc/profile
[root@localhost ~]# mkdir /opt/script
[root@localhost ~]# vim /opt/script/app-jenkins.sh 
#!/usr/bin/bash
#本脚本适用于jenkins持续集成,实现备份war包到代码更新上线!使用时请注意全局变量。
#================
#Defining variables
export JAVA_HOME=/usr/local/java
webapp_path="/data/application/tomcat/webapps"
tomcat_run="/data/application/tomcat/bin"
updata_path="/data/update/`date +%F-%T`"
backup_path="/data/backup/`date +%F-%T`"
tomcat_pid=`ps -ef | grep tomcat | grep -v grep | awk '{print $2}'`
files_dir="easy-springmvc-maven"
files="easy-springmvc-maven.war"
job_path="/root/upload"

#Preparation environment
echo "Creating related directory"
mkdir -p $updata_path
mkdir -p $backup_path

echo "Move the uploaded war package to the update directory"
mv $job_path/$files $updata_path

echo "========================================================="
cd /opt
echo "Backing up java project"
if [ -f $webapp_path/$files ];then
	tar czf $backup_path/`date +%F-%H`.tar.gz $webapp_path
	if [ $? -ne 0 ];then
		echo "打包失败,自动退出"
		exit 1
	else
		echo "Checking if tomcat is started"
		if [ -n "$tomcat_pid" ];then
			kill -9 $tomcat_pid
			if [ $? -ne 0 ];then
				echo "tomcat关闭失败,将会自动退出"
				exit 2
			fi
		fi
		cd $webapp_path
		rm -rf $files && rm -rf $files_dir
		cp $updata_path/$files $webapp_path
		cd /opt
		$tomcat_run/startup.sh
		sleep 5
		echo "显示tomcat的pid"
		echo "`ps -ef | grep tomcat | grep -v grep | awk '{print $2}'`"
		echo "tomcat startup"
		echo "请手动查看tomcat日志。脚本将会自动退出"
	fi
else
	echo "Checking if tomcat is started"
        if [ -n "$tomcat_pid" ];then
        	kill -9 $tomcat_pid
                if [ $? -ne 0 ];then
                	echo "tomcat关闭失败,将会自动退出"
                       	exit 2
                fi
        fi
	cp $updata_path/$files $webapp_path
	$tomcat_run/startup.sh
        sleep 5
        echo "显示tomcat的pid"
        echo "`ps -ef | grep tomcat | grep -v grep | awk '{print $2}'`"
        echo "tomcat startup"
        echo "请手动查看tomcat日志。脚本将会自动退出"
fi
[root@localhost ~]# chmod +x /opt/script/app-jenkins.sh 

jenkins+Github+tomcat 配置实例_第25张图片
jenkins+Github+tomcat 配置实例_第26张图片
jenkins+Github+tomcat 配置实例_第27张图片

你可能感兴趣的:(git)