This post is about setting up a simple continuous integration environment for ASP.NET 5 with GitHub and Travis-CI. So when ever someone commits to the source, the build will be triggered. Once the build is succeeds, all the unit tests will be executes. If all the tests pass, the build will be successful. Otherwise the build will be treated as failed build.
- Creating source code and unit tests - As first step you need to create a simple ASP.NET 5 application and unit tests. In this post I am using a basic CRUD MVC application, I am using Moq and repository pattern make the solution testable.
And here is the test methods for the same
I have added only two unit tests for create method. I am using almost same code as I mentioned in this post - Creating Unit Tests for ASP.NET MVC 6 Applications.
Now you need to create the project.json file, to compile and test.
I am using K Runtime with beta 3 packages. Now you can restore the packages using kpm restore command. Once everything installed, you can run the application by k web command. You can run the tests by k test command.
- The Travis YML file - This file is equivalent TFS build script or build workflow file. This file helps to configure the build environment. In Travis CI, C# beta support available, so when we specify the language, Travis CI will download the mono framework and compile the source code on top of that. You can find more details about C# support in Travis CI here. You need to keep the .travis.yml file in the root directory. Here is the .travis.yml which can be used for ASP.NET 5 build and executing tests.
The language line instructs the build agent to compile the project in C#. Because of this, build agent will download mono and execute all the steps. In the install phase, I am installing the KVM and downloading the runtime and packages using kvm upgrade, kpm restore commands. Finally, the script block will get execute, it will run the XUnit tests, once completed, Travis CI verifies the exit code of the process and based on exit code it will pass or fail the build. The -parallel none option required, it is a known issue in k test command, otherwise this command will not return exit code. You need to add this file also to the github repository.
- Linking GitHub and Travis CI - Once the above steps completed, now you need to tell travis ci about your github repository. You can do this by signin to Travis-CI. And from the Account page select the repository.
Now we are ready with the CI environment, you can verify it by committing to the github repository. Here is the log output from Travis CI
Travis CI supports build status images. You can include it in the readme.md files. You can the code for status image by clicking on the build status image, right side of the repository name.
You can find the full application, with .travis.yml and unit tests here.
Happy Programming :)