Jenkins持续集成工具,也玩了不少遍,但很少分享这类文档,今天抽空整理了下文档,和大家分享,哈哈

回想一下我们之前的发布流程:

  1. 本地把项目打包
  2. 通过FTP等工具,传输到服务器
  3. 关闭tomcat,把打包的项目放到webapp目录下
  4. 启动tomcat
    如果每次都这么搞的话,不仅慢,而且容易出错

有了jenkins以后,发布流程:

  1. 用户在jenkins上点击某个按钮进行发布
  2. jenkins收到发布命令
  3. jenkins从GIT上把源代码download下来
  4. jenkins根据你设置的mvn命令进行打包
  5. jenkins把你打包的好的war/jar工程传输到tomcat的webapps目录下
  6. tomcat启动

对于程序员而言,只要做两件事:

  1. 在jenkins上配置某个项目的部署流程
  2. 在jenkins上点击某个项目的部署按钮,进行一键部署

环境说明

环境:
   Centos 7.5
   nexus 3.12.0
   jenkins 2.128
   maven 3.5.3

项目 IP 备注
jenkins 192.168.1.60 基于docker部署
maven 192.168.1.60 和jenkins集成在一起
nexus3_oss 192.168.1.61 基于docker部署

环境部署

安装docker环境

1
2
[root@localhost ~]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
[root@localhost ~]# yum install -y docker-ce

docker-compose

  • jenkins

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    [root@localhost ~]# cat jenkins-docker-compose.yml
    version: '3'
    services:
    jenkins:
    image: yfshare/jenkins:2.128
    container_name: jenkins
    ports:
    - 8080:8080
    - 50000:50000
    hostname: jenkins.example.com
    environment:
    JAVA_OPTS: '-Xms1500m -Xmx1500m -XX:MaxPermSize=512m-Djava.awt.headless=true'
    extra_hosts:
    jenkins.example.com: 127.0.0.1
    volumes:
    - /data/docker_mount/jenkins_home:/var/jenkins_home
    - /data/docker_mount/maven_repository:/data/maven/repository
    - /etc/localtime:/etc/localtime:ro
    restart: always
    [root@localhost ~]#
  • nexus3_oss

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    [root@localhost ~]# cat nexus3-docker-compose.yml
    version: '3'
    services:
    jenkins:
    image: sonatype/nexus3:3.12.0
    container_name: nexus3
    ports:
    - 8081:8081
    volumes:
    - /data/docker_mount/nexus-data:/nexus-data
    - /etc/localtime:/etc/localtime:ro
    restart: always
    [root@localhost ~]#

应用环境部署

1
2
[root@localhost ~]# docker-compose -f jenkins-docker-compose.yml up -d
[root@localhost ~]# docker-compose -f nexus3-docker-compose.yml up -d

1
2
3
4
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
56e49974e39d yfshare/jenkins:2.128 "/sbin/tini -- /usr/…" 2 days ago Up 42 hours 0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp jenkins
[root@localhost ~]#
1
2
3
4
5
[root@localhost ~]# docker exec -it jenkins /bin/bash
root@jenkins:/# ps -ef |grep -i jenkins | grep -iv grep
root 1 0 0 Jul05 ? 00:00:01 /sbin/tini -- /usr/local/bin/jenkins.sh
root 5 1 0 Jul05 ? 00:02:04 java -Duser.home=/var/jenkins_home -Xms1500m -Xmx1500m -XX:MaxPermSize=512m -Djava.awt.headless=true -jar /usr/share/jenkins/jenkins.war
root@jenkins:/#
1
2
3
4
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
28751b5a7cdc sonatype/nexus3:3.12.0 "sh -c ${SONATYPE_DI…" 20 hours ago Up 19 hours 0.0.0.0:8081->8081/tcp nexus3
[root@localhost ~]#
访问Dashboard

打开nexus3_oss,访问http://192.168.1.61:8081
默认的账户密码为:admin/admin123
nexus3_oss_dashboard

Online - Remote Available表示nexus仓库正在向nexus远端中央库下载公共依赖包
Online - Ready to Connect表示nexus仓库当前没有下载任务(向远端nexus中央库)
nexus3_oss_dashboard

打开jenkins,访问http://192.168.1.60:8080
登录密码在日志中查找,在初始化jenkins时也会用到
jenkins_login

jenkins安装maven Integration插件,可以配置全局Maven Options
“系统管理” –“系统设置” –“Maven项目配置”
-Xms1024m -Xmx1024m -XX:MaxPermSize=512m
maven_option

编译项目

登录jenkins后,点击“新建任务”,然后配置jenkins
jenkins_CreateJob
jenkins_CreateJob

在Git上添加相应的账户,Jenkins需要通过此账户从git上clone代码进行编译
jenkins_CreateJob
jenkins_CreateJob
jenkins_CreateJob
jenkins_CreateJob

Jenkins配置完成后,就可以编译项目了

