Pipeline流水线JAVA项目发布
所有的全局变量
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
export TOMCAT_HOME=/usr/local/tomcat
export MAVEN_HOME=/usr/local/maven
export PATH=$PATH:${MAVEN_HOME}
export TOMCAT_HOME=/usr/local/tomcat
项目案例二:流水线自动化发布JAVA项目
主机名 | IP地址 | 备注 |
---|---|---|
Git | 192.168.200.70 | Git服务器 |
Jenkins | 192.168.200.91 | Jenkins服务器 |
Tomcat | 192.168.200.104 | Tomcat服务器 |
所有服务器进行如下操作
cat /etc/redhat-release
uname -r
systemctl stop firewalld
systemctl disable firewalld
systemctl stop NetworkManager
systemctl disable NetworkManager
Tomcat服务器环境部署
tomcat所有包
链接:https://pan.baidu.com/s/1bFIsmqk0AdFGeB2-fbb7cQ
提取码:uvyw
部署jdk
ls
tar xf jdk-8u171-linux-x64.tar.gz -C /usr/local
cd /usr/local
mv jdk1.8.0_171 jdk
ln -s /usr/local/jdk/bin/java /usr/local/bin
java -version
部署java环境变量
sed -i.ori '$a export JAVA_HOME=/usr/local/jdk\nexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH\nexport CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar' /etc/profile
tail -3 /etc/profile
source /etc/profile
部署maven
ls
tar xf apache-maven-3.5.0-bin.tar.gz -C /usr/local/
cd /usr/local
mv apache-maven-3.5.0 maven
ln -s /usr/local/maven/bin/mvn /usr/local/bin
mvn -version
部署maven环境变量
vim /etc/profile
export MAVEN_HOME=/usr/local/maven
export PATH=$PATH:${MAVEN_HOME}
tail -2 /etc/profile
source /etc/profile
部署tomcat
ls
tar xf apache-tomcat-8.5.31.tar.gz -C /usr/local/
cd /usr/local
mv apache-tomcat-8.0.27 tomcat
cd tomcat
rm -rf webapps/*
ls webapps/
部署tomcat环境变量
echo 'export TOMCAT_HOME=/usr/local/tomcat' >> /etc/profile
tail -1 /etc/profile
source /etc/profile
自动部署需要解压maven构建后的war包,因此需要安装unzip
yum -y install unzip
which unzip
自动部署需要git远程仓库代码,因此需要安装git
yum -y install git
which git
构建一个B-JAVA流水线项目的提前准备工作
在Git服务器上创建用于存放solo源代码的仓库
hostname -I
cd /home/git/repos
mkdir solo.git
cd solo.git
git --bare init
cd ..
chown -R git.git solo.git
在jenkins服务器上进行解压solo.tar.gz
链接:https://pan.baidu.com/s/1Fui3TNCwEsmv4TeUZ0fiWA
提取码:tjo6 --->solo源代码ls
tar xf solo.tar.gz
mkdir -p /mycode
cd /mycode
git clone git@192.168.200.70:/home/git/repos/solo.git
ls
cd solo
mv /root/solo/* .
ls
为了最后的测试,我们需要修改源代码的一个配置文件
ls
cd src/main/resources
cat -n latke.properties | sed -n '29p;31p'
vim latke.properties +29
cat -n latke.properties | sed -n '29p;31p'
29 serverHost=192.168.200.104 --->tomcat服务器IP地址
31 serverPort=8080 --->监听端口
将solo源代码提交到git仓库
cd /mycode/solo
git add *
git commit -m "第一次提交"
git push -u origin master
将代码克隆到本地进行克隆验证
cd /test
git clone git@192.168.200.70:/home/git/repos/solo.git
cd solo
ls
在jenkins服务器上创建一个流水线脚本并提交git远程仓库(构建B-JAVA的时候生成过脚本)
mkdir /jenkinsfile
cd /jenkinsfile
git clone git@192.168.200.70:/home/git/repos/jenkinsfile
ls
cd jenkinsfile
mkdir itemB
vim itemB/jenkinsfile-java-solo
cat itemB/jenkinsfile-java-solo
node ("JAVA-slave1-192.168.200.104"){
//def mvnHome
stage('checkout') { // for display purposes
checkout([$class: 'GitSCM', branches: [[name: '${branch}']],
doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '9f98962f-1a82-4da1-8a0f-bc906e92d998',
url: 'git@192.168.200.70:/home/git/repos/solo.git']]])
}
stage('maven build') {
sh '/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true'
}
stage('deploy') {
sh '''WWWROOT=/usr/local/tomcat/webapps/ROOT
[ -d /data/backup ] || mkdir -p /data/backup
if [ -d $WWWROOT ];then
mv $WWWROOT /data/backup/tomcat-$(date +%F_%T)
fi
unzip ${WORKSPACE}/target/*.war -d $WWWROOT
PID=$(ps -ef | grep tomcat | egrep -v "grep|$$" | awk '{print $2}')
[ -n "$PID" ] && kill -9 $PID
/bin/bash /usr/local/tomcat/bin/startup.sh'''
}
stage('test') {
sh 'echo susses'
}
}
在jenkins网页上创建tomcat服务器的节点信息
构建一个B-JAVA流水线项目(有上文生成的脚本信息)
推送本地仓库到远程git
git add *
git commit -m "java流水线脚本"
git push -u origin master
在B-JAVA立即构建并测试
在tomcat服务器上查看tomcat服务并在浏览器查看测试结果
ss -antup | grep 8080
如果进行流水线的单Slave从节点分布式构建测试(出现tomcat服务未启动情况)
如果构建会失败在最后一步(tomcat进程未启动)
jenkins没办法启动tomcat的socker进程
原因在于Pipeline流水线在启动一个脚本程序的时候,这个脚本程序会运行在后台。
但是当Pipeline结束以后,jenkins会自动kill掉所有和Pipeline有关的派生子进程
因此,我们需要在流水线脚本里加一个变量参数,不让Pipeline杀掉才可以JENKINS_NODE_COOKIE=dontkillme
在jenkins服务器上重新克隆远程仓库代码
cd /jenkinsfile
rm -rf *
git clone git@192.168.200.70:/home/git/repos/jenkinsfile
ls
cd /jenkinsfile
ls
修改jenkins服务器上的流水线脚本
cd /jenkinsfile/jenkinsfile/
vim itemB/jenkinsfile-java-solo
cat itemB/jenkinsfile-java-solo
node ("JAVA-slave1-192.168.200.104"){
//def mvnHome
stage('checkout') { // for display purposes
checkout([$class: 'GitSCM', branches: [[name: '${branch}']],
doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '9f98962f-1a82-4da1-8a0f-bc906e92d998',
url: 'git@192.168.200.70:/home/git/repos/solo.git']]])
}
stage('maven build') {
sh '/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true'
}
stage('deploy') {
sh '''
JENKINS_NODE_COOKIE=dontkillme
WWWROOT=/usr/local/tomcat/webapps/ROOT
[ -d /data/backup ] || mkdir -p /data/backup
if [ -d $WWWROOT ];then
mv $WWWROOT /data/backup/tomcat-$(date +%F_%T)
fi
unzip ${WORKSPACE}/target/*.war -d $WWWROOT
PID=$(ps -ef | grep tomcat | egrep -v "grep|$$" | awk '{print $2}')
[ -n "$PID" ] && kill -9 $PID
/bin/bash /usr/local/tomcat/bin/startup.sh'''
}
stage('test') {
sh 'echo susses'
}
}
推送本地仓库到远程git
git add *
git commit -m "java-流水线脚本"
git push -u origin master
在B-JAVA立即构建并测试
在tomcat服务器上查看进程并浏览器上查看测试结果
ss -antup | grep 8080 --->不添加内段话,流水线脚本默认杀掉tomcat服务
让ssh支持流水线,需要安装插件SSH Pipeline Steps
流水线ssh方式的脚本详解
def remote = [:] #定义变量不需要改
remote.name = 'test' #远程主机的名字
remote.host = 'test.domain.com' #远程主机的IP
remote.user = 'root' #远程主机的用户
remote.password = 'password' #远程主机的密码
remote.allowAnyHosts = true #照着超就可以
stage('Remote SSH') { #阶段名称
writeFile file: 'abc.sh', text: 'ls -lrt' #把后面的命令,让如abc.sh脚本里
sshScript remote: remote, script: "abc.sh" #把脚本分发自定义的主机
}
在浏览器进入jenkins主用户来添加新节点信息(有几点节点就添加几个)
流水线ssh方式的脚本内容(一个节点给多台发送SSH脚本)
node ("JAVA-slave1-192.168.200.104"){
//def mvnHome
def remote = [:]
stage('checkout') { // for display purposes
checkout([$class: 'GitSCM', branches: [[name: '${branch}']],
doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '9f98962f-1a82-4da1-8a0f-bc906e92d998',
url: 'git@192.168.200.70:/home/git/repos/solo.git']]])
}
stage('maven build') {
sh '/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true'
}
stage('Remote ssh 200.104') {
remote.name = 'test'
remote.user = '192.168.200.104'
remote.user = 'root'
remote.password = 'linyaonie'
remote.allowAnyHosts = true
writeFile file: '104.sh',text:'''
echo "`hostname -I`"
'''
sshScript remote: remote,script: "104.sh"
}
stage('Remote ssh 200.xxx') {
remote.name = 'test'
remote.user = '192.168.200.xxx'
remote.user = 'root'
remote.password = 'linyaonie'
remote.allowAnyHosts = true
writeFile file: 'xxx.sh',text:'''
echo "`hostname -I`"
'''
sshScript remote: remote,script: "xxx.sh"
}
}
重新把新修过的内容添加到远程仓库并构建测试在查看logs日志的结果
git add *
git commit -m "测试SSH"
git push -u origin master
流水线项目发布多节点需要注意的事项
多节点流水线脚本内容(多个节点)
node ("JAVA-slave1-192.168.200.104"){
//def mvnHome
stage('checkout') { // for display purposes
checkout([$class: 'GitSCM', branches: [[name: '${branch}']],
doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '9f98962f-1a82-4da1-8a0f-bc906e92d998',
url: 'git@192.168.200.70:/home/git/repos/solo.git']]])
}
stage('maven build') {
sh '/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true'
}
stage('deploy') {
sh '''
JENKINS_NODE_COOKIE=dontkillme
WWWROOT=/usr/local/tomcat/webapps/ROOT
[ -d /data/backup ] || mkdir -p /data/backup
if [ -d $WWWROOT ];then
mv $WWWROOT /data/backup/tomcat-$(date +%F_%T)
fi
unzip ${WORKSPACE}/target/*.war -d $WWWROOT
PID=$(ps -ef | grep tomcat | egrep -v "grep|$$" | awk '{print $2}')
[ -n "$PID" ] && kill -9 $PID
/bin/bash /usr/local/tomcat/bin/startup.sh'''
}
stage('test') {
sh 'echo susses'
}
}
node ("JAVA-slave2-192.168.200.xxx"){
//def mvnHome
stage('checkout') { // for display purposes
checkout([$class: 'GitSCM', branches: [[name: '${branch}']],
doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '9f98962f-1a82-4da1-8a0f-bc906e92d998',
url: 'git@192.168.200.70:/home/git/repos/solo.git']]])
}
stage('maven build') {
sh '/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true'
}
stage('deploy') {
sh '''
JENKINS_NODE_COOKIE=dontkillme
WWWROOT=/usr/local/tomcat/webapps/ROOT
[ -d /data/backup ] || mkdir -p /data/backup
if [ -d $WWWROOT ];then
mv $WWWROOT /data/backup/tomcat-$(date +%F_%T)
fi
unzip ${WORKSPACE}/target/*.war -d $WWWROOT
PID=$(ps -ef | grep tomcat | egrep -v "grep|$$" | awk '{print $2}')
[ -n "$PID" ] && kill -9 $PID
/bin/bash /usr/local/tomcat/bin/startup.sh'''
}
stage('test') {
sh 'echo susses'
}
}
假如是克隆的机器需要把hosts文件里的映射修改一下
在浏览器进入jenkins主用户来添加新节点信息(有几点节点就添加几个)
重新把新修过的内容添加到远程仓库并构建测试在查看logs日志的结果
git add *
git commit -m "测试SSH"
git push -u origin master