說明:部署傳統的web應用(.war)html
準備工做:java
(1) 系統環境:CentOS7web
(2) 安裝JDK:https://www.cnblogs.com/myitnews/p/11493847.htmljson
(3) 安裝Tomcat:https://www.cnblogs.com/myitnews/p/11493980.htmlapi
在tomcat-users.xml中配置訪問服務器的用戶名和密碼瀏覽器
(4) 安裝Maven:https://www.cnblogs.com/myitnews/p/11492967.htmltomcat
(5) 搭建SVN服務器:https://www.cnblogs.com/myitnews/p/11492187.html安全
1、Jenkins 主體程序安裝配置
下載地址:https://jenkins.io/download/服務器
1. 把 jenkins.war (v2.176.3)放在 Tomcat 解壓目錄 /webapps 目錄下網絡
2. 打開 Tomcat 解壓目錄/server.xml 修改 URL 地址的編碼解碼字符集
vi /usr/local/tomcat7/conf/server.xml
<Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
3. 啓動 Tomcat 並經過瀏覽器訪問:http://192.168.178.5:8080/jenkins
![](http://static.javashuo.com/static/loading.gif)
#密文同時也是 admin 帳號的密碼 cat /root/.jenkins/secrets/initialAdminPassword
5. 選擇插件安裝方式
輸入密文後,初始化後,選擇插件安裝方式(選擇哪一種方式都不會對後續操做有太大影響。由於有須要的插件咱們能夠在後續有針對性的安裝)。在這裏選擇「安裝推薦的插件」。
開始安裝插件以下:
打×的插件是因爲網絡傳輸致使的安裝失敗,後面再從新安裝便可。
※注意:這個步驟中若是選擇了安裝插件則 Linux 必須可以聯網。
![](http://static.javashuo.com/static/loading.gif)
6. 新建帳號或以管理員身份繼續
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
2、系統初始化配置
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
3、建立工程
package com.linhw.demo; public class Apple { private String apple = "jenkins test......"; public String getApple() { return apple; } }
<%@page import="com.linhw.demo.Apple"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <%=new Apple().getApple() %> </body> </html>
# 建立目錄 mkdir apple # 建立apple倉庫 svnadmin create apple
(3) 修改SVN倉庫相關的配置
authz :設置用戶讀寫權限
passwd:設置用戶名和密碼
svnserve.conf:設置受權訪問
(4) 在Eclipse下建立SVN倉庫
倉庫地址:svn://192.168.178.5/apple
(5) 在Eclipse下將Apple項目提交到SVN倉庫中
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
(1) 獲取crubm值
因爲2.0以上版本Jenkins增長了防跨站攻擊功能,所以在每次訪問Jenkins api的時候都須要將crubm參數帶上,不然會提示認證不經過。
瀏覽器訪問:http://ip:port/crumbIssuer/api/json
舉例:http://192.168.178.5:8080/jenkins/crumbIssuer/api/json
{"_class":"hudson.security.csrf.DefaultCrumbIssuer","crumb":"1fe463da428285deb2a0ee0181c0b44f","crumbRequestField":"Jenkins-Crumb"}
(2) curl命令說明
Linux 的 curl 命令用來發送 HTTP 請求。
-X 參數:指定請求方式
-v 參數:顯示響應結果
-u 參數:攜帶用戶名/密碼
-H 參數:攜帶請求消息頭信息
格式:curl -X post -v -u [Jenkins 用戶名]:[Jenkins 密碼] -H "請求消息頭信息" http://[服務器 IP 地址]:[服務器端口 號]/jenkins/job/[Jenkins 項目名稱]/build?token=[身份驗證令牌]
舉例:curl -X post -v -u admin:d769b039ccdc4b4dbe5624689aa81ad0 -H "Jenkins-Crumb:1fe463da428285deb2a0ee0181c0b44f" http://192.168.178.5:8080/jenkins/job/Apple/build?token=LHW_APPLE
說明:-H能夠不寫,若是認證不經過,再加上
在Linux命令行環境中輸入上面的curl命令,發現會再次構建。
9. 編輯SVN鉤子程序
# 進入svn倉庫 cd /var/svn/svnrepos/apple # 進入hooks文件夾 cd hooks # 查看文件 ls -l
#複製,千萬不能帶後綴名,直接post-commit cp post-commit.tmpl post-commit
編輯:vi post-commit
能夠將內容都註釋掉,而後在末尾添加咱們上面的curl命令。
保存退出,給post-commit受權執行
chmod 755 post-commit
這樣咱們在提交代碼後,就會觸發鉤子程序,鉤子程序通知Jenkins構建。
10. 測試驗證整個自動化持續集成流程
在Eclipse中提交代碼,查看JenKins的構建界面,用瀏覽器訪問項目。
測試過程過程當中,可能會發現Jenkins構建的老是上一個版本的,並非最新的(在一些較低版本會出現),怎麼解決呢?
在「源碼管理」中的Repository URL爲:svn://192.168.178.5/apple/Apple@HEAD