一、Jenkins pipeline自动化发布Java项目
### --- 环境准备
~~~ ——>**插件**:Maven Integration plugin
~~~ ——>**环境:**maven、tomcat,git
### --- 用的博客系统代码:
git clone https://github.com/b3log/solo.git
### --- 远端git服务器:
[root@qfedu.com ~]# mkdir -p solo
[root@qfedu.com ~]# cd solo/
[root@qfedu.com ~]# git --bare init
Initialized empty Git repository in /home/git/repos/solo/
### --- 本地web:
[root@qfedu.com ~]# git remote -v
origin git@172.16.1.3:/home/git/repos/solo (fetch)
origin git@172.16.1.3:/home/git/repos/solo (push)
[root@qfedu.com ~]# git remote rm origin
[root@qfedu.com ~]# git init
Reinitialized existing Git repository in /root/solo/.git/
[root@qfedu.com ~]# git remote add origin git@172.16.1.3:/home/git/repos/solo
[root@qfedu.com ~]# git add .
[root@qfedu.com ~]# git commit -m "java solo all"
# On branch master
nothing to commit, working directory clean
[root@qfedu.com ~]# git push origin master
Counting objects: 29058, done.
Compressing objects: 100% (9854/9854), done.
Writing objects: 100% (29058/29058), 47.77 MiB | 39.28 MiB/s, done.
Total 29058 (delta 15768), reused 29058 (delta 15768)
To git@172.16.1.3:/home/git/repos/solo
* [new branch] master -> master
### --- 因为solo需要改如下配置才可以访问:(改serverhost为指定的域名)
~~~ ——>然后再重新提交上去
[root@qfedu.com ~]# vim /root/solo/src/main/resources/latke.properties
二、**部署节点:
### --- **部署节点:** node 节点需要在系统管理中配置节点
~~~ 新建job
~~~ 参数化构建
~~~ 配置git仓库(针对jenkinsfile)
三、编写Jenkinsfile
### --- 源码配置文件:
node ("slave02-172.16.1.3") {
# def mvnHome = '/usr/local/maven'
stage('git checkout') {
checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'git@172.16.1.3:/home/git/repos/solo']]])
}
stage('maven build') {
sh '''export JAVA_HOME=/usr/local/java
/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true'''
}
stage('deploy') {
sh '''
JENKINS_NODE_COOKIE=dontkillme
export JAVA_HOME=/usr/local/java
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
'''
}
stage('test') {
sh "curl http://192.168.152.138:8080/status.html"
echo "test ok!!!!!!!"
}
}
### --- 下面为带解释版,但不可以使用,部分注释会造成问题
node ("slave02-172.16.1.3") { # 绑定到该节点构建
# def mvnHome = '/usr/local/maven'
stage('git checkout') { # 拉代码
checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'git@172.16.1.3:/home/git/repos/solo']]])
}
stage('maven build') { # maven build
sh '''export JAVA_HOME=/usr/local/java # 在指定java环境变量
/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true''' # 构建maven包 clean package表示构建之前会清理之前构建的包,-Dmaven.test.skip=true表示跳过单元测试
}
stage('deploy') { # 部署
sh '''
JENKINS_NODE_COOKIE=dontkillme # tomcat启动时会衍生出一些子进程,然后才能成功启动,但是jenkins会在构建结束杀掉tomcat的那些衍生子进程,造成tomcat启动失败,此处加上这个参数可以解决这个问题。
export JAVA_HOME=/usr/local/java
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}\') # 重启tomcat
[ -n "$PID" ] && kill -9 $PID
/bin/bash $TOMCAT_HOME/bin/startup.sh'''
}
stage('test') { # 测试
# sh "curl http://wp.test.com/status.html"
echo "test ok!!!!!!!"
}
}
四、构建
五、访问
附录一:pipeline script from SCM方式下:
### --- 报错现象:
~~~ pipeline script from SCM方式下:
Started by user ***
java.io.FileNotFoundException
at jenkins.plugins.git.GitSCMFile$3.invoke(GitSCMFile.java:167)
at jenkins.plugins.git.GitSCMFile$3.invoke(GitSCMFile.java:159)
at jenkins.plugins.git.GitSCMFileSystem$3.invoke(GitSCMFileSystem.java:193)
...l
Finished: FAILURE
### --- 解决方案:
~~~~ ——>**原因是git工程下,没有找到Script Path路径下的脚本文件**。