2016-09-17 12:08:18 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.gitlab-runner;
|
2017-08-15 13:28:25 +01:00
|
|
|
configFile =
|
|
|
|
if (cfg.configFile == null) then
|
|
|
|
(pkgs.runCommand "config.toml" {
|
|
|
|
buildInputs = [ pkgs.remarshal ];
|
|
|
|
} ''
|
|
|
|
remarshal -if json -of toml \
|
|
|
|
< ${pkgs.writeText "config.json" (builtins.toJSON cfg.configOptions)} \
|
|
|
|
> $out
|
|
|
|
'')
|
|
|
|
else
|
|
|
|
cfg.configFile;
|
2017-05-11 21:29:09 +01:00
|
|
|
hasDocker = config.virtualisation.docker.enable;
|
2016-09-17 12:08:18 +01:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.gitlab-runner = {
|
|
|
|
enable = mkEnableOption "Gitlab Runner";
|
|
|
|
|
2017-08-15 13:28:25 +01:00
|
|
|
configFile = mkOption {
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Configuration file for gitlab-runner.
|
|
|
|
Use this option in favor of configOptions to avoid placing CI tokens in the nix store.
|
|
|
|
|
|
|
|
<option>configFile</option> takes precedence over <option>configOptions</option>.
|
|
|
|
|
|
|
|
Warning: Not using <option>configFile</option> will potentially result in secrets
|
|
|
|
leaking into the WORLD-READABLE nix store.
|
|
|
|
'';
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
};
|
|
|
|
|
|
|
|
configOptions = mkOption {
|
|
|
|
description = ''
|
|
|
|
Configuration for gitlab-runner
|
|
|
|
<option>configFile</option> will take precedence over this option.
|
|
|
|
|
|
|
|
Warning: all Configuration, especially CI token, will be stored in a
|
|
|
|
WORLD-READABLE file in the Nix Store.
|
|
|
|
|
|
|
|
If you want to protect your CI token use <option>configFile</option> instead.
|
|
|
|
'';
|
|
|
|
type = types.attrs;
|
|
|
|
example = {
|
|
|
|
concurrent = 2;
|
|
|
|
runners = [{
|
|
|
|
name = "docker-nix-1.11";
|
|
|
|
url = "https://CI/";
|
|
|
|
token = "TOKEN";
|
|
|
|
executor = "docker";
|
|
|
|
builds_dir = "";
|
|
|
|
docker = {
|
|
|
|
host = "";
|
|
|
|
image = "nixos/nix:1.11";
|
|
|
|
privileged = true;
|
|
|
|
disable_cache = true;
|
|
|
|
cache_dir = "";
|
|
|
|
};
|
|
|
|
}];
|
|
|
|
};
|
2016-09-17 12:08:18 +01:00
|
|
|
};
|
|
|
|
|
2017-07-11 15:38:46 +01:00
|
|
|
gracefulTermination = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
|
|
|
Finish all remaining jobs before stopping, restarting or reconfiguring.
|
|
|
|
If not set gitlab-runner will stop immediatly without waiting for jobs to finish,
|
|
|
|
which will lead to failed builds.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
gracefulTimeout = mkOption {
|
|
|
|
default = "infinity";
|
|
|
|
type = types.str;
|
|
|
|
example = "5min 20s";
|
|
|
|
description = ''Time to wait until a graceful shutdown is turned into a forceful one.'';
|
|
|
|
};
|
|
|
|
|
2016-09-17 12:08:18 +01:00
|
|
|
workDir = mkOption {
|
|
|
|
default = "/var/lib/gitlab-runner";
|
|
|
|
type = types.path;
|
|
|
|
description = "The working directory used";
|
|
|
|
};
|
|
|
|
|
2017-03-23 21:45:23 +00:00
|
|
|
package = mkOption {
|
|
|
|
description = "Gitlab Runner package to use";
|
|
|
|
default = pkgs.gitlab-runner;
|
|
|
|
defaultText = "pkgs.gitlab-runner";
|
|
|
|
type = types.package;
|
|
|
|
example = literalExample "pkgs.gitlab-runner_1_11";
|
|
|
|
};
|
|
|
|
|
2017-08-30 12:57:57 +01:00
|
|
|
packages = mkOption {
|
|
|
|
default = [ pkgs.bash pkgs.docker-machine ];
|
|
|
|
defaultText = "[ pkgs.bash pkgs.docker-machine ]";
|
|
|
|
type = types.listOf types.package;
|
|
|
|
description = ''
|
|
|
|
Packages to add to PATH for the gitlab-runner process.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-09-17 12:08:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
systemd.services.gitlab-runner = {
|
2017-08-30 12:57:57 +01:00
|
|
|
path = cfg.packages;
|
2017-08-30 09:20:42 +01:00
|
|
|
environment = config.networking.proxy.envVars;
|
2016-09-17 12:08:18 +01:00
|
|
|
description = "Gitlab Runner";
|
2017-05-11 21:29:09 +01:00
|
|
|
after = [ "network.target" ]
|
|
|
|
++ optional hasDocker "docker.service";
|
|
|
|
requires = optional hasDocker "docker.service";
|
2016-09-17 12:08:18 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
2017-03-23 21:45:23 +00:00
|
|
|
ExecStart = ''${cfg.package.bin}/bin/gitlab-runner run \
|
2016-09-17 12:08:18 +01:00
|
|
|
--working-directory ${cfg.workDir} \
|
|
|
|
--config ${configFile} \
|
|
|
|
--service gitlab-runner \
|
|
|
|
--user gitlab-runner \
|
|
|
|
'';
|
2017-07-11 15:38:46 +01:00
|
|
|
|
|
|
|
} // optionalAttrs (cfg.gracefulTermination) {
|
|
|
|
TimeoutStopSec = "${cfg.gracefulTimeout}";
|
|
|
|
KillSignal = "SIGQUIT";
|
|
|
|
KillMode = "process";
|
2016-09-17 12:08:18 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-03-23 21:45:23 +00:00
|
|
|
# Make the gitlab-runner command availabe so users can query the runner
|
|
|
|
environment.systemPackages = [ cfg.package ];
|
|
|
|
|
2018-06-30 00:58:35 +01:00
|
|
|
users.users.gitlab-runner = {
|
2016-09-17 12:08:18 +01:00
|
|
|
group = "gitlab-runner";
|
2017-05-11 21:29:09 +01:00
|
|
|
extraGroups = optional hasDocker "docker";
|
2016-09-17 12:08:18 +01:00
|
|
|
uid = config.ids.uids.gitlab-runner;
|
|
|
|
home = cfg.workDir;
|
|
|
|
createHome = true;
|
|
|
|
};
|
|
|
|
|
2018-06-30 00:58:35 +01:00
|
|
|
users.groups.gitlab-runner.gid = config.ids.gids.gitlab-runner;
|
2016-09-17 12:08:18 +01:00
|
|
|
};
|
|
|
|
}
|