部署jenkins项目
Jenkins持续集成工具,也玩了不少遍,但很少分享这类文档,今天抽空整理了下文档,和大家分享,哈哈
回想一下我们之前的发布流程:
- 本地把项目打包
- 通过FTP等工具,传输到服务器
- 关闭tomcat,把打包的项目放到webapp目录下
- 启动tomcat
如果每次都这么搞的话,不仅慢,而且容易出错
有了jenkins以后,发布流程:
- 用户在jenkins上点击某个按钮进行发布
- jenkins收到发布命令
- jenkins从GIT上把源代码download下来
- jenkins根据你设置的mvn命令进行打包
- jenkins把你打包的好的war/jar工程传输到tomcat的webapps目录下
- tomcat启动
对于程序员而言,只要做两件事:
- 在jenkins上配置某个项目的部署流程
- 在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环境
docker-compose
jenkins
1234567891011121314151617181920[root@localhost ~]# cat jenkins-docker-compose.ymlversion: '3'services:jenkins:image: yfshare/jenkins:2.128container_name: jenkinsports:- 8080:8080- 50000:50000hostname: jenkins.example.comenvironment:JAVA_OPTS: '-Xms1500m -Xmx1500m -XX:MaxPermSize=512m-Djava.awt.headless=true'extra_hosts:jenkins.example.com: 127.0.0.1volumes:- /data/docker_mount/jenkins_home:/var/jenkins_home- /data/docker_mount/maven_repository:/data/maven/repository- /etc/localtime:/etc/localtime:rorestart: always[root@localhost ~]#nexus3_oss
12345678910111213[root@localhost ~]# cat nexus3-docker-compose.ymlversion: '3'services:jenkins:image: sonatype/nexus3:3.12.0container_name: nexus3ports:- 8081:8081volumes:- /data/docker_mount/nexus-data:/nexus-data- /etc/localtime:/etc/localtime:rorestart: always[root@localhost ~]#
应用环境部署
|
|
|
|
|
|
访问Dashboard
打开nexus3_oss,访问http://192.168.1.61:8081
默认的账户密码为:admin
/admin123
Online - Remote Available
表示nexus仓库正在向nexus远端中央库下载公共依赖包Online - Ready to Connect
表示nexus仓库当前没有下载任务(向远端nexus中央库)
打开jenkins,访问http://192.168.1.60:8080
登录密码在日志中查找,在初始化jenkins时也会用到
jenkins安装maven Integration
插件,可以配置全局Maven Options
“系统管理” –“系统设置” –“Maven项目配置”-Xms1024m -Xmx1024m -XX:MaxPermSize=512m
编译项目
登录jenkins后,点击“新建任务”,然后配置jenkins
在Git上添加相应的账户,Jenkins需要通过此账户从git上clone代码进行编译
Jenkins配置完成后,就可以编译项目了
Q & A
在编译过程中,会遇到各种依赖问题,需要找开发童鞋沟通获取相应的依赖包
总结遇到的错误有(部分):
从Maven私服(nexus)正常下载依赖包
1234567[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.pomDownloaded 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.pomDownloaded 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.pomDownloaded 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)在编译时遇到依赖关系
12345678910111213141516171819202122232425# 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 availableDownloading from deploymentRepo: http://192.168.1.61:8081/repository/maven-public/ctc-smscloud/jsonhttp/1.0/jsonhttp-1.0.pomDownloaded 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.jarDownloaded 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的文件名,如下:Dclassifier
字段有的jar依赖包没有就不需要指定该参数DgroupId
字段表示jar包的目录路径,如果有多级目录写法如上Durl
字段为上传到nexus仓库的路径deploymentRepo
字段为maven setting.xml里定义的nexus仓库的密码,具体见settings.xml文件
上传到nexus私服后,再去maven仓库目录删除对应出错的依赖jar包目录后,再次编译即可
编译时再遇到jar包依赖解决方法如上,找开发童鞋获取相应的jar依赖包并上传到nexus仓库。这个上传到nexus仓库开发写好pom.xml文件后应该可以自动上传的,而不需要我们手动上传到nexus仓库
如果不想通过jenkins来测试jar包是否存在依赖问题,可以配置好maven后并在该服务器上手动执行如下命令测试
附上一张jenkins编译失败的图片:
编译成功
披荆斩棘,经过九九八十一难后,jenkins编译成功啦,结果如下
找到jenkins的$WORKSPACE 目录,进入到项目里面的可以看到编译成功后的jar包文件
项目发布
本文出自”Jack Wang Blog”:http://www.yfshare.vip/2018/07/04/部署jenkins项目/