GitHub的dotnet core CI实践(.net core xUnit OpenCover Appv

时间:2022-06-15 18:04:22

最近利用业余时间实现.ner core 版本的 casbin ,即 Casbin.NET。之前的CI都使用的是公司搭建的jenkinsgitlab-runner,对开源社区的工具链并不是很熟悉,在casbin的原作者(hsluoyz )的“要求”下,只能*在项目的README.md加入下面这些徽标:

GitHub的dotnet core CI实践(.net core   xUnit   OpenCover   Appv

 

GitHub的dotnet core CI实践(.net core   xUnit   OpenCover   Appv

NOTE:其实我只加了coverage 和 appveyor build 徽章。

 

使用的工具和平台如下:

  1. Appveyor

  2. OpenCover

  3. coveralls

  4. coveralls.net

  5. dotnet core 2.x

 

Appveyor 和 coveralls的注册以及github授权就略过不提。然后,只需要在项目中放入.appveyor.yml 配置文件配置就算是完成了。此处关注配置文件和实际过程中碰到的一些坑。

.appveyor.yml

version: ‘{build}‘
image: Visual Studio 2017
skip_tags: true
pull_requests:
  do_not_increment_build_number: true
dotnet_csproj:
  patch: true
  file: ‘***.csproj‘
  version: ‘{version}‘
  package_version: ‘{version}‘
environment:
  COVERALLS_REPO_TOKEN:
     secure: Ggx6TlzRHUhBJzBlXHLQOepde26ODej0NODgNKlczHfB3/jqTZMf8Ro123i1pVPz
build_script:
  - dotnet build -c Release
test_script:
- ps: >-
?
    nuget install xunit.runner.console -OutputDirectory packages -Version 2.3.1
    
    nuget install OpenCover -OutputDirectory packages -Version 4.7.922
?
    dotnet tool install coveralls.net --version 1.0.0 --tool-path tools
?
    cd .NetCasbin.UnitTestbinReleasenetcoreapp2.0
?
    ........packagesOpenCover.4.7.922toolsOpenCover.Console.exe -target:dotnet.exe "-targetargs:""........packagesxunit.runner.console.2.3.1toolsnetcoreapp2.0xunit.console.dll"" "".NetCasbin.UnitTest.dll"" -noshadow -appveyor" -filter:" [NetCasbin]*  -[NetCasbin.UnitTest]*" -register:user -oldStyle -output:........opencoverCoverage.xml
    
    cd ........ 
    
    $coveralls = ".toolscsmacnz.coveralls.exe"
?
    & $coveralls --opencover -i opencoverCoverage.xml --repoToken $env:COVERALLS_REPO_TOKEN --commitId $env:APPVEYOR_REPO_COMMIT --commitBranch $env:APPVEYOR_REPO_BRANCH --commitAuthor $env:APPVEYOR_REPO_COMMIT_AUTHOR --commitEmail $env:APPVEYOR_REPO_COMMIT_AUTHOR_EMAIL --commitMessage $env:APPVEYOR_REPO_COMMIT_MESSAGE --jobId $env:APPVEYOR_JOB_ID

 

坑 

  1. 错误1:

System.IO.FileNotFoundException: Could not load file or assembly ‘System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a‘. The system cannot find the file specified.
System.IO.FileNotFoundException: Could not load file or assembly ‘System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a‘. The system cannot find the file specified.
Committing...

 GitHub的dotnet core CI实践(.net core   xUnit   OpenCover   Appv

 

GitHub的dotnet core CI实践(.net core   xUnit   OpenCover   Appv

导致这个错误的原因很简单,使用正确的包版本(nuget install xunit.runner.console -OutputDirectory packages -Version 2.3.1),还有就是单元测试项目NetCasbin.UnitTest.csproj 的目标平台过高<TargetFrameworks>netcoreapp2.1</TargetFrameworks> ,改成<TargetFrameworks>netcoreapp2.0</TargetFrameworks>

  1. 错误2:

    System.InvalidOperationException: Could not find/load any of the following assemblies: xunit.execution.dotnet.dll
    Committing...

     

    和错误1相同单元测试项目NetCasbin.UnitTest.csproj 的目标平台过高<TargetFrameworks>netcoreapp2.1</TargetFrameworks> ,改成<TargetFrameworks>netcoreapp2.0</TargetFrameworks>

  2. 错误3:

    Using Git Data Provider ‘Command line Arguments‘
    csmacnz.Coveralls.exe : Failed to upload to coveralls
    At line:9 char:1
      & $coveralls --opencover -i opencoverCoverage.xml --repoToken $env:CO ...
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          CategoryInfo          : NotSpecified: (Failed to upload to coveralls:String) [], RemoteException
          FullyQualifiedErrorId : NativeCommandError
     
    UnprocessableEntity - Couldn‘t find a repository matching this job.
    Command executed with exception: UnprocessableEntity - Couldn‘t find a repository matching this job.

    ?GitHub的dotnet core CI实践(.net core   xUnit   OpenCover   Appv

     

    GitHub的dotnet core CI实践(.net core   xUnit   OpenCover   Appv

导致这个错误的原因其实是.appveyor.yml配置文件中定义的环境变量COVERALLS_REPO_TOKENsecure的值错误,正确的值应该是原始coveralls中项目的COVERALLS_REPO_TOKEN 的密文,appveyor也提供了加密的工具 https://ci.appveyor.com/tools/encrypt,将密文作为COVERALLS_REPO_TOKENsecure的值即可。

  1. 错误4:

    如果上面的问题都排除了还有下面的问题:

    Committing...
    No results, this could be for a number of reasons. The most common reasons are:
        1) missing PDBs for the assemblies that match the filter please review the
        output file and refer to the Usage guide (Usage.rtf) about filters.
        2) the profiler may not be registered correctly, please refer to the Usage
        guide and the -register switch.

     

那需要在项目的.csproj文件中加入<DebugType>full</DebugType>

 GitHub的dotnet core CI实践(.net core   xUnit   OpenCover   Appv

 

 

最后要说的是,营造一个好的.net core生态需要每一位喜欢c#语言的coder参与者,这里希望能够通过github 的CI实践,抛砖引玉,吸引更多的参与者加入到开源社区。

 

下一篇文章:一个权限引擎的作用,Cabin.NET的使用