AppVeyor, Travis CI and a .Net Core application

Sebastian Fischer
5 min readApr 8, 2018

--

You know all these cool badges that the most Github projects got? I want this too and get also the small benefit of having continuous build, or maybe the badges are the small benefit.

You know all these cool badges that the most Github projects got? I want this too and get also the small benefit of having continuous build, or maybe the badges are the small benefit.

What are my goals to reach? I want automatic build on Windows and Linux for the server project, collect test coverage information, check if all tests succeed and deploy the application to my server. So let’s begin…

AppVeyor

My first choice is AppVeyor currently, they offer different Windows-based build environments and so it`s I think the best choice for a .Net developer because you mostly work in such an environment.

The next good thing is, it’s free for open-source projects. So it’s very trivial to begin, you sign in with your Github account and choose the repository you want to build. After that, we can choose the build worker image currently, you can choose between these. The cool thing is you can configure a build matrix of different worker images. But I need only one at the moment. So I take Visual Studio 2017.

Build it

The first goal is to make it build, so after checkout and build the application in debug mode, the Nuget packages must be restored. Also, we need to install the codecov utility to upload the coverage created by OpenCover.
So this will be the before build script.

nuget restore choco install codecov

After building the debug application for testing, the application must be build in release mode to be ready for deployment.

dotnet publish .\src\Cointo.Web.Host — configuration Release — output %appveyor_build_folder%\dist

For optimizing the speed of the build, I don’t want to load all the Nuget packages over and over again. AppVeyor offers possibility to cache directories and files. To save the packages only this row is needed.

%USERPROFILE%\.nuget\packages

The build badge is completed.

Test it

Now it’s time to run the tests, here I need some trial and error and much reading through the internet to get it working.
The AppVeyor documentation about running tests and using OpenCover doesn’t really work for me. I think it’s describe the way for non .Net Core projects. Then I stumble upon this blog post that gives me more hints and also this one is a good read.
After getting this all together and many tries later I come to this solution. (here with full paths, but it would be better to use environment variables)

$openCoverConsole = $ENV:USERPROFILE + '\.nuget\packages\OpenCover\4.6.519\tools\OpenCover.Console.exe'$target = '-target:C:\Program Files\dotnet\dotnet.exe'
$filter = '-filter:+[*]* -[*]Cointo.Tests.* -[*]Cointo.EntityFrameworkCore.Seed.* -[*]Cointo.Migrations.* -[*]Cointo.Migrator.*'
$targetArgs = '-targetargs: C:\Users\appveyor\.nuget\packages\xunit.runner.console\2.3.1\tools\netcoreapp2.0\xunit.console.dll C:\cointoserver\test\Cointo.Tests\bin\Debug\netcoreapp2.0\Cointo.Tests.dll '
$output = '-output:C:\cointoserver\test\Cointo.Tests\coverage.xml'
& $openCoverConsole $target $targetArgs '-register:user' $filter $output '-oldStyle'codecov -f "C:\cointoserver\test\Cointo.Tests\coverage.xml"

The OpenCover documentation has a good explanation about the parameters. The last obstacle on this topic was this message.

Currently OpenCover has no support for the new pdb files from .Net Core, so I must change it to the full old style like here.

And now yes it finally works!

The code coverage badge is completed.

A note about OpenCover

It was a bit frustrating to get it running. While I trying to solve my problems I stumble upon the project coverlet. It’s very easy to setup, only add the nuget package and then it integrates in the msbuild process.

You need only to call this.

dotnet test C:\cointoserver\test\Cointo.Tests/Cointo.Tests.csproj /p:CollectCoverage=true /p:CoverletOutputFormat=opencover codecov -f "C:\cointoserver\test\Cointo.Tests\coverage.xml"

But I had the strange problem that not all my projects get a coverage result, so I returned for now to OpenCover. But it’s worth a look.

Depoly it

The last step is to deploy it. I go for WebDeploy here, because I use a Windows Server here. After activating WebDeploy for my page (over Plesk) I thought I only need to write the things down in AppVeyor and everything is fine… but it’s a bit more tricky. From Visual Studio everything works but on AppVeyor I get this.

After trying around I come to this solution, I had to append for the Server http://servername/MsDeployAgentService. And I need to activate NTLM authentication in AppVeyor, why? I really don’t know.
But after that the application get deployed, but wait I overwrite all the stuff on the system now with my local files. To remove files from publishing it’s needed to mark them in the csproj file like this.

<ItemGroup> 
<Content Update=”appsettings.json” CopyToPublishDirectory=”Never” /> </ItemGroup>
<ItemGroup>
<Content Update=”appsettings.Staging.json” CopyToPublishDirectory=”Never” />
</ItemGroup>

And it works.

Travis CI

language: csharp mono: none dotnet: 2.0.0 script: — dotnet restore — dotnet build ./Cointo.sln — dotnet test ./test/Cointo.Tests/Cointo.Tests.csproj- dotnet build ./Cointo.sln- dotnet test ./test/Cointo.Tests/Cointo.Tests.csproj

And this was my story about configurate the automatic build and my hunt for the badges.

The next step is to build the client application.

Originally published at marxiati.net on April 8, 2018.

--

--