一.Jenkins简介:
jenkins,之前叫做Hudson,是基于Java开发的一种持续集成工具,用户监控秩序重复的工作,包括:
1>持续的软件版本发布测试项目。
2>监控外部调用执行的工作
Jenkins 下载网址:http://jenkins-ci.org/
war包地址 : http://mirrors.jenkins-ci.org/
linux下jenkins安装:
二.centos6.5下svn+tomcat9.0+maven3.3+jenkins实现web项目自动构建与远程发布
目录
1.>实验环境
2.>安装svn(如果没有的话)
3.>安装java
4.>安装apache tomcat
5.>安装maven
6.>安装jenkins
7.>jenkins基本设置
系统管理---系统设置
系统管理--安全设置
系统管理--插件管理
1.实验环境
centos6.5操作系统 192.168.16.11
centos7操作系统下载地址
Java(jdk-8u65-linux-x64.tar.gz)
下载地址:
Apache Tomcat(apache-tomcat-9.0.0.M1.tar.gz)
maven(apache-maven-3.3.9-bin.tar.gz)
jenkins(jenkins.war)
Deploy to container Plugin(deploy-1.0.phi)
以上软件包集合下载地址:http://pan.baidu.com/s/1c1xnUfu
三. 安装svn(略)
四.安装java
[root@localhost ~]# mkdir -p /usr/local/java
[root@localhost ~]# cd soft/
[root@localhost soft]# ls
apache-maven-3.3.9-bin.tar.gz jdk-8u65-linux-x64.tar.gz
apache-tomcat-9.0.0.M1.tar.gz jenkins.war
deploy.hpi mongodb-linux-x86_64-rhel62-3.2.7.tgz
[root@localhost soft]# mv jdk-8u65-linux-x64.tar.gz /usr/local/java
[root@localhost soft]# cd /usr/local/java/
[root@localhost java]# tar -xf jdk-8u65-linux-x64.tar.gz
环境变量配置:
[root@localhost java]# vim /etc/profile
79 export JAVA_HOME=/usr/local/java/jdk1.8.0_65
80 export JRE_HOME=/usr/local/java/jdk1.8.0_65/jre
81 CLASSPATH=.:$JAVA_HOME/lib/dr.jar:$JAVA_HOME/lib/tools.jar:
82 export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
[root@localhost java]# . /etc/profile
查看是否安装成功:
[root@localhost java]# java -version
java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)
[root@localhost java]# javac -version
javac 1.8.0_65
五.安装apache tomcat
[root@localhost java]# cd
[root@localhost ~]# cd soft/
[root@localhost soft]# tar -xf apache-tomcat-9.0.0.M1.tar.gz -C /usr/src
[root@localhost soft]# mkdir -p /usr/local/apache-tomcat
[root@localhost soft]# mv /usr/src/apache-tomcat-9.0.0.M1/ /usr/local/apache-tomcat/
环境变量配置:
[root@localhost soft]# vim /etc/profile
export JAVA_HOME=/usr/local/java/jdk1.8.0_65
export JRE_HOME=/usr/local/java/jdk1.8.0_65/jre
export CATALINA_BASS=/usr/local/apache-tomcat/apache-tomcat-9.0.0.M1
export CATALINA_HOME=/usr/local/apache-tomcat/apache-tomcat-9.0.0.M1
CLASSPATH=.:$JAVA_HOME/lib/dr.jar:$JAVA_HOME/lib/tools.jar:
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin:$CATALINA_BASE:$CATALINA_HOME
[root@localhost soft]# . /etc/profile
设置管理员账号和密码:
[root@localhost apache-tomcat-9.0.0.M1]# pwd
/usr/local/apache-tomcat/apache-tomcat-9.0.0.M1
[root@localhost apache-tomcat-9.0.0.M1]# ls
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
[root@localhost apache-tomcat-9.0.0.M1]# cd conf/
[root@localhost conf]# ls
catalina.policy context.xml server.xml tomcat-users.xsd
catalina.properties logging.properties tomcat-users.xml web.xml
[root@localhost conf]# cp tomcat-users.xml tomcat-users.xml.bak
[root@localhost conf]# ls
catalina.policy context.xml server.xml tomcat-users.xml.bak web.xml
catalina.properties logging.properties tomcat-users.xml tomcat-users.xsd
[root@localhost conf]# vim tomcat-users.xml //找到如下内容
32 <!--
33 <role rolename="tomcat"/>
34 <role rolename="role1"/>
35 <user username="tomcat" password="tomcat" roles="tomcat"/>
36 <user username="both" password="tomcat" roles="tomcat,role1"/>
37 <user username="role1" password="tomcat" roles="role1"/>
38 -->
修改成如下:
33 <role rolename="manager-gui"/>
34 <role rolename="admin-gui"/>
35 <user username="hostadmin" password="123456" roles="admin-gui"/>
36 <user username="admin" password="huozhe" roles="manager-gui,admin-gui"/>
37 -->
说明:admin用户可以访问Server Status,Manager App,Host Manager,hostadmin止咳访问Host Manager
启动Apache Tomcat
[root@localhost conf]# cd /usr/local/apache-tomcat/apache-tomcat-9.0.0.M1/
[root@localhost apache-tomcat-9.0.0.M1]# ls
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
[root@localhost apache-tomcat-9.0.0.M1]# cd bin/
[root@localhost bin]# ls
bootstrap.jar configtest.bat setclasspath.sh tomcat-native.tar.gz
catalina.bat configtest.sh shutdown.bat tool-wrapper.bat
catalina.sh daemon.sh shutdown.sh tool-wrapper.sh
catalina-tasks.xml digest.bat startup.bat version.bat
commons-daemon.jar digest.sh startup.sh version.sh
commons-daemon-native.tar.gz setclasspath.bat tomcat-juli.jar
[root@localhost bin]# sh startup.sh
Using CATALINA_BASE: /usr/local/apache-tomcat/apache-tomcat-9.0.0.M1
Using CATALINA_HOME: /usr/local/apache-tomcat/apache-tomcat-9.0.0.M1
Using CATALINA_TMPDIR: /usr/local/apache-tomcat/apache-tomcat-9.0.0.M1/temp
Using JRE_HOME: /usr/local/java/jdk1.8.0_65/jre
Using CLASSPATH: /usr/local/apache-tomcat/apache-tomcat-9.0.0.M1/bin/bootstrap.jar:/usr/local/apache-tomcat/apache-tomcat-9.0.0.M1/bin/tomcat-juli.jar
Tomcat started.
浏览器访问测试:默认开发的端口是8080
六.安装maven
[root@localhost ~]# mkdir -p /usr/local/maven
[root@localhost ~]# cd soft/
[root@localhost soft]# tar xf apache-maven-3.3.9-bin.tar.gz -C /usr/src/
[root@localhost soft]# mv /usr/src/apache-maven-3.3.9/ /usr/local/maven/
环境变量设置:
[root@localhost ~]# vim /etc/profile
export JAVA_HOME=/usr/local/java/jdk1.8.0_65
export JRE_HOME=/usr/local/java/jdk1.8.0_65/jre
export CATALINA_BASS=/usr/local/apache-tomcat/apache-tomcat-9.0.0.M1
export CATALINA_HOME=/usr/local/apache-tomcat/apache-tomcat-9.0.0.M1
export MAVEN_HOME=/usr/local/maven/apache-maven-3.3.9
CLASSPATH=.:$JAVA_HOME/lib/dr.jar:$JAVA_HOME/lib/tools.jar:
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin:$CATALINA_BASE:$CATALINA_HOME:$MAVEN_HOME/bin
[root@localhost ~]# . /etc/profile
查看安装是否成功:
[root@localhost ~]# mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: /usr/local/maven/apache-maven-3.3.9
Java version: 1.8.0_65, vendor: Oracle Corporation
Java home: /usr/local/java/jdk1.8.0_65/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "2.6.32-431.el6.x86_64", arch: "amd64", family: "unix"
七.安装jenkins
[root@localhost ~]# cd soft/
[root@localhost soft]# ls
apache-maven-3.3.9-bin.tar.gz deploy.hpi mongodb-linux-x86_64-rhel62-3.2.7.tgz
apache-tomcat-9.0.0.M1.tar.gz jenkins.war
[root@localhost soft]# cp jenkins.war /usr/local/apache-tomcat/apache-tomcat-9.0.0.M1/webapps/
重启apache-tomcat服务器
[root@localhost soft]# sh /usr/local/apache-tomcat/apache-tomcat-9.0.0.M1/bin/shutdown.sh
Using CATALINA_BASE: /usr/local/apache-tomcat/apache-tomcat-9.0.0.M1
Using CATALINA_HOME: /usr/local/apache-tomcat/apache-tomcat-9.0.0.M1
Using CATALINA_TMPDIR: /usr/local/apache-tomcat/apache-tomcat-9.0.0.M1/temp
Using JRE_HOME: /usr/local/java/jdk1.8.0_65/jre
Using CLASSPATH: /usr/local/apache-tomcat/apache-tomcat-9.0.0.M1/bin/bootstrap.jar:/usr/local/apache-tomcat/apache-tomcat-9.0.0.M1/bin/tomcat-juli.jar
[root@localhost soft]# sh /usr/local/apache-tomcat/apache-tomcat-9.0.0.M1/bin/startup.sh
Using CATALINA_BASE: /usr/local/apache-tomcat/apache-tomcat-9.0.0.M1
Using CATALINA_HOME: /usr/local/apache-tomcat/apache-tomcat-9.0.0.M1
Using CATALINA_TMPDIR: /usr/local/apache-tomcat/apache-tomcat-9.0.0.M1/temp
Using JRE_HOME: /usr/local/java/jdk1.8.0_65/jre
Using CLASSPATH: /usr/local/apache-tomcat/apache-tomcat-9.0.0.M1/bin/bootstrap.jar:/usr/local/apache-tomcat/apache-tomcat-9.0.0.M1/bin/tomcat-juli.jar
Tomcat started.
浏览器访问jenkins:
八.jenkins基本设置:
1)系统管理--系统设置
2)系统管理--安全设置
保存之后出现如下界面:
注册并登陆 :
3)系统管理--插件管理
安装方法1. 在线安装
系统管理----管理插件---可选插件--过滤,输入“Deploy to container Plugin”
缺点:可能无法下载,GFW太强大了
安装方法2:本地安装
下载插件,插件集合下载地址:
Deploy to container Plugin下载地址:
系统管理->管理插件->高级->上传插件->浏览,打开.phi插件->上传
查看是否安装成功:
九.自动创建任务与自动部署
点击 【新建】、【创建一个新任务】(从未创建过任务的情况)
如上图,填写好item名称,点击【OK】
代码结构如下:
选择Subversion,填写版本库代码url
如上,提示No credential,点击 enter credential,打开如下界面
如上,填写代码库所在url及用户名称和密码,点击【OK】提交
返回到刚才的页面,刷新,重新填写,结果如下
构建触发器
说明:
1)Poll SCM:定时检查源码变更(根据SCM软件的版本号),如果有更新就checkout最新code下来,然后执行构建动作。
2)Build periodically:按给定周期,定时构建(它不管源码是否发生变化)
示例:
*/60 * * * * (意为每60分钟检查一次源码变化)
0 2 * * * (每天2:00 执行一次构建)
这里和linux crontab文件配置是一致的。
参考连接:
[root@localhost workspace]# pwd
/root/.jenkins/jobs/test_project1/workspace
[root@localhost workspace]# ll
total 56
-rw-r-----. 1 root root 16629 Dec 20 17:21 pom.xml
-rw-r-----. 1 root root 3235 Dec 20 17:21 readme.txt
drwxr-x---. 2 root root 4096 Dec 20 17:21 review
drwxr-x---. 4 root root 4096 Dec 20 17:21 src
drwxr-x---. 8 root root 4096 Dec 20 17:21 target
-rw-r-----. 1 root root 16664 Dec 20 17:21 版本变更记录.txt
注意:这里的pom设置是有讲究的,参考连接:
如上,Post steps,选择仅build成功时才运行Post Steps
如上,点击【增加构建后操作步骤】,选择Deploy war/ear to a container,设置远程发布项目
说明:
1) 构建时会在目录:/root/.jenkins/jobs/item_name/workspace/target下生成filename.war文件,本例中为:
/root/.jenkins/jobs/test_project1/workspace/target/appServer.war
2) WAR/EAR files:填写.war、.ear文件所在的相对路径
3) Context Path:填写要发布至远程服务器的位置,通常是tomcat的webapps
4) 通常不勾选【Deploy on failure】,即构建失败则不发布
5) 这里也可以用Publish over SSH来实现远程发布,参考连接:
如上,点击 Add Contianer,可以选择容器类型,这里选择Tomcat 7.x,然后填写入Tomcat 管理员(具有manager-gui角色的tomcat用户),密码,Tomcat连接
最后提交,如下
如下,点击右侧按钮,开始第一次构建
点击连接查看Console Output
如下,第一次会根据pom.xml下载相关文件
附pdf版本下载: