CoreCRM 開發實錄——Travis-CI 實現 .NET Core 程度在 macOS 上的構建和測試 [無水乾貨]

來源:互聯網
上載者:User

標籤:ring   sdi   做了   open   except   iss   macos   配置   use   

上一篇文章我提到:為了使用“國貨”,我把 Linux 上的構建和測試委託給了 DaoCloud,而 Travis-CI 不能放著不用啊。還好,這貨支援 macOS 系統。所以就把 CoreCRM 在 macOS 上的構建和測試工作交給它了。

我想國內已經有很多寫怎麼用 Travis-CI 的部落格文章了,我就不需要在這裡多費話了。當然,最好的文章其實就是 Travis-CI 的文檔;最好的協助都在 StackOverflow 和 GitHub 上。如果還覺得自己英語不夠用,看不懂這些站的話,我覺得只有兩條路可以選:1. 學好英語;2. 放棄做程式員。

這裡我要記錄的是,使用 Travis-CI 構建 CoreCRM 時遇到的兩個問題。在我解決這兩個問題的過程中,我發現在這是兩個非常普遍的問題,基本上在現在的 .NET Core 版本下 (Microsoft.NETCore.App 1.1.0),是兩個肯定會遇到的問題。我在綜合了好多的 GitHub issue 之後,用了 10 個 commit 才把這兩個問題解決了。

1. OpenSSL 沒有安裝

首先遇到的問題是,在執行 dotnet restore 的時候,出現下面的異常:

Unhandled Exception: System.TypeInitializationException: The type initializer for ‘Crypto‘ threw an exception. ---> System.TypeInitializationException: The type initializer for ‘CryptoInitializer‘ threw an exception. ---> System.DllNotFoundException: Unable to load DLL ‘System.Security.Cryptography.Native‘: The specified module could not be found.    (Exception from HRESULT: 0x8007007E)        at Interop.CryptoInitializer.EnsureOpenSslInitialized()        at Interop.CryptoInitializer..cctor()        --- End of inner exception stack trace ---        at Interop.Crypto..cctor()        --- End of inner exception stack trace ---        at Interop.Crypto.GetRandomBytes(Byte* buf, Int32 num)        at System.IO.Path.GetCryptoRandomBytes(Byte* bytes, Int32 byteCount)        at System.IO.Path.GetRandomFileName()        at Microsoft.DotNet.InternalAbstractions.TemporaryDirectory..ctor()        at Microsoft.Extensions.EnvironmentAbstractions.DirectoryWrapper.CreateTemporaryDirectory()        at Microsoft.DotNet.Configurer.NuGetPackagesArchiver..ctor()        at Microsoft.DotNet.Cli.Program.ConfigureDotNetForFirstTimeUse(INuGetCacheSentinel nugetCacheSentinel)        at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, ITelemetry telemetryClient)        at Microsoft.DotNet.Cli.Program.Main(String[] args)/Users/travis/build.sh: line 57: 5310 Abort trap: 6 dotnet restore CoreCRM

這個問題是由於 openssl 的 libssl 這個動態庫沒有正確安裝造成的。在 Travis-CI 的環境裡,支援使用 Homebrew 來安裝缺少的組件,但是有一個問題,就是 openssl 需要手動 link 到 /usr/local/lib/ 裡才能使用。我最開始使用 Homebrew 提供的 link 功能,也沒有解決這個問題,只能自己使用 ln 來解決:

before_install:- brew install openssl- ln -s /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib /usr/local/lib/- ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/

把上面的 `before_install` 加到對應的位置,就可以解決上面這個問題了。(解決這個問題花了了大概 3 個小時的時間)

2. 開啟檔案太多

每種系統為了效能和安全的考慮,對一個進程能開啟的檔案數都做了限制。不過 macOS 的限制好像是特別的嚴格。在完成上而 restore 的過程這後,本來是要執行 test 的。結果確遇到下面的錯誤:

xUnit.net .NET CLI test runner (64-bit .NET Core osx.10.12-x64)Unhandled Exception: System.IO.IOException: Too many open files    at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)    at Interop.CheckIo[TSafeHandle](TSafeHandle handle, String path, Boolean isDirectory, Func`2 errorRewriter)    at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(Func`1 fdFunc)    at System.ConsolePal.OpenStandardOutput()    at Xunit.Runner.DotNet.Program.UseTestSinksWithStandardOutputStreams()    at Xunit.Runner.DotNet.Program.Run(String[] args)    at Xunit.Runner.DotNet.Program.Main(String[] args)SUMMARY: Total: 1 targets, Passed: 0, Failed: 1.

一開始找到的一些解決方案,可能是時間上有點久,針對的都是 Mono 下的一些方法,嘗試這後都不管用。在進一步尋找之後,找到了在 StackExchange 上的一個解決方案,就是增加允許開啟的檔案數:

script:- ulimit -n 2048- dotnet test CoreCRM.IntegrationTest

這樣,上面開啟檔案太多的問題就可以解決了。

完整的 .travis.yml 請到我的 GitHub 或者 Coding.NET 裡查看。在我寫完這篇文章的時候,三個 CI 平台,還只有 Travis-CI 是使用版本庫裡的設定檔完成配置的。後面我會把 AppVeyor 和 DaoCloud 的設定檔下載下來,放到版本庫裡,這樣大家就可以使用這些樣板實現自己的 CI 流程了。不過,現在這個時候,我還沒有把測試做到極致,還只是能看到全部測試是成功還是失敗。隨著後面的開發,我會讓測試的結束更詳細一些,以方便線上看到是哪些測試出了問題。

GitHub: https://github.com/holmescn/CoreCRM
Coding.NET: https://coding.net/u/holmescn/p/CoreCRM/git

CoreCRM 開發實錄——Travis-CI 實現 .NET Core 程度在 macOS 上的構建和測試 [無水乾貨]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.