diff --git a/main.tf b/main.tf index db1d1cc..68810e6 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" @@ -71,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" } @@ -93,21 +121,22 @@ 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://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." 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 \ --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} \ @@ -119,12 +148,15 @@ 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 \ +sudo gitlab-runner register -n \ + --description "${local.ci_runner_gitlab_name_final}" \ --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} \ @@ -132,13 +164,17 @@ 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" \ --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} \ 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 = ""