持續集成工具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如下:

Started by user anonymous
Building in workspace D:\Program Files\Jenkins\jobs\JenkinsTest\workspace
 > D:\Program Files\Git\bin\git.exe rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > D:\Program Files\Git\bin\git.exe config remote.origin.url https://github.com/GaoHongchen/WinFormGit.git # timeout=10
Fetching upstream changes from https://github.com/GaoHongchen/WinFormGit.git
 > D:\Program Files\Git\bin\git.exe --version # timeout=10
 > D:\Program Files\Git\bin\git.exe -c core.askpass=true fetch --tags --progress https://github.com/GaoHongchen/WinFormGit.git +refs/heads/*:refs/remotes/origin/*
 > D:\Program Files\Git\bin\git.exe rev-parse "refs/remotes/origin/master^{commit}" # timeout=10
 > D:\Program Files\Git\bin\git.exe rev-parse "refs/remotes/origin/origin/master^{commit}" # timeout=10
Checking out Revision 2562db18b228c399d3d0e14f83ce305f09f64be1 (refs/remotes/origin/master)
 > D:\Program Files\Git\bin\git.exe config core.sparsecheckout # timeout=10
 > D:\Program Files\Git\bin\git.exe checkout -f 2562db18b228c399d3d0e14f83ce305f09f64be1
 > D:\Program Files\Git\bin\git.exe rev-list 2562db18b228c399d3d0e14f83ce305f09f64be1 # timeout=10
Path To MSBuild.exe: C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe
Executing the command cmd.exe /C " C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe /t:Rebuild /property:Configuration=Release;TargetFrameworkVersion=v4.0 "%WORKSPACE%\WinFormGit\WinFormGit.sln" " && exit %%ERRORLEVEL%% from D:\Program Files\Jenkins\jobs\JenkinsTest\workspace
[workspace] $ cmd.exe /C " C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe /t:Rebuild /property:Configuration=Release;TargetFrameworkVersion=v4.0 "%WORKSPACE%\WinFormGit\WinFormGit.sln" " && exit %%ERRORLEVEL%%
Microsoft(R) 生成引擎版本 4.0.30319.17929
[Microsoft .NET Framework 版本 4.0.30319.18052]
版權所有(C) Microsoft Corporation 2007。保留所有權利。

Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
生成啓動時間爲 2015/12/19 15:23:19。
項目“D:\Program Files\Jenkins\jobs\JenkinsTest\workspace\WinFormGit\WinFormGit.sln”在節點 1 上(Rebuild 個目標)。
ValidateSolutionConfiguration:
  正在生成解決方案配置“Release|x86”。
項目“D:\Program Files\Jenkins\jobs\JenkinsTest\workspace\WinFormGit\WinFormGit.sln”(1)正在節點 1 上生成“D:\Program Files\Jenkins\jobs\JenkinsTest\workspace\WinFormGit\WinFormGit\WinFormGit.csproj”(2) (Rebuild 個目標)。
CoreClean:
  正在刪除文件“D:\Program Files\Jenkins\jobs\JenkinsTest\workspace\WinFormGit\WinFormGit\bin\Release\WinFormGit.exe”。
  正在刪除文件“D:\Program Files\Jenkins\jobs\JenkinsTest\workspace\WinFormGit\WinFormGit\bin\Release\WinFormGit.pdb”。
  正在刪除文件“D:\Program Files\Jenkins\jobs\JenkinsTest\workspace\WinFormGit\WinFormGit\obj\x86\Release\WinFormGit.csprojResolveAssemblyReference.cache”。
  正在刪除文件“D:\Program Files\Jenkins\jobs\JenkinsTest\workspace\WinFormGit\WinFormGit\obj\x86\Release\WinFormGit.Form1.resources”。
  正在刪除文件“D:\Program Files\Jenkins\jobs\JenkinsTest\workspace\WinFormGit\WinFormGit\obj\x86\Release\WinFormGit.Properties.Resources.resources”。
  正在刪除文件“D:\Program Files\Jenkins\jobs\JenkinsTest\workspace\WinFormGit\WinFormGit\obj\x86\Release\WinFormGit.csproj.GenerateResource.Cache”。
  正在刪除文件“D:\Program Files\Jenkins\jobs\JenkinsTest\workspace\WinFormGit\WinFormGit\obj\x86\Release\WinFormGit.exe”。
  正在刪除文件“D:\Program Files\Jenkins\jobs\JenkinsTest\workspace\WinFormGit\WinFormGit\obj\x86\Release\WinFormGit.pdb”。
ResolveAssemblyReferences:
  將生成 TargetFramework 配置文件排除列表。
CoreResGen:
  "c:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools\resgen.exe" /useSourcePath /r:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\Microsoft.CSharp.dll" /r:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\mscorlib.dll" /r:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Core.dll" /r:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Data.DataSetExtensions.dll" /r:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Data.dll" /r:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Deployment.dll" /r:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.dll" /r:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Drawing.dll" /r:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Windows.Forms.dll" /r:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Xml.dll" /r:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Xml.Linq.dll" /compile Form1.resx,obj\x86\Release\WinFormGit.Form1.resources Properties\Resources.resx,obj\x86\Release\WinFormGit.Properties.Resources.resources
  正在將資源文件“Form1.resx”處理到“obj\x86\Release\WinFormGit.Form1.resources”中。
  正在將資源文件“Properties\Resources.resx”處理到“obj\x86\Release\WinFormGit.Properties.Resources.resources”中。
GenerateTargetFrameworkMonikerAttribute:
正在跳過目標“GenerateTargetFrameworkMonikerAttribute”,因爲所有輸出文件相對於輸入文件而言都是最新的。
CoreCompile:
  C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /platform:x86 /errorreport:prompt /warn:4 /define:TRACE /highentropyva- /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\Microsoft.CSharp.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\mscorlib.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Core.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Data.DataSetExtensions.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Data.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Deployment.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Drawing.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Windows.Forms.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Xml.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Xml.Linq.dll" /debug:pdbonly /filealign:512 /optimize+ /out:obj\x86\Release\WinFormGit.exe /resource:obj\x86\Release\WinFormGit.Form1.resources /resource:obj\x86\Release\WinFormGit.Properties.Resources.resources /target:winexe /utf8output Form1.cs Form1.Designer.cs Program.cs Properties\AssemblyInfo.cs Properties\Resources.Designer.cs Properties\Settings.Designer.cs "C:\Windows\TEMP\.NETFramework,Version=v4.0,Profile=Client.AssemblyAttributes.cs"
CopyFilesToOutputDirectory:
  正在將文件從“obj\x86\Release\WinFormGit.exe”複製到“bin\Release\WinFormGit.exe”。
  WinFormGit -> D:\Program Files\Jenkins\jobs\JenkinsTest\workspace\WinFormGit\WinFormGit\bin\Release\WinFormGit.exe
  正在將文件從“obj\x86\Release\WinFormGit.pdb”複製到“bin\Release\WinFormGit.pdb”。
已完成生成項目“D:\Program Files\Jenkins\jobs\JenkinsTest\workspace\WinFormGit\WinFormGit\WinFormGit.csproj”(Rebuild 個目標)的操作。
已完成生成項目“D:\Program Files\Jenkins\jobs\JenkinsTest\workspace\WinFormGit\WinFormGit.sln”(Rebuild 個目標)的操作。

已成功生成。
    0 個警告
    0 個錯誤

已用時間 00:00:00.47
Finished: SUCCESS
Console Output

  最終,項目構建狀態如下:

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章