本章節(jié)主要介紹jenkins的使用
在前一章節(jié)中已經(jīng)介紹可jenkins安裝步驟茧泪,不了解的可以參考前文:[jenkins環(huán)境搭建](http://www.reibang.com/p/1a0971bd8139)
* * *
# jenkins如何和github對(duì)接
jenkins是插件式的微峰,所以需要對(duì)接github或svn時(shí)酱固,需要下載相應(yīng)的插件毯盈。同樣,當(dāng)需要對(duì)接github代碼庫(kù)時(shí)搜骡,就需要下載官方提供的git plugin和github plugin插件了蚌卤,具體安裝步驟如下:
#### **(1)在jenkins首頁(yè)中,點(diǎn)擊系統(tǒng)管理->管理插件**

然后幽钢,在可選插件中選擇需要下載的插件點(diǎn)擊下載即可歉备,如果安裝失敗,則需要在高級(jí)中手動(dòng)上傳phi文件下載匪燕。大部分下載失敗的原因多由于網(wǎng)絡(luò)或是依賴導(dǎo)致蕾羊。
如果是下載成功后喧笔,發(fā)現(xiàn)還會(huì)出現(xiàn)錯(cuò)誤,建議是重啟jenkins服務(wù)龟再。
#### **(2)保證從github下載代碼书闸,免密碼輸入(可選)**
從github代碼庫(kù)下載工程代碼,要達(dá)到免密碼登錄利凑,就需要設(shè)置SSH key給github浆劲,使用公鑰私鑰認(rèn)證
我們jenkins是安裝在unbuntu機(jī)器,相應(yīng)的git代碼也是要下載到這臺(tái)機(jī)器上哀澈,在這臺(tái)機(jī)器中設(shè)置證書
參考:[如何設(shè)置證書](http://jingyan.baidu.com/article/a65957f4f0acc624e67f9bc1.html?spm=5176.100239.blogcont33036.4.Os9o7b)
**第一步**
ssh-keygen -t rsa -C "your_email@example.com"
Enter passphrase (empty for no passphrase):
當(dāng)你看到這個(gè)的時(shí)候梳侨, 輸入你的密碼,重復(fù)輸入密碼就可以了日丹,這時(shí)會(huì)在/root/.ssh目錄下包含需要的證書
**第二步**
執(zhí)行以下命令將KEY加入到ssh-agent:
$ eval "$(ssh-agent -s)"
# Agent pid 59566
$ ssh-add ~/.ssh/id_rsa
**第三步**
打開 ~/.ssh/id_rsa.pub文件走哺, 復(fù)制其中所有的內(nèi)容。然后把它粘貼到github個(gè)人賬號(hào)settings配置項(xiàng)ssh key添加的表單中哲虾。

添加成功后丙躏,會(huì)顯示如下圖

**最后一步**
執(zhí)行ssh -T git@github.com
如果不提示輸入密碼,表示成功
### (3)jenkins對(duì)接github
新建jenkins jobs

輸入job名稱束凑,新建一個(gè)自由風(fēng)格的軟件項(xiàng)目晒旅,點(diǎn)擊ok

最主要的是配置選擇Github project,如果是沒有ssh認(rèn)證汪诉,就需要配置為https://github.com/geneseeq/auototest.git

這里需要配置git源碼庫(kù)地址和登錄賬戶废恋,已經(jīng)構(gòu)建的是master庫(kù)代碼

*注意:Credentials是需要新添加,添加步驟如下*
*a.首頁(yè)中點(diǎn)擊Credentials*

*添加頁(yè)面中注意選擇下圖選中的就可以了*

然后在構(gòu)建觸發(fā)器配置中需要配置觸發(fā)構(gòu)建的條件扒寄,和檢查間隔時(shí)間鱼鼓,以及每日構(gòu)建定時(shí)時(shí)間

最后一步是設(shè)置了執(zhí)行腳本,順便寫了一個(gè)測(cè)試執(zhí)行的動(dòng)作

最后就可以點(diǎn)擊構(gòu)建了该编,可以構(gòu)建成功

* * *
# 郵件功能
當(dāng)然僅有上邊的功能還是不夠便捷迄本,我想設(shè)置一個(gè)構(gòu)建結(jié)果的通知,可以使用郵件通知的插件
* 首先需要再系統(tǒng)管理中配置郵件服務(wù)器信息

點(diǎn)擊左邊的“系統(tǒng)管理”菜單课竣,選擇右邊的“系統(tǒng)設(shè)置”

在系統(tǒng)設(shè)置中找到Jenkins Location項(xiàng)填入Jenkins URL和系統(tǒng)管理員郵件地址嘉赎,系統(tǒng)管理員一定要配置,是用來轉(zhuǎn)發(fā)郵件的于樟。

找到Extended E-mail Notification項(xiàng)公条,填入smtp服務(wù)器信息及用戶名、密碼等認(rèn)證信息

其郵件的顯示信息代碼為
然后郵件配置測(cè)試成功后迂曲,設(shè)置工程中要發(fā)送的成員郵件信息靶橱,需要在構(gòu)建后中設(shè)置

在Advanced Settings中設(shè)置項(xiàng)目通知收件人的列表,主題,內(nèi)容和附件
另外還有一個(gè)要注意的是要開啟郵箱的smtp功能

# github merge代碼的check功能
首先工程配置中”源碼管理“配置需要修改

然后配置”構(gòu)建觸發(fā)器“中Github Pull Request Builder項(xiàng)

構(gòu)建環(huán)境配置如下

注意抓韩,要pull request需要再全局變量中設(shè)置web hook纠永,對(duì)接github

以及下邊的配置是連接github的,可以測(cè)試是否可以連接

當(dāng)然上邊的證書配置內(nèi)容谒拴,是需要github提供Org Webhooks Secret的

github webhooks配置

似乎一切都結(jié)束了尝江,讓我們實(shí)驗(yàn)一下吧。
如果配置成功的話英上,其結(jié)果會(huì)是下圖那樣
