1. 程式人生 > >持續整合工具Jenkins學習總結

持續整合工具Jenkins學習總結

概述  

  持續整合(Continuous Integration,簡稱CI)是一種軟體開發實踐,團隊開發人員每次都通過自動化的構建(編譯、釋出、自動化測試)來驗證,從而儘早的發現整合錯誤。持續整合最大的優點是避免了傳統模式在整合階段的除蟲會議(bug meeting),其要素包括統一的程式碼庫、自動構建、自動測試、自動部署、頻繁提交修改過的程式碼等。

  Jenkins的前身是Hudson,是基於Java開發的一種持續整合工具,是一個開源軟體專案,主要用於:

  • 持續、自動地構建/測試軟體專案,如CruiseControl與DamageControl;
  • 監控一些定時執行的任務;

  Jenkins的官網:http://jenkins-ci.org/,下面我總結一下Windows下Jenkins的安裝和使用。

Jenkins安裝

(1)安裝素材

  • JDK(Jenkins執行需要Java 1.6以上的版本)
  • Jenkins安裝包(官網下載)

(2)安裝JDK

(3)安裝Jenkins

  執行Jenkins安裝包,按提示安裝即可,安裝成功之後會自動開啟網頁http://localhost:8080/,Jenkins主介面如下所示:

Jenkins配置

(1)配置http埠號

  Jenkins預設埠號是8080

,如果該埠號與其他任務的埠號衝突,就需要更改。開啟Jenkins安裝目錄下的Jenkins.xml檔案,如下所示:

複製程式碼
<!--
  Windows service definition for Jenkins

  To uninstall, run "jenkins.exe stop" to stop the service, then "jenkins.exe uninstall" to uninstall the service.
  Both commands don't produce any output if the execution is successful. 
--> <service> <id>jenkins</id> <name>Jenkins</name> <description>This service runs Jenkins continuous integration system.</description> <env name="JENKINS_HOME" value="%BASE%"/> <!-- if you'd like to run Jenkins with a specific version of Java, specify a full path to java.exe. The following value assumes that you have java in your PATH. --> <executable>%BASE%\jre\bin\java</executable> <arguments>-Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -jar "%BASE%\jenkins.war" --httpPort=8080</arguments> <!-- interactive flag causes the empty black Java window to be displayed. I'm still debugging this. <interactive /> --> <logmode>rotate</logmode> <onfailure action="restart" /> </service>
複製程式碼

  將<arguments>元素中的httpPort的值8080改為其他值即可。

(2)管理外掛

  Jenkins預設安裝了一些外掛,可根據自己的需要再安裝其他外掛,我額外安裝的外掛有:MSBuild plugin、MSTest plugin、Git plugin、GitHub plugin、Email Extension plugin等。

(3)配置JDK

  

(4)配置Git和GitHub

  

(5)配置MSBuild

  

  這個MSBuild是我編譯DotNet程式用的,如果編譯其他的(如C++程式),可點選Add MSBuild增加相應的MSBuild。

  注意:如果電腦上未安裝,可從其他電腦上拷貝一個。

(6)配置Extended E-mail Notification

  問題:這樣設定後還是出現“郵件傳送失敗”,如果哪位解決了,還請告知,謝謝

Jenkins構建專案

(1)新建專案

  在Jenkins首頁點選New Item,Item Name:JenkinsTest,選擇freestyle Project,OK

(2)常規設定

  勾選GitHub Project,輸入Project url,如我的是“https://github.com/GaoHongchen/WinFormGit.git/”

(3)設定Source Code Management

  在Source Code Management項中,選擇Git,填寫Repository URL:https://github.com/GaoHongchen/WinFormGit.git,其他預設

(4)設定Build Triggers

  根據自己的情況進行勾選,我選的是Build when a change is pushed to GitHub

(5)設定Build

  點選Add Build Step,進行選擇:

  1)Build a Visual Studio Project or solution using Build

  

  2)Execute Windows batch command

  

  Windows Batch Command如下:

"D:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe" /rebuild "Debug" "%WORKSPACE%\WinFormGit\WinFormGit.sln" /out C:\WinFormGit.log

  以上兩種Build方式殊途同歸,本專案中選擇一種即可。

(6)設定Post-build Actions

  點選Add post-build Action,我選擇EMail notification,如下所示:

  

(7)開始構建

  設定完成後,點選左側的Build Now,開始進行構建

(8)構建結果

  點選Console Output,檢視構建過程及結果。若構建失敗,在此可檢視失敗原因。

  Console Output如下:

 Console Output

  最終,專案構建狀態如下:

  為了配置好Jenkins,我可是構建了12次啊。雖然花費了不少時間,但學會了用Jenkins持續整合,這還是令人高興的,哈哈。。。

=======================================================================
中文名:高洪臣
英文名:Gordon Scott
E-mail:[email protected]
======================================================================