安装maven 91 gitlab-ctl start
92 /etc/init.d/jenkins start 上传所需安装包 查看自身jdk环境 93 rpm -qa | grep jdk

 94  rpm -e java-1.8.0-openjdk-headless-1.8.0.102-4.b14.el7.x86_64 java-1.7.0-openjdk-headless-1.7.0.111-2.6.7.8.el7.x86_64  --nodeps    #卸载自身jdk环境
   95  tar zxf jdk-8u211-linux-x64.tar.gz -C /usr/local/     解压配置文件到/usr/local/
   96  tar zxf apache-maven-3.6.1-bin.tar.gz 
   97  mv apache-maven-3.6.1 /usr/local/maven      解压配置文件到/usr/local/
   98  vim /etc/profile                           #末尾添加
export XGP=/usr/local/maven 
export PATH=$PATH:$XGP/bin
export  JAVA_HOME=/usr/local/jdk1.8.0_211
export  JRE_HOME=/usr/local/jdk1.8.0_211/jre
export  CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export  PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

  99  source  /etc/profile    刷新一下
  100  mvn -v            测试一下是否安装成功

使用阿里云仓库地址 vim /usr/local/maven/conf/settings.xml #158添加 <mirror> <id>alimaven</id> <name>aliyun maven</name>   <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> 102 cd 103 mvn archetype:generate -DgroupId=cn.bdqn.bdqnweb -DartifactId=bdqnweb -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false #构建一个项目

   86  cd xgp-demo/     进入之前创建的本地git仓库中
   87  rm -rf  *            删除仓库的所有内容
   88  git add *             上传到缓存  这里会报错
   89  git status             查看一下状态
   92  git  rm index.html   x.txt xgp.txt      上传一下删除文件
   93  git commit  -m "del"                   上传到版板库
   94  git push  origin master                上传到远程仓库

96 netstat -anpt | grep 8081 97 /etc/init.d/jenkins restart 重启jdk 98 /etc/init.d/jenkins status 查看jdk环境 104 ln -s /usr/local/jdk1.8.0_211/bin/java /usr/bin/ 链接命令目录 105 /etc/init.d/jenkins start 再次启动

107 ps -ef | grep 8081 修改Jenkins运行用户

  108  vim /etc/sysconfig/jenkins   
JENKINS_USER="root"   #29  用户改为root
108  
/etc/init.d/jenkins  restart
  109  ps -ef | grep 8081

111 cd bdqnweb/ 112 ls 113 cat src/main/webapp/index.jsp 114 vim src/main/webapp/index.jsp 修改如下内容 115 cat src/main/webapp/index.jsp

116  cp -r src/  pom.xml  /root/xgp-demo/  复制测试页面到本地仓库
  117  cd /root/xgp-demo/              进入本地仓库
  118  git add *                            上传到缓存
  119  git commit -m "test1 index.jsp"           上传到版本库
  120  git push  origin master                    上传到版本库

Jenkins服务器免密登陆1.50,1.60

  130  ssh-copy-id  root@192.168.1.50     
  131  ssh-copy-id  root@192.168.1.60
  132  ssh 192.168.1.50
  133  ssh 192.168.1.60

在web界面上运程仓库看一下

在jdk上安装or回滚插件 系统管理----全局工具配置---Maven安装---取消自动安装 JDK安装---取消自动安装 安装成功

新建任务---构建一-个maven项 目名称为web- tomcat----参数化构建过程-- -添加参数- -选项参数---名称Status
选项Deploy
RollBack
描述: ----请选择发布or回滚----需要填写commit id号 -
选项参数---名称deploy
选项test
stag
描述: ----请选择构建环境
文本参数---名称git
描述:请输入git commit id号
源码管理: git---Repositories---- Repository URL
git@192. 168.83.3: web/web-demo.git
Branches to build----Branch Specifier (blank for 'any' )---${git}
构建环境---勾选--Color ANSI Console 0utput
Build:
Goals and options----clean package -Dmaven. test. skip-true .
Post Steps --执行shell--编写脚本

