Jenkins自动化CI CD流水线之8--流水线自动化发布Java项目

一、前提

插件:Maven Integration plugin

环境:

maven、tomcat

用的博客系统代码:

git clone https://github.com/b3log/solo.git

远端git服务器:

[git@git repos]$ mkdir -p solo
[git@git repos]$ cd solo/
[git@git solo]$ git --bare init
Initialized empty Git repository in /home/git/repos/solo/

本地web:

[root@web solo]# 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@web solo]# git remote rm origin
[root@web solo]# git init
Reinitialized existing Git repository in /root/solo/.git/
[root@web solo]# git remote add origin git@172.16.1.3:/home/git/repos/solo
[root@web solo]# git add .
[root@web solo]# git commit -m "java solo all"
# On branch master
nothing to commit, working directory clean
[root@web solo]# 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为指定的域名)

vim /root/solo/src/main/resources/latke.properties

然后再重新提交上去

二、配置

1)新建job

2)参数化构建

3)配置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/jdk1.8
        /usr/local/maven3.3/bin/mvn clean package -Dmaven.test.skip=true'''
   }
   stage('deploy') {
        sh '''
    JENKINS_NODE_COOKIE=dontkillme
    export JAVA_HOME=/usr/local/jdk1.8
        TOMCAT_NAME=tomcat
        TOMCAT_HOME=/application/$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://solo.test.com/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/jdk1.8   //在指定java环境变量
        /usr/local/maven3.3/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/jdk1.8
        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!!!!!!!"
   }
}

四、构建

五、访问

posted @ 2018-09-05 18:50  Ambition-LC  阅读(1782)  评论(0编辑  收藏  举报