From 5457fdf27380a821b2944f1ab1ccee1e20a07675 Mon Sep 17 00:00:00 2001 From: Robert Kovacsics Date: Thu, 29 Sep 2022 11:44:04 +0100 Subject: [PATCH 1/2] gitlab-runner: Install `clear-docker-cache` script --- .../continuous-integration/gitlab-runner/default.nix | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkgs/development/tools/continuous-integration/gitlab-runner/default.nix b/pkgs/development/tools/continuous-integration/gitlab-runner/default.nix index 0c2d18393390..d55ea9ae52ab 100644 --- a/pkgs/development/tools/continuous-integration/gitlab-runner/default.nix +++ b/pkgs/development/tools/continuous-integration/gitlab-runner/default.nix @@ -1,4 +1,4 @@ -{ lib, buildGoModule, fetchFromGitLab, fetchurl }: +{ lib, buildGoModule, fetchFromGitLab, fetchurl, bash }: let version = "15.4.0"; @@ -14,6 +14,9 @@ buildGoModule rec { "-X ${commonPackagePath}.REVISION=v${version}" ]; + # For patchShebangs + buildInputs = [ bash ]; + vendorSha256 = "sha256-S0x1b2ITtqMoqdssoTgnolDC6Tyq3IdkJqxwZ29qCyU="; src = fetchFromGitLab { @@ -45,6 +48,10 @@ buildGoModule rec { rm helpers/docker/auth/auth_test.go ''; + postInstall = '' + install packaging/root/usr/share/gitlab-runner/clear-docker-cache $out/bin + ''; + preCheck = '' # Make the tests pass outside of GitLab CI export CI=0 From c8eae7a5261a0d2ceaf0ad8a8b08050ec5bf40f2 Mon Sep 17 00:00:00 2001 From: Robert Kovacsics Date: Thu, 29 Sep 2022 11:44:40 +0100 Subject: [PATCH 2/2] nixos/gitlab-runner: Add `gitlab-runner.clear-docker-cache` service --- .../continuous-integration/gitlab-runner.nix | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/nixos/modules/services/continuous-integration/gitlab-runner.nix b/nixos/modules/services/continuous-integration/gitlab-runner.nix index fb148e7cffb5..2050e04d55cd 100644 --- a/nixos/modules/services/continuous-integration/gitlab-runner.nix +++ b/nixos/modules/services/continuous-integration/gitlab-runner.nix @@ -453,6 +453,43 @@ in }; }); }; + clear-docker-cache = { + enable = mkOption { + type = types.bool; + default = false; + description = lib.mdDoc '' + Whether to periodically prune gitlab runner's Docker resources. If + enabled, a systemd timer will run {command}`clear-docker-cache` as + specified by the `dates` option. + ''; + }; + + flags = mkOption { + type = types.listOf types.str; + default = [ ]; + example = [ "prune" ]; + description = lib.mdDoc '' + Any additional flags passed to {command}`clear-docker-cache`. + ''; + }; + + dates = mkOption { + default = "weekly"; + type = types.str; + description = lib.mdDoc '' + Specification (in the format described by + {manpage}`systemd.time(7)`) of the time at + which the prune will occur. + ''; + }; + + package = mkOption { + default = config.virtualisation.docker.package; + defaultText = literalExpression "config.virtualisation.docker.package"; + example = literalExpression "pkgs.docker"; + description = lib.mdDoc "Docker package to use for clearing up docker cache."; + }; + }; }; config = mkIf cfg.enable { warnings = (mapAttrsToList @@ -497,6 +534,22 @@ in KillMode = "process"; }; }; + # Enable periodic clear-docker-cache script + systemd.services.gitlab-runner-clear-docker-cache = { + description = "Prune gitlab-runner docker resources"; + restartIfChanged = false; + unitConfig.X-StopOnRemoval = false; + + serviceConfig.Type = "oneshot"; + + path = [ cfg.clear-docker-cache.package pkgs.gawk ]; + + script = '' + ${pkgs.gitlab-runner}/bin/clear-docker-cache ${toString cfg.clear-docker-cache.flags} + ''; + + startAt = optional cfg.clear-docker-cache.enable cfg.clear-docker-cache.dates; + }; # Enable docker if `docker` executor is used in any service virtualisation.docker.enable = mkIf ( any (s: s.executor == "docker") (attrValues cfg.services)