After porting FlacLibSharp to .NET Standard, publishing it to NuGet and building it on Linux. I felt I needed to add Travis CI continuous builds to my Github repo.
I thought that this would be a lot easier than it was in the past, because I made builds on Linux work.
Summary (TL;DR)
To get FlacLibSharp to build and test in .NET Core automatically on every commit I did the following:
- Log in to Travis CI with my github account.
- Went to "Accounts"
- Activated Travis CI for the appropriate github repo (in my case FlacLibSharp)
- Added a file, named ".travis.yml", in the root of my project, with the following content:
language: csharp
mono: none
dotnet: 2.0.0
dist: trusty
script:
- cd FlacLibSharp.Test.Core
- dotnet test
- Pushed this to github.
- In my readme.md added the following to show the build badge:
[![Build Status](https://travis-ci.org/AaronLenoir/flaclibsharp.svg?branch=master)](https://travis-ci.org/AaronLenoir/flaclibsharp)
Now, every time I push changes to a branch, Travis CI will try to build it for me.
It will build when:
- A change is pushed to a branch
- Periodically for your main branch
- Any pull request to your main branch (so you know it builds before you accept the pull request).
Travis Configuration
In the repository, we need a file called .travis.yml
The default example offered is the following:
language: csharp
mono: none
dotnet: 1.0.3
dist: trusty
script:
- dotnet restore
Apperently, dist: trusty
indicates Ubuntu 14 instead of the standard Ubuntu 12. According to the documentation.
Some things are probably wrong here:
- I want .NET Core 2.0
- I only want to build and test FlacLibSharp.Test.Core, not the entire solution.
So let's already try to solve the first problem by changing dotnet: 1.0.3
to dotnet: 2.0.0
language: csharp
mono: none
dotnet: 2.0.0
dist: trusty
script:
- dotnet restore
First Build Attempt
I add the .travis.yml file in my dotnetstandard branch, commit and push to github.
As soon as I push the changes, Travis CI makes an attempt at building my stuff.
Good news:
The command "dotnet restore" exited with 0.
Apparently, dotnet restore
"restores" the dependencies and tools needed for a project. It doesn't actually build anything yet.
So the next logical thing is to add "dotnet build" to the scripts and push again:
language: csharp
mono: none
dotnet: 2.0.0
dist: trusty
script:
- dotnet restore
- dotnet build
I'm expecting this to fail, since it'll probably try to build all projects. Some of which are for .NET Framework 2.0 and will not build with .NET Core.
This second push, however, doesn't seem to trigger a rebuild. So I restart manually. It takes quite a while for it to run. After some time I realize a next build was started after all.
So patience is adviced while testing this.
And finally, as expected, I receive errors in the following style:
The reference assemblies for framework ".NETFramework,Version=v4.5" were not found.
So now I need to only build the FlacLibSharp.Test.Core project.
Build specific project
The easiest solution I see is to first "cd" into that folder:
language: csharp
mono: none
dotnet: 2.0.0
dist: trusty
script:
- cd FlacLibSharp.Test.Core
- dotnet restore
- dotnet build
This push, the build is triggered a lot faster.
And this actually results in a succesful build.
So that is the build process covered. Now the question is: does Travis CI support the dotnet test
command or do you really need XUnit or NUnit, as the documentation says.
Testing with Travis CI
Maybe Travis CI just automatically knows what to do when you run dotnet test
so I try this configuration first:
language: csharp
mono: none
dotnet: 2.0.0
dist: trusty
script:
- cd FlacLibSharp.Test.Core
- dotnet restore
- dotnet build
- dotnet test
I suppose if the tests pass then dotnet test will return 0 and travis will say it is succesful. I don't know why but I was expecting a way to get more details on the tests.
I believe this could be possible if I find a way for dotnet test
to provide some test results. But I did not look into this yet.
What happens if a test fails?
So if a test fails, it would be nice to get some info.
To see what happens, I need to make a test fail on purpose. The build fails, I can find the information on what failed in the logs on Travis CI. Good enough for me.
Failed FlacLibSharp.Test.CreateTests.CreatePadding
Error Message:
Assert.AreEqual failed. Expected:<257>. Actual:<256>.
Stack Trace:
at FlacLibSharp.Test.CreateTests.CreatePadding() in /home/travis/build/AaronLenoir/flaclibsharp/FlacLibSharp.Test.Core/CreateTests.cs:line 36
Clean up
I think I can make the config file simpler by not doing the restore and build first. Assuming "test" would automatically try to do that.
language: csharp
mono: none
dotnet: 2.0.0
dist: trusty
script:
- cd FlacLibSharp.Test.Core
- dotnet test
And yes, everything still builds and tests correctly.
Finally, in my readme.md I add the following to show the build badge:
[![Build Status](https://travis-ci.org/AaronLenoir/flaclibsharp.svg?branch=master)](https://travis-ci.org/AaronLenoir/flaclibsharp)
Pull Requests
When creating a pull request, Travis CI will also try to build the project with the pull request. This will show up on github directly in the pull request.
Conclusion
Travis CI can build .NET Core projects on github without much fiddling.
It will build when:
- A change is pushed to a branch
- Periodically for your main branch
- Any pull request to your main branch (so you know it builds before you accept the pull request).
Now I have this icon on my repo: