Merge pull request #195488 from KoviRobi/gitlab-runner-clear-docker-cache

Gitlab runner clear docker cache
This commit is contained in:
Pascal Bach 2022-10-12 07:56:46 +02:00 committed by GitHub
commit 1ca4c178dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 1 deletions

View File

@ -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)

View File

@ -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