Q & A

在编译过程中,会遇到各种依赖问题,需要找开发童鞋沟通获取相应的依赖包
总结遇到的错误有(部分):

  1. 从Maven私服(nexus)正常下载依赖包

    1
    2
    3
    4
    5
    6
    7
    [INFO] Scanning for projects...
    Downloading from deploymentRepo: http://192.168.1.61:8081/repository/maven-public/org/springframework/boot/spring-boot-starter-parent/1.5.1.RELEASE/spring-boot-starter-parent-1.5.1.RELEASE.pom
    Downloaded from deploymentRepo: http://192.168.1.61:8081/repository/maven-public/org/springframework/boot/spring-boot-starter-parent/1.5.1.RELEASE/spring-boot-starter-parent-1.5.1.RELEASE.pom (7.4 kB at 986 B/s)
    Downloading from deploymentRepo: http://192.168.1.61:8081/repository/maven-public/org/springframework/boot/spring-boot-dependencies/1.5.1.RELEASE/spring-boot-dependencies-1.5.1.RELEASE.pom
    Downloaded from deploymentRepo: http://192.168.1.61:8081/repository/maven-public/org/springframework/boot/spring-boot-dependencies/1.5.1.RELEASE/spring-boot-dependencies-1.5.1.RELEASE.pom (89 kB at 12 kB/s)
    Downloading from deploymentRepo: http://192.168.1.61:8081/repository/maven-public/com/fasterxml/jackson/jackson-bom/2.8.6/jackson-bom-2.8.6.pom
    Downloaded from deploymentRepo: http://192.168.1.61:8081/repository/maven-public/com/fasterxml/jackson/jackson-bom/2.8.6/jackson-bom-2.8.6.pom (10 kB at 7.8 kB/s)
  2. 在编译时遇到依赖关系

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    # mvn -X package -P pre clean install -Dmaven.test.skip=true
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ---------------------< com.recycle:recycle-common >---------------------
    [INFO] Building recycle-common 1.0-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [WARNING] The POM for fakepath:json-lib:jar:jdk15:2.4 is missing, no dependency information available
    Downloading from deploymentRepo: http://192.168.1.61:8081/repository/maven-public/ctc-smscloud/jsonhttp/1.0/jsonhttp-1.0.pom
    Downloaded from deploymentRepo: http://192.168.1.61:8081/repository/maven-public/ctc-smscloud/jsonhttp/1.0/jsonhttp-1.0.pom (391 B at 1.8 kB/s)
    Downloading from deploymentRepo: http://192.168.1.61:8081/repository/maven-public/ctc-smscloud/jsonhttp/1.0/jsonhttp-1.0.jar
    Downloaded from deploymentRepo: http://192.168.1.61:8081/repository/maven-public/ctc-smscloud/jsonhttp/1.0/jsonhttp-1.0.jar (5.1 kB at 212 kB/s)
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 3.857 s
    [INFO] Finished at: 2018-06-28T09:58:07Z
    [INFO] ------------------------------------------------------------------------
    [WARNING] The requested profile "pre" could not be activated because it does not exist.
    [ERROR] Failed to execute goal on project recycle-common: Could not resolve dependencies for project com.recycle:recycle-common:jar:1.0-SNAPSHOT: Failure to find fakepath:json-lib:jar:jdk15:2.4 in http://192.168.1.61:8081/repository/maven-public/ was cached in the local repository, resolution will not be reattempted until the update interval of deploymentRepo has elapsed or updates are forced -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException

与开发童鞋沟通获取相应的依赖包上传到nexus仓库

上传到nexus命令
需要先安装Maven和配置settings.xml(maven/conf/settings.xml)
settings.xml文件配置 参考这里

通过报错可以看到,缺少json-lib-2.4-jdk15.jar这个依赖jar包,且json-lib-2.4-jdk15.jar这个依赖包存放的路径为:fakepath/json-lib/2.4/json-lib-2.4-jdk15.jar
手动上传到nexus仓库需要各个参数指定jar的文件名,如下:
nexus_jar包命名规范
Dclassifier字段有的jar依赖包没有就不需要指定该参数
DgroupId字段表示jar包的目录路径,如果有多级目录写法如上
Durl字段为上传到nexus仓库的路径
deploymentRepo字段为maven setting.xml里定义的nexus仓库的密码,具体见settings.xml文件

1
mvn deploy:deploy-file -DgroupId=fakepath -DartifactId=json-lib -Dversion=2.4 -Dclassifier=jdk15 -Dpackaging=jar -Dfile=/data/code/json-lib-2.4-jdk15.jar -Durl=http://192.168.1.61:8081/repository/maven-releases/ -DrepositoryId="deploymentRepo"

上传到nexus私服后,再去maven仓库目录删除对应出错的依赖jar包目录后,再次编译即可

1
2
# cd /data/docker_mount/maven_repository
# rm -rf fakepath/json-lib/

