Jenkins + Pipeline + Git + Maven (十)
一、准备环境介绍
192.168.5.71 # gitlab 仓库IP 192.168.5.72 # 开发环境,用于提交代码等 192.168.5.73 # tomcat 部署solo服务站点 192.168.5.239 # jenkins 服务器
二、在WEB机器(73)上面部署tomcat + maven环境
2.1 安装JDK,jenkins中添加slave节点需要
# tar xf jdk-8u111-linux-x64.tar.gz -C /usr/local/ # ln -s /usr/local/jdk1.8.0_111 /usr/local/jdk # vim /etc/profile export JAVA_HOME=/usr/local/jdk export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export PATH=$PATH:$JAVA_HOME/bin
2.2 安装maven环境,打war包的时候需要
# wget https://archive.apache.org/dist/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz # tar xf apache-maven-3.5.4-bin.tar.gz -C /usr/local/ # ln -s /usr/local/apache-maven-3.5.4/ /usr/local/maven
2.3 安装tomcat环境
# tar xf apache-tomcat-8.5.42.tar.gz -C /usr/local/ # ln -s /usr/local/apache-tomcat-8.5.42 /usr/local/tomcat # cd /usr/local/tomcat/webapps/ # rm -rf *
2.4 创建备份目录
# mkdir -p /data/backup
2.5 对git服务器进行免密认证
# ssh-keygen -t rsa -P '' -f ~/.ssh/id_dsa # ssh-copy-id -i /root/.ssh/id_dsa.pub git@192.168.5.71
2.6 安装unzip解压war包命令,也可以不安装,1.8会热解压,安装mariadb数据库,将数据库密码设置为123456,并创建solo数据库
# yum install unzip mariadb-server mariadb -y # mysqladmin -uroot password '123456' # mysql -uroot -p123456 MariaDB [(none)]> create database solo;
数据库密码也可以修改solo v3.6.6源码包,具体目录如下
# vim solo/src/main/resources/local.properties #### MySQL runtime #### runtimeDatabase=MYSQL jdbc.username=root jdbc.password=123456 jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.URL=jdbc:mysql://localhost:3306/solo?useUnicode=yes&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
三、在git服务器(71)上面创建solo仓库
# su - git $ mkdir repo/solo $ cd repo/solo/ $ git --bare init Initialized empty Git repository in /home/git/repo/solo/
git仓库已创建好,仓库地址是:git@192.168.5.71:/home/git/repo/solo
四、在开发机下载solo 3.6.6版本并上传到git仓库
# git clone git@192.168.5.71:/home/git/repo/solo # wget https://codeload.github.com/b3log/solo/tar.gz/v3.6.6 # tar xf v3.6.6 # mv solo-3.6.6/* solo/ # cd solo/ # 修改solo配置文件 # grep server src/main/resources/latke.properties serverScheme=http serverHost=solo.leon.com serverPort=8080 # 上传代码 # git add . # git commit -m 'all solo file' # git push origin master
五、开发机编写jenkinsfile文件
# git clone git@192.168.5.71:/home/git/repo/jenkinsfile # cd jenkinsfile/ # mkdir java-solo # cd java-solo/
jenkinsfile文件如下:
tomcat发布项目要重启服务
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
node ("192.168.5.73") { //def mvnHome = '/usr/local/maven' stage('git checkout') { checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'ccf5f140-9a28-49d6-abc8-40bc6c750323', url: 'git@192.168.5.71:/home/git/repo/solo']]]) } stage('maven build') { sh '''export JAVA_HOME=/usr/local/jdk /usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true''' } stage('deploy') { sh ''' JENKINS_NODE_COOKIE=dontkillme export JAVA_HOME=/usr/local/jdk TOMCAT_NAME=tomcat TOMCAT_HOME=/usr/local/$TOMCAT_NAME WWWROOT=$TOMCAT_HOME/webapps/ROOT if [ -d $WWWROOT ]; then mv $WWWROOT /data/backup/${TOMCAT_NAME}-$(date +"%F_%T") fi unzip ${WORKSPACE}/target/*.war -d $WWWROOT PID=$(ps -ef |grep $TOMCAT_NAME |egrep -v "grep|$$" |awk \'{print $2}\') [ -n "$PID" ] && kill -9 $PID /bin/bash $TOMCAT_HOME/bin/startup.sh''' } }
push代码:
# cd jenkinsfile/ # git add . # git commit -m 'add java-solo jenkinsfile' # git push origin master
六、配置Jenkins
6.1 添加slave机器
从节点配置信息
6.2 创建一个solo-java的pipeline配置如下
6.3 进行构建
七、如果遇到的坑
7.1 jenkins启动服务器自动关闭
原因:pipeline启动的tomcat进程是jenkins派生的,当job完成之后jenkins会自动关闭自己派生的进程。所以tomcat会被关闭。在jenkinsfile中加入此行内容。
JENKINS_NODE_COOKIE=dontkillme
7.2 solo没有皮肤的导致tomcat启动失败
[ERROR]-[2019-11-25 17:48:15]-[org.b3log.latke.Latkes:829]: Read skin [Pinghsu]'s configuration failed: null [ERROR]-[2019-11-25 17:48:15]-[org.b3log.solo.SoloServletListener:315]: Can't load the default skins, please make sure skin [Pinghsu] is under skins directory and structure correctly
经查看此目录是空的
# ls solo-3.6.6/src/main/webapp/skins/
解决方法:下载新的solo源码包,将其skins目录的文件拷贝到上面的目录即可
# cp -r solo/src/main/resources/skins/ solo-3.6.6/src/main/webapp/skins/