clean package -Dmaven.test.skip=true

##Deploy tomcat 

war_bak="/data/war/bak"
tomcat_deploy="/usr/local/tomcat/webapps"
#WAR_PATH="${WORKSPACE}/${MODULE_NAME}/target/*.war"
WAR_PATH="${WORKSPACE}/target/*.war"

test_host="192.168.1.50"
stag_host="192.168.1.60"
port="8080"

echo "构建环境:${deploy} 项目名称:${JOB_NAME} 构建时间:`date +%F` 本次上线版本:${GIT_COMMIT}" >> /var/log/${JOB_NAME}.log

### status deploy or rollback

##判断git是否为空,如果是为空进行提示
if [ "${git}" = "" ];then

echo "请输入git版本 #############"
exit 1

else

## 判断发布 or 回滚
if [ "${Status}" = "Deploy" ];then

### 判断是否为测试环境
	if [ "${deploy}" = "test" ];then
					### 构建主机
						 for i in ${test_host}
							 do
						 ssh ${i} "mkdir -p ${war_bak}/${JOB_NAME}/${git}"
							 scp ${WAR_PATH} ${i}:${war_bak}/${JOB_NAME}/${git}/ROOT.war
							 ssh ${i} rm -rf ${tomcat_deploy}/*
							 ssh ${i} cp ${war_bak}/${JOB_NAME}/${git}/ROOT.war ${tomcat_deploy}
							 ssh ${i} /etc/init.d/tomcat  restart
				### 判断tomcat是否正常      
									 for http in `seq 1 5`
								 do
											tomcat_status=`curl -I ${i}:${port} -s|awk -F "[ ]" '{print $2}' |sed -n '1p'`
									if  [[ "$tomcat_status" -ne 200 ]] || [[ "$tomcat_status" = "" ]];then
											echo -e "\033[5;34m 请稍等,服务启动中........ \033[0m"
														sleep 5
										else
											echo -e "\033[5;34m 构建 ${i}环境发布正常,返回值[${tomcat_status}] \033[0m"
														break
										fi
								done

										if [[ "${tomcat_status}" -ne 200 ]] || [[ "${tomcat_status}" = "" ]];then
												if [[ "${tomcat_status}" = "" ]];then
										echo -e "\033[5;34m 构建 ${i}服务启动异常 \033[0m"
										exit 1
												fi
											echo -e "\033[5;34m 构建 ${i}环境发布异常,返回值[${tomcat_status}] \033[0m"
									fi
						done
							echo -e "\033[5;34m 本次构建${test_host}主机,本次环境 ${deploy} \033[0m"
## 判断为预发布环境
	elif [ "${deploy}" = "stag" ];then
						 for i in "${stag_host}"
							 do
						 ssh ${i} mkdir -p ${war_bak}/${JOB_NAME}/${git}
							 scp ${WAR_PATH} ${i}:${war_bak}/${JOB_NAME}/${git}/ROOT.war
							 ssh ${i} rm -rf ${tomcat_deploy}/*
							 ssh ${i} cp ${war_bak}/${JOB_NAME}/${git}/ROOT.war ${tomcat_deploy}
							 ssh ${i} /etc/init.d/tomcat restart
				### 判断tomcat是否正常      
									 for http in `seq 1 5`
								 do
											tomcat_status=`curl -I ${i}:${port} -s|awk -F "[ ]" '{print $2}' |sed -n '1p'`
									if  [[ "$tomcat_status" -ne 200 ]] || [[ "$tomcat_status" = "" ]];then
											echo -e "\033[5;34m 请稍等,服务启动中........ \033[0m"
														sleep 5
										else
											echo -e "\033[5;34m 构建 ${i}环境发布正常,返回值[${tomcat_status}] \033[0m"
														break
										fi
								done

										if [[ "${tomcat_status}" -ne 200 ]] || [[ "${tomcat_status}" = "" ]];then
												if [[ "${tomcat_status}" = "" ]];then
										echo -e "\033[5;34m 构建 ${i}服务启动异常 \033[0m"
										exit 1
											fi
											echo -e "\033[5;34m 构建 ${i}环境发布异常,返回值[${tomcat_status}] \033[0m"
									fi
			done
						 echo -e "\033[5;34m 本次构建${test_host}主机,本次环境 ${deploy} \033[0m"
   fi



### 回滚操作
elif [[ "${Status}" = "RollBack" ]];then
        

  ### 判断回滚环境及主机
            if [ "${deploy}" = "test" ];then
              for i in ${test_host}
              do
                 ssh ${i}  "[ -d ${war_bak}/${JOB_NAME}/${git} ]"
                      if [ $? -ne '0' ];then
                            echo -e "\033[5;34m  git commit 回滚目录不存在,环境${deploy} 错误主机${i} \033[0m"
                            exit 3
                        else
                            echo -e "\033[5;34m  准备回滚操作  本次回滚环境${deploy} 回滚主机${i} \033[0m"
                            sleep 3
                      fi
                 ssh ${i}  "mkdir -p ${war_bak}/${JOB_NAME}/${git}_${Status}_rollback/"
                 ssh ${i}  "cp -r ${tomcat_deploy}/* ${war_bak}/${JOB_NAME}/${git}_${Status}_rollback/"
                 ssh ${i}  "rm -rf ${tomcat_deploy}/*"
                 ssh ${i} "cp -r ${war_bak}/${JOB_NAME}/${git}/*.war ${tomcat_deploy}/"
                 ssh ${i} /etc/init.d/tomcat  restart
                   ### 判断tomcat是否正常      
              for http in `seq 1 5`
              do
                 tomcat_status=`curl -I ${i}:${port} -s|awk -F "[ ]" '{print $2}' |sed -n '1p'`
                 if  [[ "$tomcat_status" -ne 200 ]] || [[ "$tomcat_status" = "" ]];then
                        echo -e "\033[5;34m 请稍等,服务启动中........ \033[0m"
                        sleep 5
                 else
                        echo -e "\033[5;34m 构建 ${i}环境发布正常,返回值[${tomcat_status}] \033[0m"
                        break
                 fi
              done
          
                  if [[ "${tomcat_status}" -ne 200 ]] || [[ "${tomcat_status}" = "" ]];then
                    if [[ "${tomcat_status}" = "" ]];then
                      echo -e "\033[5;34m 构建 ${i}服务启动异常 \033[0m"
                      exit 1
                    fi
                      echo -e "\033[5;34m 构建 ${i}环境发布异常,返回值[${tomcat_status}] \033[0m"
                 fi
              done

            elif [ "${deploy}" = "stag" ];then
              echo "123"
            ### 判断测试环境fi结束
            fi
    

fi

#### fi是判断是否有git地址的结束
fi

1.50,1.60 搭建tomcat环境

   19  cd /usr/src/
   20  mkdir tomcat
   21  cd tomcat/
   22  rz        上传tomcat安装包
安装tomcat
   24  tar zxf apache-tomcat-8.5.35.tar.gz 
   25  mv apache-tomcat-8.5.35 /usr/local/tomcat     移动tomcat配置文件
   26  mv tomcat /etc/init.d/                   添加一个tomcat启动脚本到/etc/init.d
   32  chmod  +x /etc/init.d/tomcat                给启动脚本一个可执行权限
   33  /etc/init.d/tomcat  start                     启动tomcat
   34  netstat -anpt | grep 8080

   35  scp /etc/init.d/tomcat  root@192.168.1.60:/etc/init.d/    复制tomcat启动文件到1.50和1.60
36 scp -r /usr/local/tomcat  root@192.168.1.60:/usr/local/tomcat/   复制tomcat配置文件到1.50和1.60

1.60
   13  /etc/init.d/tomcat  start
   14  netstat -anpt | grep 8080

**测试一下 ** 浏览器查看192.168.1.50:8080

浏览器查看192.168.1.60:8080