From b86868762b5b1751687952f7c15454263a2aac1f Mon Sep 17 00:00:00 2001 From: Guido Zockoll Date: Sat, 8 Aug 2020 06:35:48 +0000 Subject: [PATCH 1/6] Add Gitlab runner cache using cloud storage --- main.tf | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/main.tf b/main.tf index db1d1cc..76a4e98 100644 --- a/main.tf +++ b/main.tf @@ -61,6 +61,34 @@ resource "google_service_account_iam_member" "ci_worker_ci_runner" { member = "serviceAccount:${google_service_account.ci_runner.email}" } +# Cache for the Gitlab CI runner +resource "google_storage_bucket" "cache" { + name = join("-", [local.ci_runner_gitlab_name_final, "cache"]) + location = "EU" + force_destroy = true + + lifecycle_rule { + condition { + age = "30" + } + action { + type = "Delete" + } + } +} +resource "google_service_account" "cache-user" { + account_id = join("-", [local.ci_runner_gitlab_name_final, "sa"]) +} +resource "google_service_account_key" "cache-user" { + service_account_id = google_service_account.cache-user.name + public_key_type = "TYPE_X509_PEM_FILE" +} +resource "google_project_iam_member" "project" { + project = var.gcp_project + role = "roles/storage.objectAdmin" + member = format("serviceAccount:%s", google_service_account.cache-user.email) +} + resource "google_compute_instance" "ci_runner" { project = var.gcp_project name = "${var.gcp_resource_prefix}-runner" @@ -119,12 +147,14 @@ docker-machine rm -y ${var.gcp_resource_prefix}-test-machine echo "Setting GitLab concurrency" sed -i "s/concurrent = .*/concurrent = ${var.ci_concurrency}/" /etc/gitlab-runner/config.toml +echo ${google_service_account_key.cache-user.private_key} | base64 -d > /etc/gitlab-runner/key.json + echo "Registering GitLab CI runner with GitLab instance." sudo gitlab-runner register -n \ --url ${var.gitlab_url} \ --token ${var.ci_token} \ --executor "docker+machine" \ - --docker-image "alpine:latest" \ + --docker-image "alpine:latest" \ --tag-list "${var.ci_runner_gitlab_tags}" \ --machine-machine-driver google \ --docker-privileged=${var.docker_privileged} \ @@ -139,6 +169,10 @@ sudo gitlab-runner register -n \ --machine-machine-options "google-disk-type=pd-ssd" \ --machine-machine-options "google-disk-size=${var.ci_worker_disk_size}" \ --machine-machine-options "google-tags=${var.ci_worker_instance_tags}" \ + --cache-type gcs \ + --cache-shared \ + --cache-gcs-bucket-name ${google_storage_bucket.cache.name} \ + --cache-gcs-credentials-file /etc/gitlab-runner/key.json \ --machine-machine-options "google-use-internal-ip" \ --machine-machine-options "google-network=${var.ci_runner_network}" \ %{if var.ci_runner_subnetwork != ""}--machine-machine-options "google-subnetwork=${var.ci_runner_subnetwork}"%{endif} \ From 16a1e56ff4130809ba827f7b14d19bccda11a27e Mon Sep 17 00:00:00 2001 From: Toni Robles Date: Thu, 31 Aug 2023 09:54:39 +0200 Subject: [PATCH 2/6] Fix unused gitlab name --- main.tf | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.tf b/main.tf index 76a4e98..4f19a65 100644 --- a/main.tf +++ b/main.tf @@ -150,7 +150,8 @@ sed -i "s/concurrent = .*/concurrent = ${var.ci_concurrency}/" /etc/gitlab-runne echo ${google_service_account_key.cache-user.private_key} | base64 -d > /etc/gitlab-runner/key.json echo "Registering GitLab CI runner with GitLab instance." -sudo gitlab-runner register -n \ +sudo gitlab-runner register -n \ + --description "${local.ci_runner_gitlab_name_final}" \ --url ${var.gitlab_url} \ --token ${var.ci_token} \ --executor "docker+machine" \ From 428f10537c6057fbd94dd73a8fb367a182b085fc Mon Sep 17 00:00:00 2001 From: Toni Robles Date: Thu, 31 Aug 2023 11:16:30 +0200 Subject: [PATCH 3/6] Fix docker-machine verification (f1-micro not present in some zones) --- main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.tf b/main.tf index 4f19a65..02c7c0a 100644 --- a/main.tf +++ b/main.tf @@ -129,7 +129,7 @@ sudo install /tmp/docker-machine /usr/local/bin/docker-machine echo "Verifying docker-machine and generating SSH keys ahead of time." docker-machine create --driver google \ --google-project ${var.gcp_project} \ - --google-machine-type f1-micro \ + --google-machine-type ${var.ci_worker_instance_type} \ --google-zone ${var.gcp_zone} \ --google-service-account ${google_service_account.ci_worker.email} \ --google-scopes https://www.googleapis.com/auth/cloud-platform \ From c4c8dc71215f7590f9182ae5c1495bc66a86e24a Mon Sep 17 00:00:00 2001 From: Toni Robles Date: Tue, 12 Sep 2023 15:44:50 +0200 Subject: [PATCH 4/6] Use maintained gitlab version of docker-machine --- main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.tf b/main.tf index 02c7c0a..97be963 100644 --- a/main.tf +++ b/main.tf @@ -123,7 +123,7 @@ curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/sc sudo yum install -y gitlab-runner echo "Installing docker machine." -curl -L https://github.com/docker/machine/releases/download/v0.16.2/docker-machine-Linux-x86_64 -o /tmp/docker-machine +curl -L https://gitlab-docker-machine-downloads.s3.amazonaws.com/v0.16.2-gitlab.22/docker-machine-Linux-x86_64 -o /tmp/docker-machine sudo install /tmp/docker-machine /usr/local/bin/docker-machine echo "Verifying docker-machine and generating SSH keys ahead of time." From 04e8eca6f6edeabaa3f3ff1810d2d9c58a54a7ac Mon Sep 17 00:00:00 2001 From: Toni Robles Date: Fri, 5 Apr 2024 12:26:36 +0200 Subject: [PATCH 5/6] Use variables for runner and worker machine image --- main.tf | 6 +++--- variables.tf | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/main.tf b/main.tf index 97be963..b727e7e 100644 --- a/main.tf +++ b/main.tf @@ -99,7 +99,7 @@ resource "google_compute_instance" "ci_runner" { boot_disk { initialize_params { - image = "centos-cloud/centos-7" + image = "${var.ci_runner_machine_image}" size = var.ci_runner_disk_size type = "pd-standard" } @@ -135,7 +135,7 @@ docker-machine create --driver google \ --google-scopes https://www.googleapis.com/auth/cloud-platform \ --google-disk-type pd-ssd \ --google-disk-size ${var.ci_worker_disk_size} \ - --google-machine-image ubuntu-os-cloud/global/images/ubuntu-2004-focal-v20220419 \ + --google-machine-image ${var.ci_worker_machine_image} \ --google-tags ${var.ci_worker_instance_tags} \ --google-use-internal-ip \ --google-network ${var.ci_runner_network} \ @@ -163,7 +163,7 @@ sudo gitlab-runner register -n \ --machine-machine-name "${var.gcp_resource_prefix}-worker-%s" \ --machine-machine-options "google-project=${var.gcp_project}" \ --machine-machine-options "google-machine-type=${var.ci_worker_instance_type}" \ - --machine-machine-options "google-machine-image=ubuntu-os-cloud/global/images/ubuntu-2004-focal-v20220419" \ + --machine-machine-options "google-machine-image=${var.ci_worker_machine_image}" \ --machine-machine-options "google-zone=${var.gcp_zone}" \ --machine-machine-options "google-service-account=${google_service_account.ci_worker.email}" \ --machine-machine-options "google-scopes=https://www.googleapis.com/auth/cloud-platform" \ diff --git a/variables.tf b/variables.tf index 6d24408..9883d72 100644 --- a/variables.tf +++ b/variables.tf @@ -15,22 +15,27 @@ */ # Global options + variable "ci_token" { type = string description = "The runner registration token obtained from GitLab." } + variable "gcp_project" { type = string description = "The GCP project to deploy the runner into." } + variable "gcp_zone" { type = string description = "The GCP zone to deploy the runner into." } + variable "gitlab_url" { type = string description = "The URL of the GitLab server hosting the projects to be built." } + variable "gcp_resource_prefix" { type = string default = "gitlab-ci" @@ -38,6 +43,7 @@ variable "gcp_resource_prefix" { } # Runner options + variable "ci_runner_network" { type = string default = "default" @@ -55,11 +61,13 @@ variable "ci_runner_disk_size" { default = "20" description = "The size of the persistent disk in GB." } + variable "ci_runner_gitlab_name" { type = string default = "" description = "Register the runner in GitLab using this name. If empty the value \"gcp-$${var.gcp_project}\" will be used." } + variable "ci_runner_gitlab_tags" { type = string default = "" @@ -75,32 +83,50 @@ themselves run on separate worker instances. EOF } +variable "ci_runner_machine_image" { + type = string + default = "rocky-linux-cloud/rocky-linux-9-v20240313" + description = "Machine image used for the runner instance" +} + # Worker options + variable "ci_concurrency" { type = number default = 1 description = "The maximum number of worker instances to create." } + variable "ci_worker_disk_size" { type = string default = "10" description = "The size of the persistent disk in GB." } + variable "ci_worker_idle_time" { type = number default = 300 description = "The maximum idle time for workers before they are shutdown." } + variable "ci_worker_instance_tags" { type = string default = "gitlab-ci-worker" description = "The GCP instance networking tags to apply." } + variable "ci_worker_instance_type" { type = string default = "n1-standard-1" description = "The GCP instance type. This can be adjusted to meet the demands of builds jobs." } + +variable "ci_worker_machine_image" { + type = string + default = "ubuntu-os-cloud/global/images/ubuntu-2204-jammy-v20240319" + description = "Machine image used for the worker instance" +} + variable "docker_privileged" { type = string default = "false" @@ -108,6 +134,7 @@ variable "docker_privileged" { } # Pre/post hook scripts + variable "pre_clone_script" { type = string default = "" From f29d315fa7f3f5a3fc027c7fb9b0ff8b9bde8af7 Mon Sep 17 00:00:00 2001 From: Ivan Moreno Date: Wed, 12 Nov 2025 12:26:35 +0100 Subject: [PATCH 6/6] Fix openssl mismatch --- main.tf | 1 + 1 file changed, 1 insertion(+) diff --git a/main.tf b/main.tf index b727e7e..68810e6 100644 --- a/main.tf +++ b/main.tf @@ -121,6 +121,7 @@ set -e echo "Installing GitLab CI Runner" curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash sudo yum install -y gitlab-runner +sudo dnf install -y openssh-server openssh echo "Installing docker machine." curl -L https://gitlab-docker-machine-downloads.s3.amazonaws.com/v0.16.2-gitlab.22/docker-machine-Linux-x86_64 -o /tmp/docker-machine