持續集成①安裝部署jenkins從git獲取代碼

持續集成①安裝部署jenkins從git獲取代碼java

一:持續集成的概念:node

1.1:整體的歸納linux

持續集成Continuous Integration
持續交付Continuous Delivery
持續部署Continuous Deployment

 

1.2:什麼是持續集成:
持續集成是指開發者在代碼的開發過程當中,能夠頻繁的將代碼部署集成到主幹,並進程自動化測試git

1.3:什麼是持續交付:
持續交付指的是在持續集成的環境基礎之上,將代碼部署到預生產環境web

1.4:持續部署:
在持續交付的基礎上,把部署到生產環境的過程自動化,持續部署和持續交付的區別就是最終部署到生產環境是自動化的。apache

1.5:關於安全:OWASP:每一年發佈top 10web漏洞:
top1:注入攻擊漏洞,例如SQL,OS 以及 LDAP注入。這些攻擊發生在當不可信的數據做爲命令或者查詢語句的一部分,被髮送給解釋器的時候。攻擊者發送的惡意數據能夠欺騙解釋器,以執行計劃外的命令或者在未被恰當受權時訪問數據。vim

top2:失效的身份認證和會話管理:與身份認證和會話管理相關的應用程序功能每每得不到正確的實現,這就致使了攻擊者破壞密碼、密匙、會話令牌或攻擊其餘的漏洞去冒充其餘用戶的身份。瀏覽器

top3:跨站腳本(XSS):當應用程序收到含有不可信的數據,在沒有進行適當的驗證和轉義的狀況下,就將它發送給一個網頁瀏覽器,這就會產生跨站腳本攻擊(簡稱XSS)。XSS容許攻擊者在受害者的瀏覽器上執行腳本,從而劫持用戶會話、危害網站、或者將用戶轉向至惡意網站。
相關資料:http://www.owasp.org.cn/owasp-project/download/mobile-top-10-2013-2tomcat

二:安裝部署持續集成工具jenkins:
2.1:安裝部分:
2.1.1:執行安裝:
下載jdk-8u111-linux-x64.rpm安全

# rpm -ivh jdk-8u111-linux-x64.rpm

# vim /etc/profile

export JAVA_HOME=/usr/java/jdk1.8.0_111 
export PATH=$JAVA_HOME/bin:$PATH

安裝jenkins
方法①yum方式安裝

# cd /etc/yum.repos.d/
# wget http://pkg.jenkins.io/redhat/jenkins.repo
# rpm --import http://pkg.jenkins.io/redhat/jenkins.io.key
# yum install -y jenkins
# systemctl start jenkins

方法②經過war包的方式安裝下載地址:http://updates.jenkins-ci.org/download/war/2.60.1/jenkins.war

unzip apache-tomcat-8.0.37.zip
mv apache-tomcat-8.0.37 /usr/local/
cd /usr/local
ln -s /usr/local/apache-tomcat-8.0.37 /usr/local/tomcat
useradd jenkins
# 將jenkins.war放到/usr/local/tomcat/webapps目錄下,解壓
unzip -d jenkins jenkins.war
chown -R jenkins.jenkins /usr/local/apache-tomcat-8.0.37
bin/startup.sh

 

2.1.2:打開web界面:第一次打開要輸入隨機生成的密碼:
http://192.168.3.199:8080/jenkins

查看密碼

$ cat /home/jenkins/.jenkins/secrets/initialAdminPassword
8743f91ff1474a85a0abcd841fc74eb6

 

輸入密碼點下確認以後選擇插件,選擇默認安裝插件便可,此過程須要一段時間,並且根據網絡不一樣有些插件安裝不成功,可是以後能夠本身再安裝便可

報錯:
An error occurred
An error occurred during installation: No such plugin: cloudbees-folder

解決辦法:
下載cloudbees-folder.hpi放在目錄/usr/local/tomcat/webapps/jenkins/WEB-INF/detached-plugins/下,重啓tomcat便可

設置用戶名密碼

3.1配置郵件通知,系統管理–系統設置

添加管理員郵箱,添加郵件

點擊保存或測試郵件,系統會給目標郵箱發送一封測試郵件:

安裝gitlab插件,系統管理–管理插件–可選插件

插件安裝界面,會額外安裝一些依賴關係的插件,jenkins基於ruby開發,因此會有ruby環境:

在gitlab管理界面將用戶添加到一個項目,下一步要用此用戶拉取項目代碼

三:使用jenkins:
3.1:添加一個認證用戶,拉取git代碼的時候使用:

建立任務 --> 構建一個自由風格的軟件項目

