閱讀目錄:
- install configue gitlab-ci-multi-runner
- restore nuget packages
- bulid .sln
- run unit tests
- configue .gitlab-ci.yml
- configue build status badge image
CI 精華文章:
Gitlab 部署 CI 相關資料:
持續集成(Continuous integration - CI)的作用:代碼在提交到資源庫之前,進行構建、自動化測試和發布等等,我們每天需要提交大量的代碼,持續集成可以有效的幫助我們發現代碼中的 Bug,并且減少一些反復的工作等等,使團隊更加有效的開發協作。
GitLab CI 官方介紹:https://about.gitlab.com/gitlab-ci/
Gitlab 在 8.0 以上版本集成了 CI,所以我們不需要另外配置一個 gitlab-ci-server 服務器,為我們部署減少了很多的工作,點個贊👍!
先吐槽下,Gitlab 部署 CI 我大概花了一周的時間,但也只是進行了一點點,最重要的三點:nuget restore
, bulid *.sln
和run unit tests
現在基本上是可以了,在部署的過程中,深感到問題分享的重要性,遇到的大量問題,Google 基本上搜不到,中文相關資料也就上面的幾篇文章,但看過之后發現都是簡簡單單的介紹而已,并沒有記錄詳細的部署過程,所以,我基本上都是看的 Gitlab 官方幫助文檔,但 Gitlab 的更新很頻繁,所以有些幫助文檔都沒進行更新,避免不了踩進一些坑,那怎么辦呢?解決方式就是不斷的進行嘗試,比如我配置.gitlab-ci.yml
文件的時候,就不斷的進行code commit
測試(一百多個提交😂):
并且有先見之明的把問題解決過程,都用 Issue 進行記錄了😏:
下面就從上面這幾個 Issue 進行展開,把每個問題和解決過程都分享出來,希望可以幫助到遇到相同問題的園友。
1. install configue gitlab-ci-multi-runner
GitLab 部署 CI 的第一步就是安裝 gitlab-ci-multi-runner,你可以把它理解為:跑 CI 的服務。
windows 安裝教程:https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/blob/master/docs/install/windows.md
下載好 gitlab-ci-multi-runner-windows-amd64.exe 安裝文件后,將安裝文件放在C:\Multi-Runner
下,以管理員權限運行命令行,如果gitlab-ci-multi-runner
命令找不到,直接用gitlab-ci-multi-runner-windows-amd64.exe
命令運行。
在 Gitlab 項目中打開 Settings > Runners,找到URL
和token
,等會安裝的時候需要配置。
安裝配置步驟:
C:\WINDOWS\system32>cd C:\Multi-Runner
C:\Multi-Runner>gitlab-ci-multi-runner-windows-amd64.exe register
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/ci):
URL
Please enter the gitlab-ci token for this runner:
token
Please enter the gitlab-ci description for this runner:
[DESKTOP-2P9GHDD]: xishuai-ci
Please enter the gitlab-ci tags for this runner (comma separated):
dev
Registering runner... succeeded runner=avuSXASJ
Please enter the executor: docker-ssh, parallels, shell, ssh, virtualbox, docker+machine, docker-ssh+machine, docker:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
上面executor: shell
是默認配置,意思是本地執行,也可以使用ssh
和docker
,不過需要增加一些遠端鏈接配置。
完成后,會在C:\Multi-Runner
目錄下,生成一個config.toml
配置文件,我們上面輸入的配置信息也都會在這里面,配置說明:https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/blob/master/docs/configuration/advanced-configuration.md
停止,運行和驗證命令:
C:\Multi-Runner>gitlab-ci-multi-runner-windows-amd64.exe stop
C:\Multi-Runner>gitlab-ci-multi-runner-windows-amd64.exe start
C:\Multi-Runner>gitlab-ci-multi-runner-windows-amd64.exe verify
Verifying runner... is alive runner=5ae63365
如果運行C:\Multi-Runner>gitlab-ci-multi-runner-windows-amd64.exe start
出現錯誤,則需要將gitlab-ci-multi-runner-windows-amd64.exe
拷貝一份,重命名為gitlab-ci-multi-runner.exe
。
另外, Gitlab 項目 Settings > Project Settings Features > Builds 選項需要打勾。
gitlab-ci-multi-runner 安裝配置完之后,我們就可以在 Gitlab 項目 Settings > Runners 中,看到 Runners 的信息了。
2. restore nuget packages
這次任務:使用 CI, nuget 還原解決方案中的程序包。
gitlab-ci-multi-runner 安裝配置完之后,我們還需要在 Gitlab 項目中添加一個.gitlab-ci.yml
文件,官方介紹:http://doc.gitlab.com/ee/ci/yaml/README.html
因為一開始我對.gitlab-ci.yml
配置一點都不了解,所以,我當時按照這個教程 CI Quick Start,添加了如下的.gitlab-ci.yml
文件配置:
before_script:
- apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs
- ruby -v
- which ruby
- gem install bundler --no-ri --no-rdoc
- bundle install --jobs $(nproc) "${FLAGS[@]}"
rspec:
script:
- bundle exec rspec
rubocop:
script:
- bundle exec rubocop
添加好.gitlab-ci.yml
文件配置后,我們就可以在項目中的 Builds,看到提交后的構建工作了,隨便在 Gitlab 項目中添加一個解決方案,然后再添加一個類庫項目,并且使用 nuget 安裝一個程序包,最后使用 git 提交到 Gitlab 中,就可以看到 Builds 的過程和結果了,首次提交結果如下:
gitlab-ci-multi-runner 1.1.3 (a470667)
Using Shell executor...
Running on DESKTOP-2P9GHDD...
Cloning repository...
'"git"' �����ڲ����ⲿ���Ҳ���ǿ����еij���
���������ļ���
ϵͳ�Ҳ���ָ����·����
Checking out 2f82ccb0 as master...
'"git"' �����ڲ����ⲿ���Ҳ���ǿ����еij���
���������ļ���
ERROR: Build failed: exit status 9009
這個問題搞了我很久,因為錯誤信息亂碼了,根本找不到相關的解決方案,后來無意間搜到 Gitlab 中的一個 Issue,里面提到了一個gitlab-ci-multi-runner --debug run
命令,意思是調試運行 CI,這樣我們就可以看到詳細的錯誤信息了,debug 的錯誤信息比較多,并且完全看不懂,不過我們可以通過 Builds 看到簡潔的錯誤日志:
gitlab-ci-multi-runner 1.1.3 (a470667)
Using Shell executor...
Running on DESKTOP-2P9GHDD...
Cloning repository...
Cloning into 'C:/Multi-Runner/builds/500c7a25/0/dev/CNBlogsCI-Sample'...
fatal: unable to access 'https://gitlab-ci-token:xxxxxx@gitlab.com/dev/CNBlogsCI-Sample.git/': error setting certificate verify locations:
CAfile: C:\Multi-Runner\builds\500c7a25\0\dev\CNBlogsCI-Sample.tmp\GIT_SSL_CAINFO
CApath: none
The system cannot find the path specified.
Checking out ac05d090 as master...
fatal: Not a git repository (or any of the parent directories): .git
ERROR: Build failed: exit status 128
上面錯誤日志的意思是,沒有git clone repository
成功,并且沒有權限訪問,后來 Google 到了一個解決方案:error setting certificate verify locations
解決方式:C:\Multi-Runner\config.toml
文件添加shell = 'powershell'
節點,添加在[[runners]]
節點后。
解決完這個問題之后,去研究了下.gitlab-ci.yml
中的nuget restore
配置(Google 搜的,太坑),將.gitlab-ci.yml
文件修改如下:
stages:
- build
job:
stage: build
script:
- ls
- echo "Restoring NuGet Packages..."
- '"C:\Users\xishuai\.dnx\packages\ClassLibrary2\2.0.0\packages\NuGet.CommandLine.2.8.5\tools\NuGet.exe" restore "src/CNBlogsCI-Sample.sln"'
commit
提交測試,出現下面的錯誤信息:
gitlab-ci-multi-runner 1.1.3 (a470667)
Using Shell executor...
At C:\Users\xishuai\AppData\Local\Temp\build_script140243225\script.ps1:132 char:105
+ ... 0.0\packages\NuGet.CommandLine.2.8.5\tools\NuGet.exe" restore "src/CN ...
+ ~~~~~~~
Unexpected token 'restore' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
ERROR: Build failed: exit status 1
從錯誤信息中可以看到,沒有識別restore
命令,啥意思?這個問題又搞了我好久,Google Unexpected token 'restore' in expression or statement.
關鍵字,毛都搜不到,沒辦法,后來只能更換關鍵字搜,但搜到的信息鳳毛麟角,后來參考搜來的資料,將.gitlab-ci.yml
改為:
stages:
- build
job:
stage: build
script:
- ls
- echo "Restoring NuGet Packages..."
- 'call "%VS140COMNTOOLS%\vsvars32.bat"'
# - '"C:\Users\xishuai\.dnx\packages\ClassLibrary2\2.0.0\packages\NuGet.CommandLine.2.8.5\tools\NuGet.exe" restore "src\CNBlogsCI-Sample.sln"'
或
before_script:
- echo "Restoring NuGet Packages..."
- 'call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\vsvars32.bat"'
%VS140COMNTOOLS%\vsvars32.bat
是什么鬼?不太清楚,毫無疑問,又出現了錯誤,信息如下:
gitlab-ci-multi-runner 1.1.3 (a470667)
Using Shell executor...
Running on DESKTOP-2P9GHDD...
Fetching changes...
HEAD is now at 3926803 Update .gitlab-ci.yml
From https://gitlab.com/dev/CNBlogsCI-Sample
3926803..d8f10a7 master -> origin/master
Checking out d8f10a7c as master...
Previous HEAD position was 3926803... Update .gitlab-ci.yml
HEAD is now at d8f10a7... Update .gitlab-ci.yml
$ ls
Directory: C:\Multi-Runner\builds\5ae63365\0\dev\CNBlogsCI-Sample
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 5/4/2016 02:45 PM src
-a---- 5/4/2016 02:45 PM 89 .gitignore
-a---- 5/4/2016 02:49 PM 527 .gitlab-ci.yml
$ echo "Restoring NuGet Packages..."
Restoring NuGet Packages...
$ call "%VS140COMNTOOLS%\vsvars32.bat"
call : The term 'call' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\xishuai\AppData\Local\Temp\build_script250102679\script.ps1:132 char:3
+ call "%VS140COMNTOOLS%\vsvars32.bat"
+ ~~~~
+ CategoryInfo : ObjectNotFound: (call:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : CommandNotFoundException
ERROR: Build failed: exit status 1
也是毫無頭緒的錯誤,這么辦呢?后來想想nuget restore
始終不成功,能不能換個命令呢?突然想到了 ASP.NET 5,還原程序包使用的是dnu restore
命令,那就嘗試下吧,將解決方案中的項目刪掉,然后添加 ASP.NET 5 項目,.gitlab-ci.yml
改為:
before_script:
- echo "Restoring NuGet Packages..."
- dnvm use 1.0.0-beta5 -r coreclr -a x64
- dnu restore
哇塞,這次終于成功了(突然有種想哭的沖動😭),日志信息:
Running on DESKTOP-2P9GHDD...
Fetching changes...
HEAD is now at 33436d8 test commit
From https://gitlab.com/dev/CNBlogsCI-Sample
33436d8..c80b2d5 master -> origin/master
Checking out c80b2d5d as master...
Previous HEAD position was 33436d8... test commit
HEAD is now at c80b2d5... test commit
$ echo "Release build..."
Release build...
$ dnvm use 1.0.0-beta5 -r coreclr -a x64
Adding C:\Users\xishuai\.dnx\runtimes\dnx-coreclr-win-x64.1.0.0-beta5\bin to process PATH
$ dnu restore
Microsoft .NET Development Utility CoreCLR-x64-1.0.0-beta5-12103
Restoring packages for C:\Multi-Runner\builds\5ae63365\0\dev\CNBlogsCI-Sample\src\CNBlogsCI-Sample.ClassLibrary\project.json
GET https://www.nuget.org/api/v2/
OK https://www.nuget.org/api/v2/ 5524ms
GET http://nuget.cnitblog.com/nuget/Default/
OK http://nuget.cnitblog.com/nuget/Default/ 2406ms
GET https://www.myget.org/F/aspnetvnext/api/v2/
OK https://www.myget.org/F/aspnetvnext/api/v2/ 5225ms
CACHE https://www.nuget.org/api/v2/
GET https://www.myget.org/F/aspnetmaster/api/v3/index.json
OK https://www.myget.org/F/aspnetmaster/api/v3/index.json 2938ms
GET https://www.myget.org/F/xunit/api/v3/index.json
OK https://www.myget.org/F/xunit/api/v3/index.json 1976ms
Writing lock file C:\Multi-Runner\builds\5ae63365\0\dev\CNBlogsCI-Sample\src\CNBlogsCI-Sample.ClassLibrary\project.lock.json
Restore complete, 18775ms elapsed
Build succeeded
雖然 ASP.NET 5 還原程序包成功了,但依舊解決不了問題啊,因為必須得解決nuget restore
的問題,因為很多項目都沒用 ASP.NET 5,怎么辦呢?又回到了出發點,問題能磨死人啊,過程就不敘述了,后來無意間將.gitlab-ci.yml
改為:
before_script:
- echo "Restoring NuGet Packages..."
- C:\Program Files (x86)\NuGet\nuget.exe restore src/CNBlogsCI-Sample.sln
仔細看看和上面的配置有什么不同,我把'"
去掉了,commit
代碼測試,出現了下面和一開始不一樣的錯誤(有戲了😏):
gitlab-ci-multi-runner 1.1.3 (a470667)
Using Shell executor...
Running on DESKTOP-2P9GHDD...
Fetching changes...
Removing src/ClassLibrary1/bin/
Removing src/ClassLibrary1/obj/
HEAD is now at 191e7e0 test commit
From https://gitlab.com/dev/CNBlogsCI-Sample
191e7e0..feebdef master -> origin/master
Checking out feebdefb as master...
Previous HEAD position was 191e7e0... test commit
HEAD is now at feebdef... test commit
$ echo "Restoring NuGet Packages..."
Restoring NuGet Packages...
$ C:\Program Files (x86)\NuGet\nuget.exe restore src/CNBlogsCI-Sample.sln
x86 : The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\xishuai\AppData\Local\Temp\build_script166211738\script.ps1:128 char:21
+ C:\Program Files (x86)\NuGet\nuget.exe restore src/CNBlogsCI-Sample ...
+ ~~~
+ CategoryInfo : ObjectNotFound: (x86:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : CommandNotFoundException
ERROR: Build failed: exit status 1
根據上面的錯誤日志,可以看到,就是目錄中的x86
問題,然后我把目錄改為C:\Program Files\NuGet\nuget.exe
之后(nuget.exe
拷貝到相應目錄下),還是有問題,然后就直接放在C
盤目錄下,終于build
成功(眼淚奪眶而出😂)。
.gitlab-ci.yml
配置:
before_script:
- echo "Restoring NuGet Packages..."
- C:\NuGet\nuget.exe restore "src\CNBlogsCI-Sample.sln"
build
成功日志:
gitlab-ci-multi-runner 1.1.3 (a470667)
Using Shell executor...
Running on DESKTOP-2P9GHDD...
Fetching changes...
HEAD is now at 1ac80d7 test commit
From https://gitlab.com/dev/CNBlogsCI-Sample
1ac80d7..683a8bc master -> origin/master
Checking out 683a8bcb as master...
Previous HEAD position was 1ac80d7... test commit
HEAD is now at 683a8bc... test commit
$ echo "Restoring NuGet Packages..."
Restoring NuGet Packages...
$ C:\NuGet\nuget.exe restore "src\CNBlogsCI-Sample.sln"
Installing 'AutoMapper 4.2.1'.
Successfully installed 'AutoMapper 4.2.1'.
Build succeeded
看似簡單的結果,但過程真是太扯蛋了,如果我當時看到類似這篇博文分享,也不至于如此,還沒完,繼續。。。
3. build *.sln
這次任務:使用 CI, build 生成解決方案中的項目。
生成解決方案的問題解決過程相對簡單些,不過上面漏掉了一處,這邊再補充下,.gitlab-ci.yml
配置:
stages:
- build
job:
stage: build
script:
- echo "Release build..."
- C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe /consoleloggerparameters:ErrorsOnly /maxcpucount /nologo /property:Configuration=Release /verbosity:quiet "CNBlogsCI-Sample.sln"
tags:
except:
- tags
錯誤日志:
gitlab-ci-multi-runner 1.1.3 (a470667)
Using Shell executor...
Running on DESKTOP-2P9GHDD...
Fetching changes...
HEAD is now at 07a6ffd Merge branch 'master' of gitlab.com:dev/CNBlogsCI-Sample
From https://gitlab.com/dev/CNBlogsCI-Sample
07a6ffd..73bd820 master -> origin/master
Checking out 73bd8207 as master...
Previous HEAD position was 07a6ffd... Merge branch 'master' of gitlab.com:dev/CNBlogsCI-Sample
HEAD is now at 73bd820... test commit
$ echo "Release build..."
Release build...
$ C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe /consoleloggerparameters:ErrorsOnly /maxcpucount /nologo /property:Configuration=Release /verbosity:quiet "CNBlogsCI-Sample.sln"
MSBUILD : error MSB1009: ��Ŀ�ļ������ڡ�
����: CNBlogsCI-Sample.sln
ERROR: Build failed: exit status 1
這個錯誤和最開始的亂碼錯誤一樣,未知的錯誤,無從下手,后來,又無意間搜到了一個 Gitlab Issue(好多無意間😄,沒辦法,Google 只能搜索所有可能的關鍵字):Question about local project path
.gitlab-ci.yml
配置改為:
stages:
- build
job:
stage: build
script:
- ls
- echo "Release build..."
- C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe /consoleloggerparameters:ErrorsOnly /maxcpucount /nologo /property:Configuration=Release /verbosity:quiet "CNBlogsCI-Sample.sln"
tags:
except:
- tags
然后看到了詳細錯誤(又有戲了😏):
gitlab-ci-multi-runner 1.1.3 (a470667)
Using Shell executor...
Running on DESKTOP-2P9GHDD...
Fetching changes...
HEAD is now at eb2ec26 Update .gitlab-ci.yml
Checking out eb2ec265 as master...
HEAD is now at eb2ec26... Update .gitlab-ci.yml
$ ls
Directory: C:\Multi-Runner\builds\5ae63365\0\dev\CNBlogsCI-Sample
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 5/4/2016 10:26 AM src
-a---- 5/4/2016 11:19 AM 315 .gitlab-ci.yml
$ echo "Release build..."
Release build...
$ C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe /consoleloggerparameters:ErrorsOnly /maxcpucount /nologo /property:Configuration=Release /verbosity:quiet "CNBlogsCI-Sample.sln"
MSBUILD : error MSB1009: Project file does not exist.
Switch: CNBlogsCI-Sample.sln
ERROR: Build failed: exit status 1
error MSB1009: Project file does not exist.
這個錯誤就很清晰了,項目文件找不到,也就是沒有找到CNBlogsCI-Sample.sln
,怎么會呢?重新查看了 Gitlab 中的項目文件目錄,CNBlogsCI-Sample.sln
在根目錄下的src
目錄下,重新修改下.gitlab-ci.yml
配置:
stages:
- build
job:
stage: build
script:
- ls
- echo "Release build..."
- C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe /consoleloggerparameters:ErrorsOnly /maxcpucount /nologo /property:Configuration=Release /verbosity:quiet "src/CNBlogsCI-Sample.sln"
tags:
except:
- tags
build
成功,日志詳情:
gitlab-ci-multi-runner 1.1.3 (a470667)
Using Shell executor...
Running on DESKTOP-2P9GHDD...
Fetching changes...
HEAD is now at a51aeea test commit
From https://gitlab.com/dev/CNBlogsCI-Sample
a51aeea..170fbc4 master -> origin/master
Checking out 170fbc4a as master...
Previous HEAD position was a51aeea... test commit
HEAD is now at 170fbc4... test commit
$ ls
Directory: C:\Multi-Runner\builds\5ae63365\0\dev\CNBlogsCI-Sample
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 5/4/2016 11:38 AM src
-a---- 5/4/2016 11:38 AM 319 .gitlab-ci.yml
$ echo "Release build..."
Release build...
$ C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe /consoleloggerparameters:ErrorsOnly /maxcpucount /nologo /property:Configuration=Release /verbosity:quiet "src/CNBlogsCI-Sample.sln"
Build succeeded
4. run unit tests
這次任務:使用 CI, run 跑解決方案中的單元測試,可以成為自動化測試。
這次基本上沒有什么問題解決過程,因為 Google 完全搜不到相關資料,所以,我最后是按照我的想法實現的,xUnit 除了用 VS2015 進行跑單元測試外,我們還可以用命令行的方式,打開 cmd
輸入:C:\xunit.runner.console\tools\xunit.console.exe "src\ClassLibrary2\bin\debug\ClassLibrary2.dll"
,結果如下:
C:\Users\xishuai\Desktop\CNBlogs\CNBlogsCI-Sample\src> C:\xunit.runner.console\tools\xunit.console.exe "src\ClassLibrary2\bin\debug\ClassLibrary2.dll"
xUnit.net Console Runner (64-bit .NET 4.0.30319.42000)
Discovering: ClassLibrary2
Discovered: ClassLibrary2
Starting: ClassLibrary2
ClassLibrary2.Class1.Test2 [FAIL]
Assert.True() Failure
Expected: True
Actual: False
Stack Trace:
ClassLibrary2\Class1.cs(21,0): at ClassLibrary2.Class1.Test2()
Finished: ClassLibrary2
=== TEST EXECUTION SUMMARY ===
ClassLibrary2 Total: 2, Errors: 0, Failed: 1, Skipped: 0, Time: 0.224s
好,既然命令行可以跑單元測試,那么我們就可以在.gitlab-ci.yml
中添加腳本配置,如下:
stages:
- build
- test
before_script:
- echo "Restoring NuGet Packages..."
- C:\NuGet\nuget.exe restore "src\CNBlogsCI-Sample.sln"
build_job:
stage: build
script:
- echo "Release build..."
- C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe /consoleloggerparameters:ErrorsOnly /maxcpucount /nologo /property:Configuration=Release /verbosity:quiet "src\CNBlogsCI-Sample.sln"
except:
- tags
test_job:
stage: test
script:
- echo "Tests run..."
- C:\xunit.runner.console\tools\xunit.console.exe "src\ClassLibrary2\bin\debug\ClassLibrary2.dll"
- C:\xunit.runner.console\tools\xunit.console.exe "src\ClassLibrary3\bin\debug\ClassLibrary3.dll"
xUnit 單元測試不通過日志:
gitlab-ci-multi-runner 1.1.3 (a470667)
Using Shell executor...
Running on DESKTOP-2P9GHDD...
Fetching changes...
Removing src/ClassLibrary1/bin/
Removing src/ClassLibrary1/obj/
Removing src/ClassLibrary2/bin/Release/
Removing src/ClassLibrary2/obj/
Removing src/ClassLibrary3/bin/
Removing src/ClassLibrary3/obj/
Removing src/packages/
HEAD is now at d176025 test commit
Checking out d1760259 as master...
HEAD is now at d176025... test commit
$ echo "Restoring NuGet Packages..."
Restoring NuGet Packages...
$ C:\NuGet\nuget.exe restore "src\CNBlogsCI-Sample.sln"
Installing 'xunit.abstractions 2.0.0'.
Installing 'xunit.assert 2.1.0'.
Installing 'xunit 2.1.0'.
Installing 'AutoMapper 4.2.1'.
Successfully installed 'xunit 2.1.0'.
Installing 'xunit.core 2.1.0'.
Successfully installed 'xunit.abstractions 2.0.0'.
Successfully installed 'xunit.core 2.1.0'.
Installing 'xunit.extensibility.core 2.1.0'.
Successfully installed 'xunit.assert 2.1.0'.
Installing 'xunit.extensibility.execution 2.1.0'.
Installing 'xunit.runner.console 2.1.0'.
Successfully installed 'AutoMapper 4.2.1'.
Successfully installed 'xunit.runner.console 2.1.0'.
Successfully installed 'xunit.extensibility.core 2.1.0'.
Successfully installed 'xunit.extensibility.execution 2.1.0'.
$ echo "Tests run..."
Tests run...
$ C:\xunit.runner.console\tools\xunit.console.exe "src\ClassLibrary2\bin\debug\ClassLibrary2.dll"
xUnit.net Console Runner (64-bit .NET 4.0.30319.42000)
Discovering: ClassLibrary2
Discovered: ClassLibrary2
Starting: ClassLibrary2
Finished: ClassLibrary2
=== TEST EXECUTION SUMMARY ===
ClassLibrary2 Total: 2, Errors: 0, Failed: 0, Skipped: 0, Time: 0.179s
$ C:\xunit.runner.console\tools\xunit.console.exe "src\ClassLibrary3\bin\debug\ClassLibrary3.dll"
error: file not found: src\ClassLibrary3\bin\debug\ClassLibrary3.dll
ERROR: Build failed: exit status 1
xUnit 單元測試通過日志:
gitlab-ci-multi-runner 1.1.3 (a470667)
Using Shell executor...
Running on DESKTOP-2P9GHDD...
Fetching changes...
Removing src/ClassLibrary1/bin/
Removing src/ClassLibrary1/obj/
Removing src/ClassLibrary2/bin/Release/
Removing src/ClassLibrary2/obj/
Removing src/ClassLibrary3/bin/Release/
Removing src/ClassLibrary3/obj/
Removing src/packages/
HEAD is now at 2467772 test commit
Checking out 2467772f as master...
HEAD is now at 2467772... test commit
$ echo "Restoring NuGet Packages..."
Restoring NuGet Packages...
$ C:\NuGet\nuget.exe restore "src\CNBlogsCI-Sample.sln"
Installing 'AutoMapper 4.2.1'.
Installing 'xunit.abstractions 2.0.0'.
Installing 'xunit.assert 2.1.0'.
Installing 'xunit 2.1.0'.
Successfully installed 'xunit 2.1.0'.
Installing 'xunit.core 2.1.0'.
Successfully installed 'xunit.abstractions 2.0.0'.
Successfully installed 'xunit.core 2.1.0'.
Installing 'xunit.extensibility.execution 2.1.0'.
Installing 'xunit.extensibility.core 2.1.0'.
Successfully installed 'xunit.assert 2.1.0'.
Installing 'xunit.runner.console 2.1.0'.
Successfully installed 'AutoMapper 4.2.1'.
Successfully installed 'xunit.runner.console 2.1.0'.
Successfully installed 'xunit.extensibility.core 2.1.0'.
Successfully installed 'xunit.extensibility.execution 2.1.0'.
$ echo "Tests run..."
Tests run...
$ C:\xunit.runner.console\tools\xunit.console.exe "src\ClassLibrary2\bin\debug\ClassLibrary2.dll"
xUnit.net Console Runner (64-bit .NET 4.0.30319.42000)
Discovering: ClassLibrary2
Discovered: ClassLibrary2
Starting: ClassLibrary2
Finished: ClassLibrary2
=== TEST EXECUTION SUMMARY ===
ClassLibrary2 Total: 2, Errors: 0, Failed: 0, Skipped: 0, Time: 0.194s
$ C:\xunit.runner.console\tools\xunit.console.exe "src\ClassLibrary3\bin\debug\ClassLibrary3.dll"
xUnit.net Console Runner (64-bit .NET 4.0.30319.42000)
Discovering: ClassLibrary3
Discovered: ClassLibrary3
Starting: ClassLibrary3
Finished: ClassLibrary3
=== TEST EXECUTION SUMMARY ===
ClassLibrary3 Total: 1, Errors: 0, Failed: 0, Skipped: 0, Time: 0.184s
Build succeeded
基本上實現了我們想要的效果,但這種實現方式有兩個不好的地方:
- 需要將單元測試的
*.dll
文件上傳到 git 資源庫。 - 每增加一個單元測試項目,就必須在
.gitlab-ci.yml
中添加一段腳本。
我個人覺得 CI 中的自動化測試,肯定不是像我這樣搞的,但實在找不到相關資料,如果大家知悉,還請告知,感謝~
另外,如果是 ASP.NET 5 項目,進行自動化測試配置,會非常簡單,配置如下:
test:
stage: test
script:
- echo "Tests run..."
- dnx test #或者 dotnet test
5. configue .gitlab-ci.yml
.gitlab-ci.yml
官方資料:http://doc.gitlab.com/ee/ci/yaml/README.html
其他示例:
- http://stackoverflow.com/questions/32964953/gitlab-ci-and-msbuild-with-tests
- https://github.com/CWISoftware/accounts/blob/master/.gitlab-ci.yml
- http://www.timtilberg.com/tag/gitlab/
- http://doc.gitlab.com/ee/ci/yaml/README.html#stages
- https://github.com/travis-ci/travis-ci/issues/5210
.gitlab-ci.yml
中的配置說明,上面的官方資料介紹的非常詳細,下面我再簡單介紹下,就用我這次部署 CI 完善后的.gitlab-ci.yml
配置:
stages:
- build
- test
before_script:
- echo "Restoring NuGet Packages..."
- C:\NuGet\nuget.exe restore "src\CNBlogsCI-Sample.sln"
only:
- master
build_job:
stage: build
script:
- echo "Release build..."
- C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe /consoleloggerparameters:ErrorsOnly /maxcpucount /nologo /property:Configuration=Release /verbosity:quiet "src\CNBlogsCI-Sample.sln"
except:
- tags
only:
- master
test_job:
stage: test
script:
- echo "Tests run..."
- C:\xunit.runner.console\tools\xunit.console.exe "src\ClassLibrary2\bin\debug\ClassLibrary2.dll"
- C:\xunit.runner.console\tools\xunit.console.exe "src\ClassLibrary3\bin\debug\ClassLibrary3.dll"
only:
- master
stage
翻譯為階段的意思,在構建的過程中,必須要有一個先后順序,最上面的stages
配置意思是,先構建階段為build
的job
,然后再構建階段為test
的job
,下面build_job
和test_job
都是job
,如果不配置stages
,默認為:
stages:
- build
- test
- deploy
before_script
的意思是,執行在所有的job
之前的腳本,比如構建build_job
和test_job
都先執行before_script
,build_job
和test_job
中的stage
配置,意思是此job
屬于哪個stage
,這個stage
就是最上面的stages
配置,除了默認的build
,test
和deploy
,你也可以添加自定義的stage
,另外,如果job
不添加stage
配置,默認配置為test
,比如上面的test_job
,就可以省略stage: test
配置。
另外,job
還有一個when: on_failure/on_success /always
配置,如果我們對job
進行了stage
配置,默認都會是when: on_success
:
only - master
的意思是,只有`master
分支才會進行構建,script
的意思很明了,就是要執行的腳本命名。
6. configue build status badge image
構建狀態徽章,就是我們平常在 Github 項目中看到構建圖標,有pass
和failing
等等。
Gitlab CI 中的教程 builds-badge 真的很坑爹,怎么試都不行,后來無意間看到 Gitlab 項目的一個選項 Settings > Badges:
復制上面的代碼,然后添加在README.md
文件中:
[](https://gitlab.com/dev/CNBlogsCI-Sample/commits/master)
這樣在commit``bulid
的時候,就會動態的顯示bulid
的過程和結果,并且是圖片顯示。
Gitlab 部署好 CI 之后,我們會發現,在項目中隨處可見這樣的圖標:
這篇博文沒有什么閱讀價值,因為都是零零碎碎的問題和解決紀錄,沒有什么可讀性,如果你能閱讀到這,我真的會很感動。
分享是有價值的一件事,如果園友在遇到相同問題的時候,可以 Google 到這篇博文,那寫這篇博文也就值了😏。
文章列表