GitLab CI Runners on Google Cloud - The Easy Way
2 minute read.
After spending quite some time optimizing docker images and build times on a semi-large C++ project, the 1 vCPU + 2.75 Gib RAM runners still took 7+ minutes to build. Times 3-5 stages–that’s quite inhibitive to productivity.
Final resort: Upgrade the hardware.
There are a lot of tutorials on how to set up GitLab CI runners on various cloud infrastructure providers, but I have one big problem:
I am a cloud noob and I just want my C++ builds to go faster on GitLab CI!
Luckily there’s a something called “Infrastructure as Code” (IaC), which allows sharing the amazing work other smart an skilled people have done previously rather than struggling through an outdated tutorial/step-by-step guide, just to figure out you need to do it all again, since you messed something up in the third step.
Terraform to the Rescue
Terraform is a framework for IaC. It allows defining the specification of your cloud infrastructure in text to the have it applied to some infrastructure service, in this case Google Cloud Platform (GCP).
I found terraform-google-gitlab-runner.
Set up
Find the project’s instructions here.
It’s fairly simple and the commands a straight forward, you can easily read it and find configuration for the parts you need to tailor to your need.
- Notes:
- Install Terraform and Google Cloud CLI, login is straight forward through Google
- Make sure to set the projects full name your-project-123456, otherwise it will error out with 403 unauthorized
- Bucket names are unique across the entire GCP universe, you have to configure a different one in examples/standard/main.tf
- Make sure that the zone you choose supports the worker instance type (some machine types are only supported in some regions)
- To debug the runner, you can SSH via the GCP web console (no auth required, it just works). sudo gitlab-runner stop && sudo gitlab-runner run will give you the full output while you start a job in GitLab.
Result
With that, I was able to reduce the build time by 60%, expecting around ~50$/mo for about 10-20 runs per day of a pipeline with 20 x 7 min (now 3 min) jobs. (Might update this once I have the exact number.)