感谢xxh等整理
系统信息
操作系统版本
CentOS release 6.2 (Final)
Kernel \r on an \m
Linux localhost 2.6.32-220.el6.x86_64 #1 SMP Tue Dec 6 19:48:22
GMT 2011 x86_64 x86_64 x86_64 GNU/Linux
硬件信息
CPU:
model name : 2 Intel(R) Xeon(R) CPU E5-2630 v2 @ 2.60GHz
cpu MHz : 2600.000
cache size : 15360 KB
cpuid level : 13
内存:
MemTotal : 8062088 kB
硬盘:
文件系统 容量 已用 可用 已用%% 挂载点
/dev/sda3 75G 15G 57G 22% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
/dev/sda1 194M 25M 160M 14% /boot
/dev/sdb1 197G 48G 140G 26% /work
网卡:
e1000 0000:02:00.0: eth0: (PCI:66MHz:32-bit) 00:0c:29:31:08:3c
e1000 0000:02:00.0: eth0: Intel(R) PRO/1000 Network Connection
e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
eth0: no IPv6 routers present
搭建Jenkins
下载地址
Jenkins rpm包:https://pkg.jenkins.io/redhat-stable/jenkins-2.190.1-1.1.noarch.rpm
JDK:https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
maven:https://mirrors.bfsu.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
配置JDK
配置环境变量
vi /etc/profile
在文件最后增加以下:
export JAVA_HOME=/usr/local/jdk1.8.0_73
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export PATH
export CLASSPATH
保存退出后执行以下命令使配置生效:
source /etc/profile
显示以下信息则配置成功
[root@localhost ~]# java -version
java version "1.8.0_73"
Java(TM) SE Runtime Environment (build 1.8.0_73-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.73-b02, mixed mode)
配置MAVEN
配置环境变量
vi /etc/profile
在文件最后增加:
export MAVEN_HOME=/usr/local/apache-maven-3.6.3
export PATH=$MAVEN_HOME/bin:$PATH
保存退出后执行以下命令使配置生效:
source /etc/profile
显示以下信息则配置成功
[root@localhost ~]# mvn --version
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da;
2013-02-19 21:51:28+0800)
Maven home: /usr/local/apache-maven-3.6.3
Java version: 1.8.0_73, vendor: Oracle Corporation
Java home: /usr/local/java.test/jdk1.8.0_73/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "2.6.32-220.el6.x86_64", arch: "amd64", family: "unix"
配置MAVEN仓库
vi /usr/local/apache-maven-3.0.5/setting.xml
修改仓库目录:
/work/jenkins/repository
安装jenkins
cd 进到jenkins-2.190.1-1.1.noarch.rpm所有目录,执行以下命令
sudo rpm -ih jenkins-2.190.1-1.1.noarch.rpm
自动安装完成之后:
/usr/lib/jenkins/jenkins.war WAR包
/etc/sysconfig/jenkins 配置文件
/var/lib/jenkins/ 默认的JENKINS_HOME目录
/var/log/jenkins/jenkins.log Jenkins日志文件
配置文件修改
vi /etc/init.d/jenkins
在/usr/bin/java下面添加
/usr/local/jdk1.8.0_73/bin/java
端口修改
此修改为可选项
如端口8080被占用修改以下文件:
vi /etc/sysconfig/jenkins
修改JENKINS_PORT="8080" 为 JENKINS_PORT="8082"
主目录修改
此修改为可选项
Jenkins储存所有的数据文件在这个目录下,如果主目录硬盘挂载空间不足,则可
修改主目录位置。
vi /etc/sysconfig/jenkins
修改JENKINS_HOME="/var/lib/jenkins" 为 JENKINS_HOME="/work/jenkins"
用户修改
此修改为可选项Jenkins默认会以jenkins帐户运行,由于权限及不可预见的错误可以修改为以root 运行。vi /etc/sysconfig/jenkins修改JENKINS_USER="jenkins" 为 JENKINS_USER="root"
启动Jenkins
sudo service jenkins start
[root@bogon jenkins]# sudo service jenkins start
Starting Jenkins [ OK ]
访问Jenkins
启动正常,输入http://192.168.x.x:8080/访问
系统管理
系统设置
执行者数量修改为4,即可一次同时执行4个JOB构建
输入JDK和Git路径(Git需要先安装)
输入MAVEN路径
使用SVN做为版本控制工具的请选择对应的SVN版本
插件管理
需要安装以下插件:
Git plugin
Git Parameter Plug-In
Gitlab Plugin
Role-based Authorization Strategy
Active Choices Plug-in
Extended Choice Parameter Plug-In
配置自动部署
第一种方式
服务器端创建用户
useradd deploy
passwd deploy
配置ssh免密登录
配置rsync远程传输
第二种方式
直接在jenkins配置远程上传
tomcat
脚本:
#!/bin/sh#defined TOMCAT_HOME="/usr/local/tomcat/"ID=ps -ef | grep java | grep tomcat|awk '{print $2}'
echo $ID echo "kill tomcat"kill -9 $ID
cd "$TOMCAT_HOME"/webapps
echo "copy war to webapp"#cd /home/lincp /work/jenkins/repository/com/vendor/2.24.4/vendor-2.24.4.war "$TOMCAT_HOME"/webapps
sleep 5s
cd "$TOMCAT_HOME"/webappsecho "remover war file"rm -rf ROOTrm -rf ROOT.war
sleep 2smv vendor-2.24.4.war ROOT.war
sleep 2s#echo "start tomcat"cd /usr/local/tomcat/bin./startup.sh
#rm -rf /var/lib/jenkins/jobs/项目/modules/com$vendor/builds
微服务
参考:https://www.cnblogs.com/jxxiaocao/p/12691289.html
Discard old builds 选中
Local module directory设置为空
Build whenever a SNAPSHOT dependency is built 选中
Post-build Actions
参考:https://www.cnblogs.com/st666/p/13093259.html
Source files:NewShop/manager-api/target/*.jar
Remote directory :/usr/local/newshop
脚本:cd /usr/local/newshop/NewShop/manager-apish display.sh restart
#!/bin/bash#这里可替换为你自己的执行程序,其他代码无需更改APP_NAME=seller-api-7.1.5.jarTARGET_PATH=/usr/local/newshop/NewShop/seller-api/targetsource /etc/profileBUILD_ID=dontKillMels_date=date +%Y%m%d
#启动方法
start(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'` if [ "$pid" ]; then echo "$APP_NAME is already running. pid=$pid ." else cd $TARGET_PATH #cp ~/$APP_NAME $TARGET_PATH nohup java -jar $APP_NAME --spring.profiles.active=dev >> /usr/local/newshop/config-server/seller-api.log-$ls_date 2>&1 & sleep 10 echo "$APP_NAME now is running" fi
}
#停止方法
stop(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'` if [ "$pid" ]; then kill -9 $pid echo "Pid:$pid stopped" else echo "$APP_NAME is not running" fi
}
#输出运行状态
status(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'` if [ "$pid" ]; then echo "$APP_NAME is running. Pid is ${pid}" else echo "$APP_NAME is NOT running." fi
}
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
start) start ;; stop) stop ;; status) status ;; restart) stop sleep 5 start ;; *) echo "Usage:{start|stop|status|restart}" ;;
esac
exit 0
构建
测试环境JOB
构建一个maven项目
Item名称:默认同线上发布包名称一致
选择构建一个maven项目,也可以复制已有的Item,等于重新拷贝一个Item,拷 贝 后需要重新修改下URL
参数化构建
Git Parameter
Name: branch
Description: GIT分支名称,默认同计划名称保持一致
Parameter Type:Branch
Choice
Name: project_name
Choice: xxxService
xxxService
Description: 需要发布的项目
Choice
Name: ips
Choices: ip:2212
ip:2211
Description: 需要发布的目标服务器,格式说明:IP:数据传输端口
源码管理
在Branches to build 填入参数化构建时Git Parameter的变量名
Build
此处输入maven 参数:clean install -Dmaven.test.skip=true
Post Steps
要执行的shell脚本:test.sh
#!/bin/shBUILD_ID=dontKillMecd /usr/local
if [[ $project_name =~ "-" ]]; then echo "请选择需要发布的项目" exit 1fi
#部署到指定服务器ip=echo ${ips} | cut -d \: -f 1
rsync_port=echo ${ips} | cut -d \: -f 2
echo "开始向$ip部署${project_name}"rsync -azvr --progress --port=$rsync_port --password-file=/etc/rsync.passwd --exclude=application.properties --exclude=bin ./package/${project_name} deploy@$ip::write_dataecho ""
echo "$ip的${project_name}项目部署完成,正在重启..."ssh $ip "sh /usr/local/${project_name}/bin/restart.sh && $ip的${project_name}项目重启完成!&& if [ ! -f /usr/local/bak/${project_name}.$(date +%Y-%m-%d).tgz ]; then mkdir -p /usr/local/bak 2>/dev/null && echo 正在备份${project_name}至/usr/local/bak/ && cd /usr/local && tar -zcvf bak/${project_name}.$(date +%Y-%m-%d).tgz ${project_name} >/dev/null;fi " || exit 1echo ""
构建JOB
选择相应参数后点击开始构建,开始打包并自动部署和重启。
生产环境JOB
Item名称:默认同线上发布包名称一致
选择构建一个maven项目,也可以复制已有的Item,等于重新拷贝一个Item,拷 贝 后需要重新修改下URL
参数化构建
Git Parameter
Name: branch
Description: GIT分支名称,默认同计划名称保持一致
Parameter Type:Branch
Choice
Name: project_name
Choice: xxxService
xxx1Service
xxx1Service
Description: 需要发布的项目
Extended Choice
Name: ips
Description: 需要发布的目标服务器,按住Ctrl可多选,格式说明:SSH连接端口:数据传输端口
Basic Parameter Types: Multi Selet
Value: 2250:2050,2251:2051
Default Value: 2250:2050
源码管理
在Branches to build 填入参数化构建时Git Parameter的变量名
Build
此处输入maven 参数:clean install -Dmaven.test.skip=true
Post Steps
要执行的shell脚本:deploy.sh
#!/bin/shBUILD_ID=dontKillMecd /usr/local
if [[ $project_name =~ "-" ]]; then echo "请选择需要发布的项目" exit 1fi
ip='120.241.38.221'ip_array=(${ips//,/ })
#部署到各个服务器for i in ${ip_array[@]};do ssh_port=echo $i | cut -d \: -f 1
rsync_port=echo $i | cut -d \: -f 2
echo "开始向$ip:$ssh_port部署${project_name}"rsync -azvr --progress --port=$rsync_port --password-file=/etc/rsync.passwd --exclude=application.properties --exclude=bin ./package/${project_name} deploy@$ip::write_dataecho ""echo "$ip:$ssh_port的${project_name}项目部署完成,正在重启..."ssh -p $ssh_port deploy@$ip "sudo sh /usr/local/${project_name}/bin/restart.sh && sudo echo $ip:$ssh_port的${project_name}项目重启完成!&& if [ ! -f /usr/local/bak/${project_name}.$(date +%Y-%m-%d).tgz ];then sudo mkdir -p /usr/local/bak 2>/dev/null && sudo echo 正在备份${project_name}至/usr/local/bak/ && sudo tar -zcvPf /usr/local/bak/${project_name}.$(date +%Y-%m-%d).tgz /usr/local/${project_name} >/dev/null;fi " || exit 1echo ""
done
构建JOB
选择相应参数后点击开始构建,开始打包并自动部署和重启。
直接发布JOB
Item名称:默认同线上发布包名称一致
选择构建一个maven项目,也可以复制已有的Item,等于重新拷贝一个Item,拷 贝 后需要重新修改下URL
参数化构建
Choice
Name: project_name
Choice: xxxproject
xxxproject1
xxxproject2
-----------------------
xxxproject2
xxxproject2
xxxproject2
Description: 需要发布的项目
Active Choice Reactive
Name: ips
Script: Groovy Script:
if(project_name.equals("xxxproject2") || project_name.equals("xxxproject2") || project_name.equals("xxxproject2") || project_name.equals("xxxproject2") || project_name.equals("xxxproject2") || project_name.equals("xxxproject2")){
return ["2220:2020:selected","2221:2021"]
} else if (project_name.equals("xxxproject2") || project_name.equals("xxxproject2") || project_name.equals("xxxproject2") || project_name.equals("xxxproject2")) {
return ["2230:2030:selected","2231:2031"]
} else {
return ["请选择需要发布的项目"]
}
Description: 需要发布的目标服务器,按住Ctrl可多选,格式说明:SSH连接端口:数据传输端口
传输端口
Choice Types: Multi Selet
Referenced parameters: project_name
构建
脚本
构建JOB
选择相应参数后点击开始构建,开始打包并自动部署和重启。
日志下载JOB
配置同“直接发布job
log.sh
#!/bin/shBUILD_ID=dontKillMecd /usr/local
if [[ $project_name =~ "-" ]]; then echo "请选择需要下载日志的项目" exit 1fi
ip='120.241.38.221'ip_array=(${ips//,/ })
#部署到各个服务器for i in ${ip_array[@]};do ssh_port=echo $i | cut -d \: -f 1
rsync_port=echo $i | cut -d \: -f 2
ssh -p $ssh_port deploy@$ip "if [ ! -f /usr/local/logs/download -a -d /usr/local/logs/${project_name} ];then sudo mkdir -p /usr/local/logs/download 2>/dev/null && sudo echo 正在打包${ssh_port}上的${project_name}最新日志文件至/usr/local/logs/download && sudo tar -zcvPf /usr/local/logs/download/${project_name}.${ssh_port}.tgz /usr/local/logs/${project_name}/service.log >/dev/null;fi " || exit 1echo ""echo "开始下载日志文件:${project_name}.${ssh_port}.tgz"rsync -azvr --progress --port=$rsync_port --password-file=/etc/rsync.passwd deploy@$ip::log_data ./logsecho "$ip:$ssh_port的${project_name}日志文件下载完成,保存在/usr/local/logs"echo ""
done
项目中脚本
bin restart.sh start.sh stop.sh
restart
#!/bin/bash. /etc/profile
cd dirname $0
BIN_DIR=pwd
cd ..DEPLOY_DIR=pwd
NAME_SERVER=pwd | rev |awk -F \/ '{print $1}' | rev
LOGS_DIR=$DEPLOY_DIR/logs/stdout.log
echo -e "Stoping the $NAME_SERVER ..."sh $BIN_DIR/stop.sh
echo -e "Starting the $NAME_SERVER ..."sh $BIN_DIR/start.sh
tailf $LOGS_DIR|while read linedo a=echo $line|grep "Main server started!"|wc -l
b=0 b=echo $line|grep -E "^at "|wc -l
if [ $b -ge 1 ];then echo -e "\t$line" else echo "$line" fi
if [ $a -eq 1 ];thenrm -f $LOGS_DIRkill -9 `ps -ef|grep "tailf $LOGS_DIR" |awk '{print $2}'`;break;fi
done
PIDS1=ps -ef | grep java | grep "$DEPLOY_DIR" |awk '{print $2}'
echo "PID:$PIDS1"
start
#!/bin/bashcd dirname $0
BIN_DIR=pwd
cd ..DEPLOY_DIR=pwd
CONF_DIR=$DEPLOY_DIR/conf
SERVER_NAME=sed '/dubbo.transaction_service_name/!d;s/.*=//' conf/application.properties | tr -d '\r'
SERVER_PORT=sed '/dubbo.transaction_service_port/!d;s/.*=//' conf/application.properties | tr -d '\r'
if [ -z "$SERVER_NAME" ]; then SERVER_NAME=hostname
fi
PIDS=ps -ef | grep java | grep "$CONF_DIR" |awk '{print $2}'
if [ -n "$PIDS" ]; then echo "ERROR: The $SERVER_NAME already started!" echo "PID: $PIDS" exit 1fi
if [ -n "$SERVER_PORT" ]; then SERVER_PORT_COUNT=netstat -tln | grep $SERVER_PORT | wc -l
if [ $SERVER_PORT_COUNT -gt 0 ]; then echo "ERROR: The $SERVER_NAME port $SERVER_PORT already used!" exit 1 fifi
LOGS_DIR=$DEPLOY_DIR/logs
if [ ! -d $LOGS_DIR ]; then mkdir $LOGS_DIRfi
STDOUT_FILE=$LOGS_DIR/stdout.log
LIB_DIR=$DEPLOY_DIR/libLIB_JARS=ls $LIB_DIR|grep .jar|awk '{print "'$LIB_DIR'/"$0}'|tr "\n" ":"
JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true "JAVA_DEBUG_OPTS=""if [ "$1" = "debug" ]; then JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n "fiJAVA_JMX_OPTS=""if [ "$1" = "jmx" ]; then JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false "fi
JAVA_MEM_OPTS=""BITS=java -version 2>&1 | grep -i 64-bit
if [ -n "$BITS" ]; then JAVA_MEM_OPTS=" -server -Xmx1g -Xms1g -Xmn384m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 "else JAVA_MEM_OPTS=" -server -Xmx1g -Xms1g -Xmn384m -XX:PermSize=128m -XX:SurvivorRatio=2 -XX:+UseParallelGC "fi
echo -e "Starting the $SERVER_NAME ...\c"nohup java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS -classpath $CONF_DIR:$LIB_JARS com.vendor.MainServiceWxShop > $STDOUT_FILE 2>&1 &
echo -e "Starting the while...\c"COUNT=0while [ $COUNT -lt 1 ]; do echo -e ".\c" sleep 1 if [ -n "$SERVER_PORT" ]; then COUNT=netstat -an | grep $SERVER_PORT | wc -l
elseCOUNT=`ps -ef | grep java | grep "$DEPLOY_DIR" | awk '{print $2}' | wc -l`fiif [ $COUNT -gt 0 ]; then breakfi
done
echo "OK!"PIDS=ps -ef | grep java | grep "$DEPLOY_DIR" | awk '{print $2}'
echo "PID: $PIDS"echo "STDOUT: $STDOUT_FILE"
stop
#!/bin/bashcd dirname $0
BIN_DIR=pwd
cd ..DEPLOY_DIR=pwd
CONF_DIR=$DEPLOY_DIR/conf
SERVER_NAME=sed '/dubbo.transaction_service_name/!d;s/.*=//' conf/application.properties | tr -d '\r'
if [ -z "$SERVER_NAME" ]; then SERVER_NAME=hostname
fi
PIDS=ps -ef | grep java | grep "$CONF_DIR" |awk '{print $2}'
if [ -z "$PIDS" ]; then echo "ERROR: The $SERVER_NAME does not started!" exit 1fi
echo -e "Stopping the $SERVER_NAME ...\c"for PID in $PIDS ; do kill $PID > /dev/null 2>&1done
COUNT=0while [ $COUNT -lt 1 ]; do echo -e ".\c" sleep 1 COUNT=1 for PID in $PIDS ; do PID_EXIST=ps -f -p $PID | grep java
if [ -n "$PID_EXIST" ]; then COUNT=0 break fi donedone
echo "OK!"echo "PID: $PIDS"