這篇來看看如何讓Jenkins幫我們pull code、build和run Unit Test
我希望只要有push code後,Jenkins就要自動從github拉最新code,
並自動build和跑相關Test case,這裡我使用NetCore 進行測試。
Jenkins version:2.89.4
NetCore version:2.1.4
Source Repository: https://github.com/ricoisme/unit-testing-using-nunit (fork Microsoft)
@install .NET core
Sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
Sudo sh -c 'echo -e "[packages-microsoft-com-prod]\nname=packages-microsoft-com-prod \nbaseurl= https://packages.microsoft.com/yumrepos/microsoft-rhel7.3-prod\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/dotnetdev.repo'
Sudo yum -y update
Sudo yum -y install scl-utils
sudo yum -y install libunwind libicu
sudo yum -y install dotnet-sdk-2.1.4
Sudo dotnet --version
@install git,github,Nunit plugin on jenkins
@setting global property
我透過ngrok進行反向代理,讓webhook可以正常工作
@create a new job
設定Github project
設定原始碼管理
設定觸發
Push code就會觸發。
設定建置環境
會先刪除相對應workspace。
我的build scripts很簡單(執行shell)
#!/bin/sh
dotnet --info
dotnet --version
rm -rf /home/PrimeService/AssemblyOutput
mkdir /home/PrimeService/AssemblyOutput
rm -rf /home/PrimeService/UnitTestOutput
mkdir /home/PrimeService/UnitTestOutput
cd $WORKSPACE
dotnet build --configuration Release --output /home/PrimeService/AssemblyOutput
dotnet test ./PrimeService.Tests/PrimeService.Tests.csproj --configuration Release --output /home/PrimeService/AssemblyOutput --logger "trx;LogFileName=TestResult.xml"
cp /var/lib/jenkins/workspace/UnitTest-Using-Nunit/PrimeService.Tests/TestResults/TestResult.xml /var/lib/jenkins/workspace/UnitTest-Using-Nunit/TestResult.xml
設定建置後動作
@設定jenkins service on your github
填入你的jenkins hook url
@結果
從github hook log確認觸發是否正常
從console log確認建置步驟是否正常(擷取部分)
Note:無法讀取NUinit3格式,錯誤如下
Error in NUnit processing: Could not transform the NUnit report. Please report this issue to the plugin author
ERROR: Step ‘Publish NUnit test result report’ failed: Could not read the XSL XML file. Please report this issue to the plugin author
目前NUnit plugin只能夠讀取NUnit2格式,所以我的Test Result Report無法正常讀取,
JIRA jenkins查到可以透過 nunit3-junit 進行轉換,
但CentOS7看來要透過mono來處理這段(寫在build scripts),後面再來看看有無其他好辦法。
參考
How to Integrate .NET Projects with Jenkins
Unit testing C# with NUnit and .NET Core
Prerequisites for .NET Core on Linux