Jenkins詳細安裝與構建部署使用教程
Ubuntu安裝配置JDK、Tomcat、SVN服務器
鏈接: http://blog.51cto.com/13505030/2054688
========================================================
========================================================
一、安裝Jenkins
Jenkins在自動化方面使用很廣泛,那麽怎麽部署呢?
工具/原料
Jenkins war包
Tomcat
方法/步驟
從官網下載Jenkins的war包
鏈接: https://jenkins.io/download/
將下載好的War放到Tomcat的網站根目錄webapps下,然後啟動Tomcat。
root@iZuf6ea6asxmy2epuhqiodZ:/usr/java/tomcat/apache-tomcat-7.0.82/webapps# ll
total 72588
drwxr-xr-x 8 root root 4096 Dec 26 13:22 ./
drwxr-xr-x 9 root root 4096 Dec 26 12:47 ../
drwxr-xr-x 14 root root 4096 Dec 26 12:47 docs/
drwxr-xr-x 7 root root 4096 Dec 26 12:47 examples/
drwxr-xr-x 5 root root 4096 Dec 26 12:47 host-manager/
drwxr-xr-x 10 root root 4096 Dec 26 13:22 jenkins/
-rw-r--r-- 1 root root 74294776 Dec 26 13:18 jenkins.war
drwxr-xr-x 5 root root 4096 Dec 26 12:47 manager/
drwxr-xr-x 3 root root 4096 Dec 26 12:47 ROOT/
解壓jenkins.war包
root@iZuf6ea6asxmy2epuhqiodZ:/usr/java/tomcat/apache-tomcat-7.0.82/webapps# java -jar jenkins.war
Running from: /usr/java/tomcat/apache-tomcat-7.0.82/webapps/jenkins.war
webroot: $user.home/.jenkins
Dec 26, 2017 1:27:41 PM Main deleteWinstoneTempContents
WARNING: Failed to delete the temporary Winstone file /tmp/winstone/jenkins.war
Dec 26, 2017 1:27:41 PM org.eclipse.jetty.util.log.Log initialized
INFO: Logging initialized @260ms to org.eclipse.jetty.util.log.JavaUtilLog
Dec 26, 2017 1:27:41 PM winstone.Logger logInternal
INFO: Beginning extraction from war file
Dec 26, 2017 1:27:42 PM org.eclipse.jetty.server.handler.ContextHandler setContextPath
WARNING: Empty contextPath
Dec 26, 2017 1:27:42 PM org.eclipse.jetty.server.Server doStart
INFO: jetty-9.4.z-SNAPSHOT
Dec 26, 2017 1:27:42 PM org.eclipse.jetty.webapp.StandardDescriptorProcessor visitServlet
INFO: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet
Dec 26, 2017 1:27:42 PM org.eclipse.jetty.server.session.DefaultSessionIdManager doStart
INFO: DefaultSessionIdManager workerName=node0
Dec 26, 2017 1:27:42 PM org.eclipse.jetty.server.session.DefaultSessionIdManager doStart
INFO: No SessionScavenger set, using defaults
Dec 26, 2017 1:27:42 PM org.eclipse.jetty.server.session.HouseKeeper startScavenging
INFO: Scavenging every 660000ms
Jenkins home directory: /root/.jenkins found at: $user.home/.jenkins
Dec 26, 2017 1:27:43 PM org.eclipse.jetty.server.handler.ContextHandler doStart
INFO: Started w.@65e61854{/,file:///root/.jenkins/war/,AVAILABLE}{/root/.jenkins/war}
Dec 26, 2017 1:27:43 PM org.eclipse.jetty.server.AbstractConnector doStop
INFO: Stopped ServerConnector@40dd3977{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
Dec 26, 2017 1:27:43 PM org.eclipse.jetty.server.session.HouseKeeper stopScavenging
INFO: Stopped scavenging
Dec 26, 2017 1:27:43 PM hudson.WebAppMain contextDestroyed
INFO: Shutting down a Jenkins instance that was still starting up
java.lang.Throwable: reason
at hudson.WebAppMain.contextDestroyed(WebAppMain.java:388)
at org.eclipse.jetty.server.handler.ContextHandler.callContextDestroyed(ContextHandler.java:898)
at org.eclipse.jetty.servlet.ServletContextHandler.callContextDestroyed(ServletContextHandler.java:545)
at org.eclipse.jetty.server.handler.ContextHandler.stopContext(ContextHandler.java:873)
at org.eclipse.jetty.servlet.ServletContextHandler.stopContext(ServletContextHandler.java:355)
at org.eclipse.jetty.webapp.WebAppContext.stopWebapp(WebAppContext.java:1507)
at org.eclipse.jetty.webapp.WebAppContext.stopContext(WebAppContext.java:1471)
at org.eclipse.jetty.server.handler.ContextHandler.doStop(ContextHandler.java:927)
at org.eclipse.jetty.servlet.ServletContextHandler.doStop(ServletContextHandler.java:271)
at org.eclipse.jetty.webapp.WebAppContext.doStop(WebAppContext.java:569)
at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89)
at org.eclipse.jetty.util.component.ContainerLifeCycle.stop(ContainerLifeCycle.java:142)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStop(ContainerLifeCycle.java:160)
at org.eclipse.jetty.server.handler.AbstractHandler.doStop(AbstractHandler.java:124)
at org.eclipse.jetty.server.Server.doStop(Server.java:523)
at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89)
at winstone.Launcher.shutdown(Launcher.java:307)
at winstone.Launcher.<init>(Launcher.java:167)
at winstone.Launcher.main(Launcher.java:354)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at Main._main(Main.java:294)
at Main.main(Main.java:132)
Exception in thread "Jenkins initialization thread" java.lang.NoClassDefFoundError: hudson/util/HudsonFailedToLoad
at hudson.WebAppMain$3.run(WebAppMain.java:247)
Caused by: java.lang.ClassNotFoundException: hudson.util.HudsonFailedToLoad
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:560)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
Dec 26, 2017 1:27:43 PM org.eclipse.jetty.server.handler.ContextHandler doStop
INFO: Stopped w.@65e61854{/,null,UNAVAILABLE}{/root/.jenkins/war}
Dec 26, 2017 1:27:43 PM winstone.Logger logInternal
INFO: Winstone shutdown successfully
java.io.IOException: Failed to start Jetty
at winstone.Launcher.<init>(Launcher.java:156)
at winstone.Launcher.main(Launcher.java:354)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at Main._main(Main.java:294)
at Main.main(Main.java:132)
Caused by: java.net.BindException: Address already in use
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:298)
at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.server.Server.doStart(Server.java:431)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at winstone.Launcher.<init>(Launcher.java:154)
... 7 more
Dec 26, 2017 1:27:43 PM winstone.Logger logInternal
SEVERE: Container startup failed
java.io.IOException: Failed to start Jetty
at winstone.Launcher.<init>(Launcher.java:156)
at winstone.Launcher.main(Launcher.java:354)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at Main._main(Main.java:294)
at Main.main(Main.java:132)
Caused by: java.net.BindException: Address already in use
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:298)
at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.server.Server.doStart(Server.java:431)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at winstone.Launcher.<init>(Launcher.java:154)
... 7 more
root@iZuf6ea6asxmy2epuhqiodZ:/usr/java/tomcat/apache-tomcat-7.0.82/webapps# ll
total 72588
drwxr-xr-x 8 root root 4096 Dec 26 13:22 ./
drwxr-xr-x 9 root root 4096 Dec 26 12:47 ../
drwxr-xr-x 14 root root 4096 Dec 26 12:47 docs/
drwxr-xr-x 7 root root 4096 Dec 26 12:47 examples/
drwxr-xr-x 5 root root 4096 Dec 26 12:47 host-manager/
drwxr-xr-x 10 root root 4096 Dec 26 13:22 jenkins/
-rw-r--r-- 1 root root 74294776 Dec 26 13:18 jenkins.war
drwxr-xr-x 5 root root 4096 Dec 26 12:47 manager/
drwxr-xr-x 3 root root 4096 Dec 26 12:47 ROOT/
打開瀏覽器,輸入http://IP:8080/jenkins/ 進行訪問,按提示找到Jenkins生成的密碼並粘貼到框中,開始部署Jenkins
宣言Jenkins插件,選擇建議安裝的插件,開始安裝
創建第一個用戶及密碼
部署完成,開始使用吧
二、構建一個Maven項目
1、新建一個job
2、輸入名稱和項目類型
3、設置編譯的版本號等信息
4、設置svn庫地址:
輸入項目托管的svn的地址,如果有出現要輸入賬號和密碼的地方,輸入即可。
如果有出現如下說明用戶或密碼不對:
點擊進去重新設置用戶和密碼
5、配置jdk和maven
第一次打開出現如下,點擊進去。要求提示設置JDK和Maven
點擊系統管理-》系統設置,找到JDK和Maven的設置位置
選擇本電腦的:
9、到這裏已經可以構造這個項目了。
點擊構建
構建輸出的信息:
構建成功輸出如下 :
10、驗證
這時它已經自動把這個項目打包了一個war包,默認打包到了C:\Users\linbingwen\.jenkins\workspace\JavaWeb\JavaWeb\target
或者點擊如下:
三、自動遠程部署到tomcat
接下來要完成自動構建成war包後,將些war包上傳到遠程linux的tomcat的webapps目錄,更新項目的war包,並重啟tomcat.
1、安裝插件
系統管理-》管理插件,在可選插件裏找到下面這個,然後點擊直接安裝命令,安裝成功後要重啟jenkins
t筆者在安裝插件時報錯如下:
這應該是天朝的墻所導致的,所以筆者就使用了手動安裝的方式。
解決方法:
手動安裝
到https://wiki.jenkins-ci.org/display/JENKINS/Publish+Over+SSH+Plugin,下載hpi到本地電腦到
在系統管理--管理插件--高級--瀏覽-上傳插件即可
然後它自動上傳並安裝:
紅色的是筆者在線安裝不成功的,藍色的是筆者安裝成功的了,之後重啟jenkis即可。
2、配置ssh內容:
在系統管理-》系統設置裏
找到Publish over SSH
然後輸入:
3、配置Post Steps
這裏還是接著上面的JavaWeb項目,這個配置得安裝了上面的插件後才會顯示!
其中,
Transfer SetSource files:表示要上傳的本地的war包及路徑,可到工作空間去看
Remove prefix:表示要上傳時要去除的文件夾,即只上傳war包
remote driectory:即表示執行時的路徑,相當於把war包上傳到這裏了
exec commad:要執行的命令
要執行的腳本的內容:
步驟:
先停掉tomcat
刪除webapp下對應的war包
復制war到webapps
重啟tomcat
4、構建部署
點擊項目的構建按鈕,最終出現如下:
在linux上打開瀏覽器,輸入http://localhost:8080/JavaWeb-0.0.1-SNAPSHOT/
註意:這裏配置的ssh用戶:lin要有root的權限,要不可以會報錯沒有權限執行kill 或rm 命令
本文使用的Linux:Ubuntu14.04
其中JDK、Tomcat、SVN服務器請看這裏Ubuntu安裝配置JDK、Tomcat、SVN服務器
轉自:http://m.blog.csdn.net/article/details?id=50518959
Jenkins詳細安裝與構建部署使用教程