項目描述信息:

配置git項目地址,咱們先進行其餘配置,等會再繼續配置git相關的選項

系統管理 --> Global Tool Configuration

 

配置deploy-key
以下配置,jenkins服務器上root用戶生成密鑰對

[root@node1 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
bf:1e:4d:b3:0f:fb:8b:71:cd:ef:d3:70:69:a4:46:ff root@node1.chinasoft.com
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
| . . |
| S + + .|
| . o =.*.|
| o * oo=|
| o B .E|
| .o o.+o+|
+-----------------+
[root@node1 ~]# cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC3vhfbA4NdkP8g1WVyYkw93f1PZ04nWoyFnoerC9b40jIY5px+tkugdy/RZ3/bp4hMC5yNgV1S25Tm8RpzBJMp7pbJz8dO+LCKqUnXq9Eh0QhsZE0xlQN+J4awy9YIGiD2nFg7k/ZzeAtQRzNryrrPnKaWpXtg7tMGGTWBjeyLAVuqCOMU7euY94G26UmVfjDLIJkcJqB+8dwvodW3wpmUER32qVPKZLnSYSOCfoNed+P4Eujs5PBCLuzFvGyiDhY2Pwrk/4S11jWUa7TJItfoPXzeGc/ujaJi/o24dt8VXeFa/Rm4wywYLNW3TRjXy1mPpdVlob3701MMQ0bf3qPv root@node1.chinasoft.com

 

gitlab上配置web-demo項目設置private deploy key

把key複製進去,同時title起一個簡單明瞭的名字

在jenkins服務器192.168.3.199上測試下是否可以正常獲取代碼

[root@node1 tmp]# git clone git@192.168.3.198:web/web-demo.git
Cloning into 'web-demo'...
The authenticity of host '192.168.3.198 (192.168.3.198)' can't be established.
ECDSA key fingerprint is 9d:60:c3:8e:49:72:e8:9c:c0:d6:c8:d2:f1:b6:3a:74.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.3.198' (ECDSA) to the list of known hosts.
remote: Counting objects: 79, done.
remote: Compressing objects: 100% (79/79), done.
remote: Total 79 (delta 17), reused 0 (delta 0)
Receiving objects: 100% (79/79), 1.18 MiB | 0 bytes/s, done.
Resolving deltas: 100% (17/17), done.

 

jenkins拉取代碼沒問題,繼續配置jenkins的認證,這裏應該輸入私鑰

複製jenkins拉取git代碼服務器root的私鑰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r
6bHZzCnp9cYe56z9qsGk064LR2N9TR16xAzeud0CgYEArx0B2NZKeaNDBhHPxI8Q
/IwOJSs0O0Gv6a4iu/F3gviffUFTOIgkTjbFwCqMrnuHYfOSccDeb5vIZlcvuyj2
D/DP8gS2UknnQDGzRhQxAuOGJg8CQrcOtka3brWfZVTVL3Q2OYMg4Iej2HXDoP8h
42zRR4u/THmhLL1O6NxVjUMCgYBslKyVbuja9T61HjXPVmDbqIIyEZ6a4NnlA6jy
7Mnq85LofCosQB00vDDH7SCyF0B75P/KaSNjH8JLtSoKfpoXVn2nkz/gxcZ9WWhP
Ve+CCuoNjDJpQriVDT74m42Jt14uJ9eaiPy+JUR4w5YOhdmZhg5NkKs75XJUkUQk
FR8FVQKBgQDOWXJYF3VEYdCD1qIy/CsY3s4ARD9kery+5aOCu6TNd6auYEfqxsev
ie1jD+cvAgVR1aZ5aP3C8jEYhZa4xgn1+hxfRLWs79uMYnXXNGJ+FxKftDyhuV+m
efEQrUf8jh4MofW9Ee6Z7YjitIRY7SQ+kZl2Xms+QZlJotBzTG2DSA==
-----END RSA PRIVATE KEY-----

 

添加描述,點擊OK

繼續編輯配置

這樣就不報錯了,下面還能夠指定從哪一個分支拉取,默認從master拉取,有些公司使用release分支

源碼瀏覽器,去gitlab上找到http的url:http://192.168.3.198/web/web-demo.git

找出gitlab的版本,輸入url和版本,版本只支持2位,拉到最下面保存

[root@web01 ~]# rpm -qa|grep gitlab
gitlab-ce-8.10.5-ce.0.el7.x86_64

 

點擊當即構建,查看控制檯,能夠看到構建的詳細輸出

相關文章
相關標籤/搜索