Creating a dotnet core CI environment and build server using JenkinsCI (Part 2)

Tom Smith

In the first part of this 4 part series, we discussed using Jenkins CI to build an automatic build system for dotnet core and .NET 5 software projects.

We outlined why we might want to do this, and how to set up a virtual machine ready to create the build system.

In this part, we'll install Jenkins itself, and the runtimes and things it needs, along with the latest version of dotnet core (3.1 as of the date I write this).

As noted in the previous post, I'll be building this on an Ubuntu Linux 18.04 Install, however much of what I describe does have windows and MacOS equivalents too so shouldn't be too difficult to easily adapt for other platforms.

The first part of the journey

On some Ubuntu installs, you will be asked if you want to install an SSH server by default.  Generally, you're going to want to answer yes to this one.

Most VM consoles make it difficult to copy and paste between applications on the host PC and the VM running in the hypervisor of your choice.

If you didn't get a choice, then one of the first things you'll want to install is an SSH server.

Your first login however, no matter how you do should be to perform a full update of your chosen OS, in our case we can do this with the following command:


sudo apt update

Which should look something like the following:

As you can see from fig 1, we have packages to upgrade.  In my case on the VM I'm using, I already have Jenkins and all the other bits I need installed, so my updates will not only be to update the OS, but also all my installed software.

To go ahead and make sure everything is up to date, type

sudo apt upgrade

And press the enter key to kick it all off. Depending on how many updates you have, the speed of your connection and the amount of work that has to be done, this could take a while

Once the upgrade has finished, you should install the following 2 applications to help you, you can do this by typing and entering the following 2 commands at your command line to install them.

sudo apt install openssh-server

sudo apt install mc

The first install the industry standard SSH server, and opens its port 22 on the server to allow you to log in remotely, from this point on you can use tools such as PuttySSH on a windows machine to log in, and more importantly, be now able to cut and paste much more easily.

The second command installs "Midnight Commander" a terminal based combined file manager and plain text editor.  If you're old enough to remember "Norton Commander" and the original MS-Dos version of "Edit" then you'll be right at home using MC.  Later on in this series when we need to start editing configuration files, MC will make it an effortless task, and works well over an SSH link too.

If you also want to make it easy to transfer files via FTP to/from your build server, you may also want to install open ssh ftp server using:

sudo apt install openssh-sftp-server

This will install the secure FTP portion of OpenSSH which will then allow you to use windows tools such as "FileZilla" to send/receive files to/from your server file system.

If everything has installed ok, you should be able to type

mc

Followed by return, and be greeted with a display similar to the following:

Now the fun begins…

Because Jenkins CI is a java based product, the first thing we need on our VM is a java runtime.  You can if you must, use the oracle version of java on Ubuntu linux, but due to the whole mess with licensing and many other things, it's much more advisable to use and install "OpenJDK"

As with everything else on Ubuntu/Debian, installing the package is as easy as typing

sudo apt install default-jdk

Then pressing return and following the prompts given by the installer.  Once the installer is finished, you can test your install by typing

java –version

then observing the output.

If your response is something similar to the following (Version numbers may be different), then you should be good to continue.



Adding the digital butler

The Jenkins team, maintain their own repository.  As a result, just performing a normal install of "Jenkins" will either tell you the application doesn't exist, or will get you a much older version than the current one supported directly by the Jenkins team.

In order to get the most up to date version, you need to add and then install from the "Jenkins.io" repository rather than your OS default.

To make this work, you first need to add the signing key by doing typing the following at your command line

wget -q -O - http://pkg.jenkins.io/debian-stable/Jenkins.io.key | sudo apt-key add –

if you've entered that correctly, you should be greeted with a simple "OK"

Once you've added the signing key, you then need to add the web address of the Jenkins software repository, you can do this with the following command

sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'

Once the software repository is added, perform a "sudo apt update" followed by a "sudo apt upgrade" as you did at the beginning of this article.

You might at this stage get an error telling you "gnupg" doesn't exist, if you do, then simply install gnupg by entering the following

sudo apt install gnupg

Then go back to the "wget" command above and try again.

With everything set up and in place, you can finally enter

sudo apt install jenkins

At your command line, to install the main Jenkins build system.

Jenkins should start automatically once the install finishes.  To check that it has, enter the following at your command line, and observe the output

sudo netstat –nlp

The output should look similar to fig 6 below.

As can be seen in fig 6, there is a Java based process listening on port 8080, this is where Jenkins listens by default.

You can if you wish, change the Jenkins configuration once up and running to listen to any port you would like the service to listen on.

Dotnet to the core

Dotnet core, like Jenkins, is held in its own repository, and so you'll need to add Microsoft's own packages, rather than using a simple apt install command.

Like Jenkins, it's a bit long winded, but it really doesn't take as long as most people think it does.

The thing you MUST be careful of when installing on Linux, are versions.  All the distros, and all the different versions, do have specific set's of commands to follow.

If you browse to : https://dotnet.microsoft.com/download/dotnet-core you will find the official Microsoft instructions for installing dotnet core on your chosen system.

For Linux, you need the "Package Manager Instructions" usually.  You can if you wish install the binaries, directly, and by hand, but that's really NOT advised.  If installed with the package manager, then updating it is as simple as issuing "apt update"/"apt upgrade" and is a lot less trouble to look after.

Since my set-up already has dotnet core installed I can't take you through and show you the process without building a new Virtual Machine, the steps though are very simple.

Unlike Jenkins, you don't need to add a repository key for the Microsoft stuff, just a package source and a few updates.

Enter the following commands at your CLI to install the base stuff you need. (NOTE:  this is correct for the version and the date I installed – sept 2020, if you are using a different version, then you'll need to consult the install instructions for the version you wish to use.)

wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb

NOTE 2: In the command above you can see "Ubuntu 18.04" you WILL need to adjust that to match the version of Linux OS you are using.

sudo dpkg -i packages-microsoft-prod.deb

sudo apt-get update

sudo apt-get install -y apt-transport-https

sudo apt-get update

sudo apt-get install -y dotnet-sdk-3.1

Once you have dot net core installed, you should be able to type

dotnet --info

And get an output similar to the following image

Jenkins and dotnet core are the two main packages needed to create the build server, but you need 2 more tool sets so that Jenkins is able to grab your project from source control, and serve your project for testing using your Linux VM.

First up, you need the "git" command line tools, these work with a local git install AND services like github.  If you're using something different such as SVN or Mercurial, then you'll need to appropriate tools to allow Jenkins to access those repositories instead of the git command line tools.

As you've most likely guessed by now, installing git is easy.

sudo apt install git

The final part of the puzzle is a web server.

Microsoft's recommendation for all dotnet core applications is to run them behind a front end reverse proxy.

Any of the main stream web servers (Nginx, Apache, IIS, LightHttpd…) will work for this purpose.  In our case I'll be showing you how to set up Nginx, the instructions for setting up others however are easily found on the internet, essentially all you need to do is configure the web server for normal operation, and then proxy either your "root" path, leaf node access or a virtual host name, so that it forwards to an ip address and port that your dotnet service is configured to listen on.  We'll go into this in more detail in one of the remaining parts of this series.

For now simply install Nginx using the following command

sudo apt install nginx

Again you can use "netstat –nlp" to make sure the server is listening on port 80, but for now that's all you need to do to set up your build server ready to perform a build.

In part 3 we'll go through setting up Jenkins to actually perform a build of your software, and how to get Jenkins to do a few interesting tricks such as check your service is running after it's deployed the build.