Agilix
Published in

Agilix

.NET Core on Jenkins in Docker series 1/4

References : https://github.com/merken/netCoreBuild

In this series I will try to explain you how to setup your own local continuous integration environment using Jenkins CI and Docker for an ASP.net core application.

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.

In this tutorial, I will use Jenkins as a deployment tool as well, which is not uncommon but there are more suitable products out there, like Octopus Deploy for .Net projects.

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.

Prerequisites

  1. A Windows/Linux/Mac machine with Docker CE
  2. Your favorite shell (Bash, Powershell, …)
  3. Your favorite IDE (I use VS Code)
  4. .Net core 2

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:

.

— build

— netcoreapp

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

dotnet

You should notice that dotnet is installed, we can check the version like so

dotnet --version

Which should give us the version.

2.0.0

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

dotnet run

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.

dotnet

The dotnet command will host your application in a production environment.

dotnet build

This command will build your current project/solution.

dotnet test

With test, we can run a test project and collect the output of the test run.

dotnet publish

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

.

— build

— netcoreapp

— netcoreapp.tests

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.

dotnet test

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store