Why you should automate your development environment setup

Photo by Mike Hindle on Unsplash

Why you should automate your development environment setup

Setting up a development environment for a new project is not one of the most popular tasks. As a new developer in the project, you don't know the environment and have to work through pages and pages of documentation until your own development environment is up and running. Experience shows that such documentation is often not up to date. This leads to a lot of trial and error and asking questions. In the end, it often takes several hours or sometimes more than a day before you can start developing.

This can be very frustrating and we even haven't talked about repeating this when you are working on a micro-service-based project or want to flush your dev env setup.

I'm working on lots of self-contained systems and micro-service-based projects. And it can happen that I need to switch those projects several times a week. So it is crucial for me to avoid such wasted time. I automate the dev env setup as much as possible and keep improving it. With this approach, you can get up and running within minutes!

Use docker to install your infrastructure

First of all, we can avoid installing a huge list of software infrastructure, such as SQL Server, Message Broker, SMTP Client, Identity Provider, etc. All this can be set up by using docker.

Using docker has several advantages:

  • You don't pollute your operating systems with these applications. Even when you uninstall them, some relics are left in your system.

  • Everyone on the team has exactly the same versions of the infrastructure.

  • Everyone in the team automatically gets additional infrastructure, without installing them manually.

  • You can get and run this software within minutes, it actually only needs one command to start them all.

Using docker reduces the list of software to install to the following and this is something that will be the same for all of your projects, so you do it only once:

  • GIT (or your preferred source version control system)

  • Docker Desktop

  • SDK's (Usually .Net Core and Node)

  • IDE (In my case Visual Studio, Rider, or whatever)

And then I prepare a docker-compose file, containing all required infrastructure, in the root of the SVC repository:

version: "3.8"

services:
  mssql:
    container_name: mssql
    image: mcr.microsoft.com/azure-sql-edge:1.0.6
    ports:
      - 1433:1433
    environment:
      - ACCEPT_EULA=Y
      - SA_PASSWORD=<any password>
      - TZ=Europe/Zurich
    volumes:
      - local-dev-sql:/var/opt/mssql

  azurite:
    container_name: azurite
    image: mcr.microsoft.com/azure-storage/azurite:1.4.2-linux-amd64
    ports:
      - 10000:10000
      - 10001:10001
      - 10002:10002
    volumes:
      - local-dev-azurite:/data mcr.microsoft.com/azure-storage/azurite

  smtp-mock:
    container_name: smtp
    image: rnwood/smtp4dev:v3.1
    ports:
      - "6200:80"
      - "2525:25"

volumes:
  local-dev-sql:
  local-dev-azurite:

And it will require only one command to start up everything:

docker-compose -f docker-compose.dev-env -p my-app up

You can flush your infrastructure with only one command as well:

docker-compose -f docker-compose.dev-env -p my-app down -v

Script application-specific configurations and setups with Nuke

The first part of your dev env setup is done with that. Usually, there needs to be additional things done. Things like local configurations, database setup, etc.

A few years ago, I stumbled over Nuke. A build scripting engine running plain .Net code and is fully integrated into your .Net development IDE. Everyone in our teams is used to writing C# code, so everyone is comfortable in using this tool. And this is the reason why I love this great tool so much. Kudos to Matthias Koch for it!

As you might guess, I'm using Nuke scripts to set up everything else which usually has to be done manually. I'm not going to write a tutorial for nuke at this place, but just as much as need to get you a picture of it.

A nuke script can be initialized with the following commands:

dotnet tool install Nuke.GlobalTool --global
nuke :setup

This will guide you through a wizard and set up a Nuke project including some startup scripts for you. Then you can start to implement your automation script with it.

class Build : NukeBuild
{
    public static int Main() => Execute<SetupDevEnv>();

    Target SetupDevEnv => _ => _
            .DependsOn(CreateOrUpgradeDatabase)
        .Executes(() =>
        {
             // This one usually has no implementation, but calls other targets which are doing their jobs
        });

    Target CreateOrUpgradeDatabase => _ => _
        .Executes(() =>
        {
            // Create or upgrade databasae implementation
        });
}

And afterward you can execute the script with the following command:

nuke.sh --target SetupDevEnv

And with that your dev env is ready and you can start hacking.

Final dev env setup steps

With all this in place, your dev env setup will be reduced to the following steps.

Dev Env Setup Instruction

Required software:

  • GIT

  • Docker Desktop

  • DotNet Core SDK

  • NodeJS

  • Rider or Visual Studio

Clone the repo:

git clone <repor-url> my-app

Startup infrastructure:

docker-compose -f docker-compose.dev-env -p my-app up

Setup dev env:

nuke.sh --target SetupDevEnv

Great, that's it! You can open your IDE and hit F5.