If you are using Gitlab as a code repository, having an automated build/test/deployment process is becoming more and more common (and more and more necessary in order to scale as your team/project grows and accommodate a faster turn-around for deployments). I definitely learned a lot from my first time setting up my CI process, so in this post I’ll show you exactly what steps to follow to install a Docker Gitlab Runner on Windows.
If you’re looking to setup Gitlab CI from scratch, check out the official Gitlab docs
Once you’ve got a repo setup to run continuous integration, you’ll need to create a runner in order to process and run the continuous integration jobs. Gitlab provides multiple executors that you can choose from, but in this tutorial I’ll be using the Docker executor.
Gitlab Docker CI Executor
Gitlab has a Docker image that comes with the gitlab-runner pre-installed, so we’ll be using that to run our CI jobs. Once we spin up a container from this image, it will listen for jobs and spin up a new container to process each job that it receives.
I prefer using the Gitlab Docker runner over the normal shell runner since Docker containers ensures that all jobs are in isolated environments, and I can provide my own Dockerfile if I have jobs that require special setup steps or specific dependencies. The last thing I want is to have a shared environment that is shared between all my builds.
Create a Gitlab Runner with Docker
- Install docker desktop
- Download the installer
- Install Docker then open CMD and type
docker versionto make sure it is working correctly
- Since Docker is using virtual-machine technology, you’ll need to enable Hyper-V if not already enabled on the host machine.
- The easiest way to use PowerShell. Open a PowerShell terminal as Administrator
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
- Restart the machine
- You’ll need to pick a directory to use as the “home” directory of your Gitlab runner. This will store config files or anything else that the runner needs to persist
- I like to use C:\gitlab-runner
- Create a config folder inside your runner “home” (e.g. C:\gitlab-runner\config)
- Next we’ll create a Docker container for the runner, which will pick up the CI jobs from Gitlab repos and then spin up other Docker containers for each individual CI job that it processes.
- Run the following command to create a new Docker container for the gitlab runner
docker run -d --name gitlab-runner --restart always -v C:\gitlab-runner\config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest
-dtells the container to run in the background (this way we can keep using the terminal)
--name gitlab-runner– Specifies the name of the container we’re creating (in this case we’re calling it gitlab-runner, but feel free to use any name you want)
--restart always– Always spin up the container when Docker starts
-v C:\gitlab-runner\config:/etc/gitlab-runner– Here we are mounting a volume between the host machine and the container. This shared folder will be where the container stores its config files so that they persist even after the container shuts down. If you used a different container home, replace
C:\gitlab-runner\with whatever folder you used.
-v /var/run/docker.sock:/var/run/docker.sock– allows the runner to spin up Docker containers as sibling containers instead of child containers (Docker-in-Docker approach)
gitlab/gitlab-runner– the name of the Docker image we’re going to download and then spin up
Now that the container with the gitlab runner is running, we need to register the runner with Gitlab
Before we register, you’ll need a registration token from your Gitlab repo. From your repo, go to Settings > CI / CD. Find the Runners section and expand it to reveal the registration token.
docker run --rm -t -i -v C:\gitlab-runner\config:/etc/gitlab-runner gitlab/gitlab-runner register --non-interactive --executor "docker" --docker-image alpine:latest --url "https://gitlab.com/" --registration-token "REGISTRATION_TOKEN" --description "docker-runner" --run-untagged="true" --locked="false"
Again, if you used a different “home” folder other than C:\gitlab-runner, replace that path with the folder you created earlier.
Make sure to replace “REGISTRATION_TOKEN” with the registration token you grabbed earlier. If you’re running a private/enterprise version of Gitlab, replace the “https://gitlab.com” url with your own Gitlab URL.
I set this runner to default to the “alpine:latest” docker image if no image is specified in the project’s .gitlab-ci.yml. Feel free to default to a different image if it suits your needs.
Unless you only want to run a single build at a time, you’ll want to enable concurrency for your gitlab runner.
- The first line should read
concurrent: 1— change this to the number of builds you’d like to be able to run in parallel
docker logs gitlab-runner – view logs
Currently (using Docker 18.09.2 and Gitlab Runner 11.10.1), there is an issue where if Docker restarts, when the gitlab-runner container spins back up automatically it will not properly re-attach the config volume.
docker restart gitlab-runner and it will restart with the volume properly attached.