编译时再遇到jar包依赖解决方法如上,找开发童鞋获取相应的jar依赖包并上传到nexus仓库。这个上传到nexus仓库开发写好pom.xml文件后应该可以自动上传的,而不需要我们手动上传到nexus仓库

如果不想通过jenkins来测试jar包是否存在依赖问题,可以配置好maven后并在该服务器上手动执行如下命令测试

1
2
3
cd $WORKSPACE/recycle-back/
mvn clean -P pre
mvn package -P pre clean install -Dmaven.test.skip=true

附上一张jenkins编译失败的图片:
jenkins_build_fail

编译成功

披荆斩棘,经过九九八十一难后,jenkins编译成功啦,结果如下
jenkins_built_result

找到jenkins的$WORKSPACE 目录,进入到项目里面的可以看到编译成功后的jar包文件

1
2
3
4
5
6
7
8
9
10
11
# ls recycle-*/target/*.jar | grep -iv 'dubbo-privider'
recycle-activity/target/recycle-activity.jar
recycle-common/target/recycle-common-1.0-SNAPSHOT.jar
recycle-front/target/recycle-front.jar
recycle-message/target/recycle-message.jar
recycle-product/target/recycle-product.jar
recycle-risk/target/recycle-risk.jar
recycle-task/target/recycle-task.jar
recycle-trade/target/recycle-trade.jar
recycle-user/target/recycle-user.jar
#

项目发布

Jenkins_ExecuteShell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash
BUILD_ID=DONTKILLME
cd $WORKSPACE/king-back/
mvn clean -P pre
mvn package -P pre clean install -Dmaven.test.skip=true
result=$?
if [ $result == '0' ];then
find . -type f -iname "king*.jar" | egrep -iv 'common|dubbo-privider|api' | xargs -i scp {} user@ip:/data/king/project/
find . -type f -iname "king*.war" | egrep -iv 'common|dubbo-privider|api' | xargs -i scp {} user@ip:/data/king/project/
ssh -fn user@ip "ps -ef | grep 'king-front.jar' |grep -v grep |awk '{print \$2}' |xargs -i -i kill {}"
ssh -fn user@ip "ps -ef | grep 'king-mis.war' |grep -v grep |awk '{print \$2}' |xargs -i kill {}"
ssh -fn user@ip "ps -ef | grep 'king-task.jar' |grep -v grep |awk '{print \$2}' |xargs -i kill {}"
ssh -fn user@ip "ps -ef | grep 'king-trade.jar' |grep -v grep |awk '{print \$2}' |xargs -i kill {}"
ssh -fn user@ip "ps -ef | grep 'king-risk.jar' |grep -v grep |awk '{print \$2}' |xargs -i kill {}"
ssh -fn user@ip "ps -ef | grep 'king-user.jar' |grep -v grep |awk '{print \$2}' |xargs -i kill {}"
ssh -fn user@ip "ps -ef | grep 'king-activity.jar' |grep -v grep |awk '{print \$2}' |xargs -i kill {}"
ssh -fn user@ip "ps -ef | grep 'king-product.jar' |grep -v grep |awk '{print \$2}' |xargs -i kill {}"
ssh -fn user@ip "ps -ef | grep 'king-message.jar' |grep -v grep |awk '{print \$2}' |xargs -i kill {}"
ssh -fn user@ip "nohup java -jar -Xms350m -Xmx350m /data/king/project/king-message.jar >/dev/null 2>&1 &"
ssh -fn user@ip "nohup java -jar -Xms350m -Xmx350m /data/king/project/king-user.jar >/dev/null 2>&1 &"
ssh -fn user@ip "nohup java -jar -Xms350m -Xmx350m /data/king/project/king-trade.jar >/dev/null 2>&1 &"
ssh -fn user@ip "nohup java -jar -Xms350m -Xmx350m /data/king/project/king-risk.jar >/dev/null 2>&1 &"
ssh -fn user@ip "nohup java -jar -Xms350m -Xmx350m /data/king/project/king-task.jar >/dev/null 2>&1 &"
ssh -fn user@ip "nohup java -jar -Xms350m -Xmx350m /data/king/project/king-product.jar >/dev/null 2>&1 &"
ssh -fn user@ip "nohup java -jar -Xms350m -Xmx350m /data/king/project/king-activity.jar >/dev/null 2>&1 &"
ssh -fn user@ip "nohup java -jar -Xms600m -Xmx600m /data/king/project/king-mis.war >/dev/null 2>&1 &"
ssh -fn user@ip "nohup java -jar -Xms350m -Xmx350m /data/king/project/king-front.jar >/dev/null 2>&1 &"
ssh -fn user@ip "ps -ef | grep -i king | egrep -iv 'grep|color'"
else
echo '编译失败'
exit 1
fi

jenkins_build_fail
jenkins_build_success


本文出自”Jack Wang Blog”:http://www.yfshare.vip/2018/07/04/部署jenkins项目/