一.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)
三. 安裝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版本下載: