How to create Dotnet Core TeamCity Build Agent on Ubuntu

Setting up a cross platform DevOps environment for ASP.net Core applications, we will learn how to configure a headless ubuntu server to act as TeamCity BuildAgent to use Cake to build ASP.net Core applications.

Important

This guide assumes you have configured a ubuntu headless server with .net core CLI.

Install .net core on ubuntu headless server

Install the prequisites

You will also need install Mono-Complete

Mono is a platform for running and developing applications based on the ecma/iso standards. mono is an open source effort led by xamarin. mono provides a complete clr (common language runtime) including compiler and runtime, which can produce and execute cil (common intermediate language) bytecode (aka assemblies), and a class library.

Install Mono on Ubuntu

Although not strictly necessary to build .net core applications we will also install Mono on the server to facilitate the building of other .net framework applications.

You may need to first add references to the Mono Repository to ensure you get the latest versions.

Shell

Install mono-complete via the terminal

Shell

Test Cake Build on the server

In order to test that we have the server configured correctly We can now try out Cake following instructions from CakeBuild.net – Getting Started example.

Install the Bootstrapper

Shell

Create a Cake script

We will use nano to create the file

Shell

Copy & Paste the sample code

C#

Information

If you want more information about how to write a cake build script.
Check out How to write a Cake Build script for ASP.net core project

Change permission on build.sh

Shell

Execute the Build

Shell

We should see the following result indicating that we were able to successfully build a project using Cake on the server.

Shell

Configure as Build Agent

We are going to be using this server as an external build agent for use with TeamCity. In my scenario I have TeamCity installed on a seperate ubuntu server.

We need to install a few more pre-requisites to enable this.

Install Unzip

Shell

Install Java JDK & JRE

TeamCity is a Java based application therefore all build agents etc have been developed using Java.

Install the default JDK in the ubuntu repository

Shell

Check your Java Version

Shell

The version information returned should be similar too

Shell

Configure JAVA_HOME environment variable
To find out where Java was installed

Shell

you should see a response similar too

Shell

Copy the path from your preferred installation and then open /etc/environment using nano or another text editor.

Shell

At the end of the file on a new line paste the following or whatever your java installation path is

Shell

Refresh your terminal windows source, to ensure it picks up you change in the current session

Plain Text

We can now test the environment variable has been set

Shell

Create TeamCity User on Server

We are going to create a user on our server which will we use to run the build agent. We will need to create a user with Home Directory, because this is where we are going to install the team city build agent too.

Shell

We also want to create a group for our build agents on the server, we could potentially have several builders running on the server. We create a group to add them all in.

Shell

You may also want to give your netBuild account ssh access. So to do this simply edit your ssh config to enable ssh password authentication. Typically we have this set up to only enable ssh keys, but because this user will not have ssh keys defined, we only really want to give it password access and only from computers on our network.

So to do this we will edit our ssh config

Shell

Then simply change the following.

Shell

Configure using Agent Push from TeamCity

We can now configure this server as BuildAgent from within TeamCity utilizing the Agent Push functionality. Then simply click the install agent, then select Use Custom Setting and select Unix. Then complete the rest of the form with your details.

Set Agent to Auto Start

There are several methods you can use in Linux to set services to auto start when the server reboots.

Using init.d

Shell

Paste the following code, change the name of the USER variable to whatever username required to run the the script. In my case I have created an agentuser account. You may need to change this value to suit your environment.

Shell

Setting up and Running Additional Build Agents

Provide the sufficient permissions for the file to execute

Shell

Add it to the start up process

Shell

Reference Sites

Latest posts by Gary Woodfine (see all)