前言:本文示例所涉及的工具配置為jenkins+tomcat8+jdk8+maven3.6.1+git
一.安裝
這裡以jenkins.war為執行個體,官方也提供了mac,rpm,windows等版本,對背景開發來說,可能war包方式比較親切。
官方文檔見:https://jenkins.io/zh/download/
下載下傳:wget http://mirrors.jenkins.io/war/latest/jenkins.war
下載下傳完畢後,把它丢到tomcat裡啟動就行了,對于部署而言,暫時不需要做任何更改
我們啟動後通路 http://yourhost:ip/jenkins,第一次開打開會出現如下提示資訊:
【為了確定管理者安全地安裝 Jenkins,密碼已寫入到日志中(不知道在哪裡?)該檔案在伺服器上:/root/.jenkins/secrets/initialAdminPassword】
照做找到管理者密碼登入即可。
進去後會提示選擇插件,該步驟暫時不用理他,jenkins裡面有個【Manage Plugins】的子產品,到時需要什麼插件該子產品下載下傳安裝即可。
登入成功後看到主界面:

(1)進來後,先建立個使用者,或者修改admin密碼,友善登入,菜單Manage Jenkins->Manage Users(略過)
(2)進入菜單Manage Jenkins->ConfigureSystem,設定jenkins所在的ip:port
(3)進入菜單Manage Jenkins->Configure Global Security,以下設定一定要鈎上,不然一堆奇葩的問題!!!
(4) 設定jenkins所在伺服器的jdk路徑,建議使用oracle的jdk,jenkins也提供了自動下載下傳安裝的選項,也可以手工安裝配置
(5)設定jenkins所在伺服器上maven的路徑
(6)jenkins通過maven建構項目,預設安裝情況下是沒有安裝jenkins的maven插件的,需要手工安裝,進入菜單Manage Jenkins->Manage Plugins,搜尋Maven Integration plugin,安裝即可;(如果需要通過SSH傳輸檔案之類的還需要添加【Publish Over SSH Plugin】)
jenkins可以自動從版本管理工具(如git,svn等)下載下傳代碼然後通過maven建構,預設情況也是沒有該插件,需要下載下傳插件GIT Integration plugin,當建構完畢,我們想jenkins自動部署到遠端伺服器tomcat上,還需要下載下傳個【Deploy to container plugin】插件
(7)後續我們需要登入tomcat,登入git之類的,需要使用者密碼,jenkins提供了一個子產品來管理這些憑據,進入管理憑據,設定後續需要用到的各平台的賬号密碼
二:開始建構
前面準備活動已經完成,現在開始建構我們的第一個應用
回到主菜單,點選add item->建構maven項目,名稱為test,然後進去到項目配置頁面
(1)配置git位址
(2) 配置遠端tomcat
(這裡需要注意紅框選項如果沒有安裝deploy tocontainer插件是不會出現的)這裡選擇該選項
配置tomcat ip位址時,最後不要加斜杠,否則報錯,正确示例:http://127.0.0.1:8080
(3)其他觸發器之類的設定這裡不做介紹。
(4)開始建構
(自動建構,持續內建,要求tomcat必須處于啟動狀态!否則無法自動釋出)
點選完右邊按鈕後點選項目名稱test,進入建構詳情頁面,然後點選Console Output,這裡會實時列印出建構的日志
這個時候你會發現可能控制台上下面等錯誤
org.codehaus.cargo.container.tomcat.internal.TomcatManagerException: The username
you provided is not allowed to use the text-based Tomcat Manager (error 403)
ava.io.FileNotFoundException: http://88.88.88.88:8080/manager/text/list
第一個是說明jenkins調用tomcat 的文本接口時認證,進而無法執行一些指令(詳情請了解tomcat manager子產品)
第二個是說明tomcat manager子產品的檔案不存在(很可能是你下載下傳完tomcat後,你把webapps下的manager檔案夾等删除掉了,切記,不能删!!!)
解決方法:
進入tomcat,修改/conf/tomcat-user.xml檔案
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<tomcat-users xmlns="http://tomcat.apache.org/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
version="1.0">
<!--
NOTE: By default, no user is included in the "manager-gui" role required
to operate the "/manager/html" web application. If you wish to use this app,
you must define such a user - the username and password are arbitrary. It is
strongly recommended that you do NOT use one of the users in the commented out
section below since they are intended for use with the examples web
application.
-->
<!--
NOTE: The sample user and role entries below are intended for use with the
examples web application. They are wrapped in a comment and thus are ignored
when reading this file. If you wish to configure these users for use with the
examples web application, do not forget to remove the <!.. ..> that surrounds
them. You will also need to set the passwords to something appropriate.
-->
<!--
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
<user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
<user username="role1" password="<must-be-changed>" roles="role1"/>
-->
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<role rolename="manager-script"/>
<user username="jianlejun" password="123456" roles="manager-script,admin-gui,manager-gui"/>
</tomcat-users>
增加 manager-script角色的使用者(這個是一定必須要的)沒有它就調不了tomcat對外的文本接口
tomcat8還需要在修改webapps/manager/META-INF/context.xml的内容
<Context antiResourceLocking="false" privileged="true" >
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="^.*$" />
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
(這裡留意allow屬性,預設是隻允許本地通路manage子產品)
此時再點選建構,即可成功建構并自動釋出的遠端tomcat伺服器上,并自動啟動tomcat
最終的頁面如下:
Started by user zhoudada
Running as SYSTEM
Building in workspace /root/.jenkins/workspace/test
using credential b4584322-f92f-486e-992e-0ec5d5de1f4d
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://gitee.com/jianlejun/WMS.git # timeout=10
Fetching upstream changes from https://gitee.com/jianlejun/WMS.git
> git --version # timeout=10
using GIT_ASKPASS to set credentials git login
> git fetch --tags --progress https://gitee.com/jianlejun/WMS.git +refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 3828ceb6ed0b5a37ddfe3d649889689673c86f71 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f 3828ceb6ed0b5a37ddfe3d649889689673c86f71
Commit message: "."
> git rev-list --no-walk 3828ceb6ed0b5a37ddfe3d649889689673c86f71 # timeout=10
Parsing POMs
Established TCP socket on 43597
[test] $ /root/.jenkins/tools/hudson.model.JDK/sunjdk1.8/bin/java -cp /root/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven35-agent-1.13.jar:/usr/local/software/maven/boot/plexus-classworlds-2.6.0.jar:/usr/local/software/maven/conf/logging jenkins.maven3.agent.Maven35Main /usr/local/software/maven/ /usr/local/software/tmp_test/tomcat2/webapps/jenkins/WEB-INF/lib/remoting-3.33.jar /root/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven35-interceptor-1.13.jar /root/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.13.jar 43597
<===[JENKINS REMOTING CAPACITY]===>channel started
Executing Maven: -B -f /root/.jenkins/workspace/test/pom.xml install
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------------< com.ken:WMS >-----------------------------
[INFO] Building WMS 1.0
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ WMS ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 7 resources
[INFO] Copying 14 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ WMS ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ WMS ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /root/.jenkins/workspace/test/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ WMS ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /root/.jenkins/workspace/test/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ WMS ---
[JENKINS] Recording test results
[INFO]
[INFO] --- maven-war-plugin:2.2:war (default-war) @ WMS ---
[INFO] Packaging webapp
[INFO] Assembling webapp [WMS] in [/root/.jenkins/workspace/test/target/WMS]
[INFO] Processing war project
[INFO] Copying webapp resources [/root/.jenkins/workspace/test/src/main/webapp]
[INFO] Webapp assembled in [493 msecs]
[INFO] Building war: /root/.jenkins/workspace/test/target/WMS.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ WMS ---
[INFO] Installing /root/.jenkins/workspace/test/target/WMS.war to /root/.m2/repository/com/ken/WMS/1.0/WMS-1.0.war
[INFO] Installing /root/.jenkins/workspace/test/pom.xml to /root/.m2/repository/com/ken/WMS/1.0/WMS-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.710 s
[INFO] Finished at: 2019-06-27T23:37:02+08:00
[INFO] ------------------------------------------------------------------------
Waiting for Jenkins to finish collecting data
[JENKINS] Archiving /root/.jenkins/workspace/test/pom.xml to com.ken/WMS/1.0/WMS-1.0.pom
[JENKINS] Archiving /root/.jenkins/workspace/test/target/WMS.war to com.ken/WMS/1.0/WMS-1.0.war
channel stopped
Deploying /root/.jenkins/workspace/test/target/WMS.war to container Tomcat 8.x Remote with context /WMS
[/root/.jenkins/workspace/test/target/WMS.war] is not deployed. Doing a fresh deployment.
Deploying [/root/.jenkins/workspace/test/target/WMS.war]
Finished: SUCCESS
拓展:部署到docker容器篇
基本配置與上面一緻,唯一差別就是建構後的操作
SSH 建構配置:
SSH Server配置:
菜單:Manage Jenkins->Configure System
最終建構成功:
SSH: Connecting from host [vultr.guest]
SSH: Connecting with configuration [vultrVPS] ...
SSH: EXEC: STDOUT/STDERR from command [docker cp /usr/local/software/jenkin2docker/demo.war tomcat8:/usr/local/tomcat/webapps/
docker stop tomcat8
sleep 10
docker start tomcat8] ...
tomcat8
tomcat8
SSH: EXEC: completed after 17,605 ms
SSH: Disconnecting configuration [vultrVPS] ...
SSH: Transferred 1 file(s)
Finished: SUCCESS