Jenkins部署實戰篇
html
做者:尹正傑java
版權聲明:原創做品,謝絕轉載!不然將追究法律責任。python
一.持續集成理論linux
1>.開發寫代碼的演變過程web
1.1>.一個開發單打獨鬥,擼代碼,開發網站,自由自在;shell
1.2>.多個開發同時開發一個網站,同時改一份代碼。可是同時給一個文件會致使衝突;centos
1.3>.分支結構,天天上班第一件事克隆代碼,下班前最後一件事合併代碼;服務器
1.4>.好景不長,開發愈來愈多,代碼文件愈來愈多。天天下班前合併代碼時,發現不少合併失敗的文件。爲了解決這些合併失敗的文件,最後天天加班3小時人工合併代碼並且尚未工資;oracle
舒適提示:面臨以上的問題的解決方法:將合併代碼的週期縮短,之前一天,如今兩小時,一小時,半小時......,隨時隨地將代碼合併,這種方法專業術語叫作持續集成。app
2>.持續集成特色
持續集成的英文名稱爲:Continuous Integation,簡稱CI。持續集成指的是,頻繁地(一天屢次)將代碼集成到主幹。它的好處主要有如下兩個:
2.1>.快速發現錯誤。每完成一點更新,就集成到主幹,能夠快速發現錯誤,定位錯誤也比較容易;
2.2>.防止分支大幅度偏離主幹。若是不是常常集成,主幹又在不斷更新,會致使之後集成的難度變大,甚至難以集成;
3>.運維測試代碼的演變
3.1>.初級運維很苦逼,剛開始開發每次合併一次代碼,而後運維把代碼pull下來測試就能夠了;
3.2>.可是,後來開發引進了持續集成方法論,開發們都「彈冠相慶」;
3.3>.運維同窗感受好苦逼,一天到晚不聽的測試代碼;
3.4>.天天下班以後,運維拖着疲倦的身子回到宿舍,就想,有沒有辦法自動化?
3.5>.初級運維請教了傳說中的大神,知道了一些方法;
3.6>.藉助一個自動化的部署工具,叫作Jenkins;
3.7>.開發上傳本身的代碼到GitLab,GitLab發消息通知Jenkins,隨後Jenkins從倉庫拉取代碼,最後全自動部署到測試服務進行相關測試,並將測試結果通知運維和開發;
3.8>.還有偷懶的方法,直接把這個工具交給開發使用,今後就能夠高枕無憂了;
舒適提示:這種自動測試的方法叫作持續交付。
4>.持續交付特色
持續交付英文名稱爲:Continuous Delivery,簡稱CD。咱們能夠說持續交付是持續集成的下一步,它的好處主要有如下兩個:
4.1>.持續交付指的是頻繁的將軟件的新版本,交付給質量團隊或者用戶,以供審批。若是評審經過,代碼就進入生產階段;
4.2>.持續交付能夠看做持續集成的下一步。它強調的時,無論怎麼更新,軟件是隨時隨地能夠交付的;
5>.運維上線代碼的演變
5.1>.代碼測試經過了,該到生產環境部署了,運維又該幹活了;
5.2>.其實這是一錘子買賣了,要麼成功,要麼失敗回滾;
5.3>.可使用自動部署工具,可是不少公司仍是相信人工上線;
5.4>.可是咱們還有偷懶的方法,好比Ansible,Saltstack;
舒適提示:這裏也有個方法叫作持續部署。
6>.持續部署特色
持續部署,的英文名稱爲:Continuous Deployment,簡稱CD。把代碼部署到服務器就是持續部署,它有如下兩個特色:
6.1>.持續部署是持續交付的下一步,指的是代碼經過評審之後,自動部署到生產環境;
6.2>.持續部署的目標是,代碼在任什麼時候刻都是可部署的,能夠進入生產階段;
舒適提示:你是否常常聽一些作持續集成的公司喜歡說的行話:CI/CD ->持續集成/持續交付/持續部署。
二.Jenkins介紹
1>.Jenkins是一個用Java編寫的開源的持續集成工具。在Oracle發生爭執後,項目充Hudson項目獨立出來;
2>.Jenkins提供了軟件開發的持續集成服務;
3>.Jenkins運行在servlet容器中(例如Apache Tomcat);
4>.Jenkins支持軟件配置管理(SCM)工具(包括Accurev SCM,CVS,Subversion,Git,Perforce,Clearcase和RIC);
5>.Jenkins能夠執行基於Apache Ant和Apache Maven的項目,以及任意的shell腳本和Windows批處理命令;
6>.Jenkins的主要開發者是川口耕介;
7>.Jenkins是MIT許可證下發布的自由軟件;
三.安裝Jenkins環境準備
1>.Jenkins服務器配置
2>.GitLab服務器配置
3>.Jenkins的RPM安裝包下載地址
本篇博客採用常規安裝方法,即便用RPM包安裝
Jenkins官網的RPM安裝包:http://pkg.jenkins.io/redhat-stable/
清華源地址關於Jenkins的安裝包:https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat-stable/
4>.linux中能發郵件的帳號
四.安裝Jenkins
1>.安裝jdk
[root@jenkins yinzhengjie]# yum search jdk Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.shu.edu.cn * extras: mirrors.huaweicloud.com * updates: mirrors.shu.edu.cn ====================================================== N/S matched: jdk ======================================================= copy-jdk-configs.noarch : JDKs configuration files copier java-1.6.0-openjdk.x86_64 : OpenJDK Runtime Environment java-1.6.0-openjdk-demo.x86_64 : OpenJDK Demos java-1.6.0-openjdk-devel.x86_64 : OpenJDK Development Environment java-1.6.0-openjdk-javadoc.x86_64 : OpenJDK API Documentation java-1.6.0-openjdk-src.x86_64 : OpenJDK Source Bundle java-1.7.0-openjdk.x86_64 : OpenJDK Runtime Environment java-1.7.0-openjdk-accessibility.x86_64 : OpenJDK accessibility connector java-1.7.0-openjdk-demo.x86_64 : OpenJDK Demos java-1.7.0-openjdk-devel.x86_64 : OpenJDK Development Environment java-1.7.0-openjdk-headless.x86_64 : The OpenJDK runtime environment without audio and video support java-1.7.0-openjdk-javadoc.noarch : OpenJDK API Documentation java-1.7.0-openjdk-src.x86_64 : OpenJDK Source Bundle java-1.8.0-openjdk.i686 : OpenJDK Runtime Environment java-1.8.0-openjdk.x86_64 : OpenJDK Runtime Environment java-1.8.0-openjdk-accessibility.i686 : OpenJDK accessibility connector java-1.8.0-openjdk-accessibility.x86_64 : OpenJDK accessibility connector java-1.8.0-openjdk-accessibility-debug.i686 : OpenJDK accessibility connector for packages with debug on java-1.8.0-openjdk-accessibility-debug.x86_64 : OpenJDK accessibility connector for packages with debug on java-1.8.0-openjdk-debug.i686 : OpenJDK Runtime Environment with full debug on java-1.8.0-openjdk-debug.x86_64 : OpenJDK Runtime Environment with full debug on java-1.8.0-openjdk-demo.i686 : OpenJDK Demos java-1.8.0-openjdk-demo.x86_64 : OpenJDK Demos java-1.8.0-openjdk-demo-debug.i686 : OpenJDK Demos with full debug on java-1.8.0-openjdk-demo-debug.x86_64 : OpenJDK Demos with full debug on java-1.8.0-openjdk-devel.i686 : OpenJDK Development Environment java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment java-1.8.0-openjdk-devel-debug.i686 : OpenJDK Development Environment with full debug on java-1.8.0-openjdk-devel-debug.x86_64 : OpenJDK Development Environment with full debug on java-1.8.0-openjdk-headless.i686 : OpenJDK Runtime Environment java-1.8.0-openjdk-headless.x86_64 : OpenJDK Runtime Environment java-1.8.0-openjdk-headless-debug.i686 : OpenJDK Runtime Environment with full debug on java-1.8.0-openjdk-headless-debug.x86_64 : OpenJDK Runtime Environment with full debug on java-1.8.0-openjdk-javadoc.noarch : OpenJDK API Documentation java-1.8.0-openjdk-javadoc-debug.noarch : OpenJDK API Documentation for packages with debug on java-1.8.0-openjdk-javadoc-zip.noarch : OpenJDK API Documentation compressed in single archive java-1.8.0-openjdk-javadoc-zip-debug.noarch : OpenJDK API Documentation compressed in single archive for packages with debug on java-1.8.0-openjdk-src.i686 : OpenJDK Source Bundle java-1.8.0-openjdk-src.x86_64 : OpenJDK Source Bundle java-1.8.0-openjdk-src-debug.i686 : OpenJDK Source Bundle for packages with debug on java-1.8.0-openjdk-src-debug.x86_64 : OpenJDK Source Bundle for packages with debug on ldapjdk-javadoc.noarch : Javadoc for ldapjdk icedtea-web.x86_64 : Additional Java components for OpenJDK - Java browser plug-in and Web Start implementation ldapjdk.noarch : The Mozilla LDAP Java SDK Name and summary matches only, use "search all" for everything. [root@jenkins yinzhengjie]#
[root@jenkins yinzhengjie]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.shu.edu.cn * extras: mirrors.huaweicloud.com * updates: mirrors.shu.edu.cn Resolving Dependencies --> Running transaction check ---> Package java-1.8.0-openjdk.x86_64 1:1.8.0.181-3.b13.el7_5 will be installed --> Processing Dependency: java-1.8.0-openjdk-headless(x86-64) = 1:1.8.0.181-3.b13.el7_5 for package: 1:java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64 --> Processing Dependency: libjvm.so(SUNWprivate_1.1)(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64 --> Processing Dependency: libjava.so(SUNWprivate_1.1)(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64 --> Processing Dependency: libjvm.so()(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64 --> Processing Dependency: libjava.so()(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64 ---> Package java-1.8.0-openjdk-devel.x86_64 1:1.8.0.181-3.b13.el7_5 will be installed --> Running transaction check ---> Package java-1.8.0-openjdk-headless.x86_64 1:1.8.0.181-3.b13.el7_5 will be installed --> Processing Dependency: jpackage-utils for package: 1:java-1.8.0-openjdk-headless-1.8.0.181-3.b13.el7_5.x86_64 --> Running transaction check ---> Package javapackages-tools.noarch 0:3.4.1-11.el7 will be installed --> Processing Dependency: python-javapackages = 3.4.1-11.el7 for package: javapackages-tools-3.4.1-11.el7.noarch --> Running transaction check ---> Package python-javapackages.noarch 0:3.4.1-11.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved =============================================================================================================================== Package Arch Version Repository Size =============================================================================================================================== Installing: java-1.8.0-openjdk x86_64 1:1.8.0.181-3.b13.el7_5 updates 250 k java-1.8.0-openjdk-devel x86_64 1:1.8.0.181-3.b13.el7_5 updates 9.8 M Installing for dependencies: java-1.8.0-openjdk-headless x86_64 1:1.8.0.181-3.b13.el7_5 updates 32 M javapackages-tools noarch 3.4.1-11.el7 base 73 k python-javapackages noarch 3.4.1-11.el7 base 31 k Transaction Summary =============================================================================================================================== Install 2 Packages (+3 Dependent packages) Total download size: 42 M Installed size: 144 M Downloading packages: (1/5): python-javapackages-3.4.1-11.el7.noarch.rpm | 31 kB 00:00:00 (2/5): javapackages-tools-3.4.1-11.el7.noarch.rpm | 73 kB 00:00:00 java-1.8.0-openjdk-1.8.0.181-3 FAILED ] 579 kB/s | 9.8 MB 00:00:56 ETA http://centos.ustc.edu.cn/centos/7.5.1804/updates/x86_64/Packages/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64.rpm: [Errno 14] curl#7 - "Failed to connect to 2001:da8:d800:95::110: Network is unreachable" Trying other mirror. (3/5): java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64.rpm | 250 kB 00:00:00 (4/5): java-1.8.0-openjdk-devel-1.8.0.181-3.b13.el7_5.x86_64.rpm | 9.8 MB 00:00:47 (5/5): java-1.8.0-openjdk-headless-1.8.0.181-3.b13.el7_5.x86_64.rpm | 32 MB 00:01:02 ------------------------------------------------------------------------------------------------------------------------------- Total 687 kB/s | 42 MB 00:01:02 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : python-javapackages-3.4.1-11.el7.noarch 1/5 Installing : javapackages-tools-3.4.1-11.el7.noarch 2/5 Installing : 1:java-1.8.0-openjdk-headless-1.8.0.181-3.b13.el7_5.x86_64 3/5 Installing : 1:java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64 4/5 Installing : 1:java-1.8.0-openjdk-devel-1.8.0.181-3.b13.el7_5.x86_64 5/5 Verifying : 1:java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64 1/5 Verifying : 1:java-1.8.0-openjdk-headless-1.8.0.181-3.b13.el7_5.x86_64 2/5 Verifying : 1:java-1.8.0-openjdk-devel-1.8.0.181-3.b13.el7_5.x86_64 3/5 Verifying : python-javapackages-3.4.1-11.el7.noarch 4/5 Verifying : javapackages-tools-3.4.1-11.el7.noarch 5/5 Installed: java-1.8.0-openjdk.x86_64 1:1.8.0.181-3.b13.el7_5 java-1.8.0-openjdk-devel.x86_64 1:1.8.0.181-3.b13.el7_5 Dependency Installed: java-1.8.0-openjdk-headless.x86_64 1:1.8.0.181-3.b13.el7_5 javapackages-tools.noarch 0:3.4.1-11.el7 python-javapackages.noarch 0:3.4.1-11.el7 Complete! [root@jenkins yinzhengjie]#
[root@jenkins yinzhengjie]# java -version #查看java版本 openjdk version "1.8.0_181" OpenJDK Runtime Environment (build 1.8.0_181-b13) OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode) [root@jenkins yinzhengjie]# [root@jenkins ~]# mkdir /usr/java #切結,若是是源碼安裝的java的話,咱們須要手動建立這個目錄 [root@jenkins ~]# [root@jenkins ~]# cd /usr/java/ [root@jenkins java]# [root@jenkins java]# ln -s /soft/jdk1.8.0_181/ /usr/java/jdk1.8 #這兩個連接必須得作,不然若是你rpm方式安裝jdk的話,可能會出現Jenkins只啓動進程,可是端口始終沒法啓動的情況 [root@jenkins java]# [root@jenkins java]# ln -s /soft/jdk1.8.0_181/ /usr/java/defalut [root@jenkins java]#
2>.安裝Jenkins的rpm安裝包
[root@jenkins download]# wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat-stable/jenkins-2.73.1-1.1.noarch.rpm --2018-09-08 20:13:20-- https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat-stable/jenkins-2.73.1-1.1.noarch.rpm Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1 Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 72756863 (69M) [application/x-redhat-package-manager] Saving to: ‘jenkins-2.73.1-1.1.noarch.rpm’ 100%[=====================================================================================>] 72,756,863 496KB/s in 2m 0s 2018-09-08 20:15:20 (593 KB/s) - ‘jenkins-2.73.1-1.1.noarch.rpm’ saved [72756863/72756863] [root@jenkins download]# ll total 71052 drwxrwxr-x 3 yinzhengjie yinzhengjie 144 Sep 6 13:26 cdh -rw-r--r-- 1 root root 72756863 Sep 14 2017 jenkins-2.73.1-1.1.noarch.rpm [root@jenkins download]#
[root@jenkins download]# ll total 71052 drwxrwxr-x 3 yinzhengjie yinzhengjie 144 Sep 6 13:26 cdh -rw-r--r-- 1 root root 72756863 Sep 14 2017 jenkins-2.73.1-1.1.noarch.rpm [root@jenkins download]# [root@jenkins download]# rpm -ivh jenkins-2.73.1-1.1.noarch.rpm warning: jenkins-2.73.1-1.1.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID d50582e6: NOKEY Preparing... ################################# [100%] Updating / installing... 1:jenkins-2.73.1-1.1 ################################# [100%] [root@jenkins download]#
3>.啓動Jenkins服務
[root@jenkins download]# /etc/init.d/jenkins start Starting jenkins (via systemctl): [ OK ] [root@jenkins download]# [root@jenkins download]# [root@jenkins download]# [root@jenkins download]# [root@jenkins download]# ps -ef | grep jenkins jenkins 5627 1 0 20:18 ? 00:00:00 /etc/alternatives/java -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon --httpPort=8080 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20 root 5647 5501 0 20:20 pts/0 00:00:00 grep --color=auto jenkins [root@jenkins download]# [root@jenkins ~]# netstat -untalp | grep 8080 tcp6 0 0 :::8080 :::* LISTEN 6106/java [root@jenkins ~]#
4>.訪問Jenkins的webUI界面(根據界面的提示信息,去服務器端將密碼寫進去便可)
5>.上個步驟執行成功後,會彈出下面的對話框,咱們點擊關閉便可
6>.開始使用Jenkins
7>.若是出現下面的界面,那麼恭喜你,Jenkins部署成功啦!
8>.Jenkins的目錄介紹
[root@jenkins ~]# rpm -ql jenkins /etc/init.d/jenkins #啓動文件存放位置 /etc/logrotate.d/jenkins #日誌分割配置文件 /etc/sysconfig/jenkins #Jenkins主配置文件 /usr/lib/jenkins /usr/lib/jenkins/jenkins.war #war包存放目錄,war包就是把網站站點打個包 /usr/sbin/rcjenkins #Jenkins的命令 /var/cache/jenkins #war包解壓目錄,Jenkins網頁代碼目錄 /var/lib/jenkins #Jenkins的工做目錄,Jenkins的配置就在這個目錄 /var/log/jenkins #Jenkins的日誌存放目錄 [root@jenkins ~]#
五.修改Jenkins密碼(咱們在安裝Jenkins的時候就發現Jenkins存放初始密碼存放位置是:/var/lib/jenkins/secrets/initialAdminPassword)
1>.點擊當前用戶,進入用戶配置界面
2>.點擊設置
3>.修改密碼
4>.點擊註銷
5>.使用新密碼登陸Jenkins
6>.通過我反覆驗證,修改密碼後,原始存放Jenkins密碼文件並無發生變化
六.Jenkins的其它操做
1>.Jenkins的插件安裝
詳情請參考:https://www.cnblogs.com/yinzhengjie/p/9589319.html
2>.
3>.