.NET Core on Jenkins in Docker series 1/4
References : https://github.com/merken/netCoreBuild
First off, what does Continuous Integration mean ?
Continuous integration is the practice of integrating changes to the source code (or system) in a large organisation by
- Committing code frequently to a common repository
- Having an shared independent build process
- Running automated tests
… to have a potential releasable product as a result.
There are a lot of CI tools out there, but I consider Jenkins to be one of the more versatile ones and would recommend it even for .Net development.
Though it relies on a lot of configuration, you can script your entire build process in a pipeline via a Jenkinsfile.
We will deploy an ASP.net core application. ASP.net core is the open source, x-platform web development stack of Microsoft’s .Net framework and relies on .Net core. For this tutorial, we will be using net core 2.
We will deploy our application using Docker as a docker container. Docker is a software platform that is build upon Linux Containers (LXC). This technology provides OS-level isolation in form of containers instead of Virtual Machines. Thus making it more lightweight. Docker allows for container composition based on a Dockerfile. To deploy our application to Docker, we will need a Dockerfile as well.
Let’s start with a project
.Net core comes with a lot of default templates, we will use the ASP.net MVC template for our project, but you can use any of the other web templates for this demo if you like.
Create a working directory for our project, this is my setup:
The project will be created in the netcoreapp directory, the build directory will contain the files required for creating our build environment. Let us start by navigating into netcoreapp directory and creating our mvc app.
In the shell, type
You should notice that dotnet is installed, we can check the version like so
Which should give us the version.
Now to create the project, type the following command
dotnet new mvc
This will create our mvc project in the current working directory, the project’s name will be that of our working directory by default.
Let the framework install the first-time dependencies and you should be good to go.
Once completed, you can run the application using the run command
Your application should now be running on port 5000
Hosting environment: Production
Content root path: C:\Users\merke\Documents\projects\netcoreapp2\netcoreapp
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
Stop the run (CTRL+C).
We’ve seen the dotnet run command, but there are more CLI commands you can use to build, run, publish and host your application.
The dotnet command will host your application in a production environment.
This command will build your current project/solution.
With test, we can run a test project and collect the output of the test run.
This command will publish our application for deployment to a staging or production environment.
Let’s say our app is now feature-complete, we still need to write tests for our functionality. Though I will not go into too much detail, I just want to simulate that testing can be done using the dotnet cli.
So let’s create a test project, the folder structure should look like this
In the netcoreapp.tests directory execute the dotnet new command with the xunit template.
dotnet new xunit
This will scaffold a default XUnit test project with an empty test class UnitTest1.cs. Alter the class to resemble something like this:
Nothing too fancy, now we have 1 unit test in our test project so that we can run the dotnet test command within this directory.
The output should be something like the following.
Microsoft (R) Test Execution Command Line Tool Version 15.3.0-preview-20170628–02
Copyright © Microsoft Corporation. All rights reserved.
Starting test execution, please wait…
[xUnit.net 00:00:00.9630476] Discovering: netcoreapp.tests
[xUnit.net 00:00:01.0297156] Discovered: netcoreapp.tests
[xUnit.net 00:00:01.0630137] Starting: netcoreapp.tests
[xUnit.net 00:00:01.1885844] Finished: netcoreapp.tests
Total tests: 1. Passed: 1. Failed: 0. Skipped: 0.
Nice, now we have a ‘production-ready’ application with ‘tests’. In the next section, we will show you how to deploy it using a docker container.
In this introduction tutorial we got started with dotnet core and aspnet core. We’ve scaffolded an application and write tests, please drop by next time to see how we will continue to setup our CI-CD environment using Jenkins and Docker.
If you do not wish to wait, you may look at the complete solution on github.