git.iko.soy/typeable/nixpkgs
on master help sign in
Revert "nixos/gitlab-runner: support multiple services"

Reverting because of merge conflict. Rebase the fix and submit again. This reverts commit 3853c2711126d2c1518514222d19be631b609bac.

commit309711c4b4b665f47876f2a113d3872237467784 authorFrederik Rietdijk <fridh@fridh.nl> dateSat 2 May 2020, 09:38 (7 years ago) parentf855e59
1 file changed+103−437
nixos/modules/services/continuous-integration/gitlab-runner.nix+103−437
nixos/modules/services/continuous-integration/gitlab-runner.nix+103−437
@@ -1,494 +1,160 @@
11{ config, lib, pkgs, ... }:
2+
23with lib;
4+
35let
46 cfg = config.services.gitlab-runner;
7+ configFile =
8+ if (cfg.configFile == null) then
9+ (pkgs.runCommand "config.toml" {
10+ buildInputs = [ pkgs.remarshal ];
11+ preferLocalBuild = true;
12+ } ''
13+ remarshal -if json -of toml \
14+ < ${pkgs.writeText "config.json" (builtins.toJSON cfg.configOptions)} \
15+ > $out
16+ '')
17+ else
18+ cfg.configFile;
519 hasDocker = config.virtualisation.docker.enable;
6− hashedServices = with builtins; (mapAttrs' (name: service: nameValuePair
7− "${name}_${config.networking.hostName}_${
8− substring 0 12
9− (hashString "md5" (unsafeDiscardStringContext (toJSON service)))}"
10− service)
11− cfg.services);
12− configPath = "$HOME/.gitlab-runner/config.toml";
13− configureScript = pkgs.writeShellScriptBin "gitlab-runner-configure" (
14− if (cfg.configFile != null) then ''
15− mkdir -p $(dirname ${configPath})
16− cp ${cfg.configFile} ${configPath}
17− # make config file readable by service
18− chown -R --reference=$HOME $(dirname ${configPath})
19− '' else ''
20− export CONFIG_FILE=${configPath}
21−
22− mkdir -p $(dirname ${configPath})
23−
24− # remove no longer existing services
25− gitlab-runner verify --delete
26−
27− # current and desired state
28− NEEDED_SERVICES=$(echo ${concatStringsSep " " (attrNames hashedServices)} | tr " " "\n")
29− REGISTERED_SERVICES=$(gitlab-runner list 2>&1 | grep 'Executor' | awk '{ print $1 }')
30−
31− # difference between current and desired state
32− NEW_SERVICES=$(grep -vxF -f <(echo "$REGISTERED_SERVICES") <(echo "$NEEDED_SERVICES") || true)
33− OLD_SERVICES=$(grep -vxF -f <(echo "$NEEDED_SERVICES") <(echo "$REGISTERED_SERVICES") || true)
34−
35− # register new services
36− ${concatStringsSep "\n" (mapAttrsToList (name: service: ''
37− if echo "$NEW_SERVICES" | grep -xq ${name}; then
38− bash -c ${escapeShellArg (concatStringsSep " \\\n " ([
39− "set -a && source ${service.registrationConfigFile} &&"
40− "gitlab-runner register"
41− "--non-interactive"
42− "--name ${name}"
43− "--executor ${service.executor}"
44− "--limit ${toString service.limit}"
45− "--request-concurrency ${toString service.requestConcurrency}"
46− "--maximum-timeout ${toString service.maximumTimeout}"
47− ] ++ service.registrationFlags
48− ++ optional (service.buildsDir != null)
49− "--builds-dir ${service.buildsDir}"
50− ++ optional (service.preCloneScript != null)
51− "--pre-clone-script ${service.preCloneScript}"
52− ++ optional (service.preBuildScript != null)
53− "--pre-build-script ${service.preBuildScript}"
54− ++ optional (service.postBuildScript != null)
55− "--post-build-script ${service.postBuildScript}"
56− ++ optional (service.tagList != [ ])
57− "--tag-list ${concatStringsSep "," service.tagList}"
58− ++ optional service.runUntagged
59− "--run-untagged"
60− ++ optional service.protected
61− "--access-level ref_protected"
62− ++ optional service.debugTraceDisabled
63− "--debug-trace-disabled"
64− ++ map (e: "--env ${escapeShellArg e}") (mapAttrsToList (name: value: "${name}=${value}") service.environmentVariables)
65− ++ optionals (service.executor == "docker") (
66− assert (
67− assertMsg (service.dockerImage != null)
68− "dockerImage option is required for docker executor (${name})");
69− [ "--docker-image ${service.dockerImage}" ]
70− ++ optional service.dockerDisableCache
71− "--docker-disable-cache"
72− ++ optional service.dockerPrivileged
73− "--docker-privileged"
74− ++ map (v: "--docker-volumes ${escapeShellArg v}") service.dockerVolumes
75− ++ map (v: "--docker-extra-hosts ${escapeShellArg v}") service.dockerExtraHosts
76− ++ map (v: "--docker-allowed-images ${escapeShellArg v}") service.dockerAllowedImages
77− ++ map (v: "--docker-allowed-services ${escapeShellArg v}") service.dockerAllowedServices
78− )
79− ))} && sleep 1
80− fi
81− '') hashedServices)}
82−
83− # unregister old services
84− for NAME in $(echo "$OLD_SERVICES")
85− do
86− [ ! -z "$NAME" ] && gitlab-runner unregister \
87− --name "$NAME" && sleep 1
88− done
89−
90− # update global options
91− remarshal --if toml --of json ${configPath} \
92− | jq -cM '.check_interval = ${toString cfg.checkInterval} |
93− .concurrent = ${toString cfg.concurrent}' \
94− | remarshal --if json --of toml \
95− | sponge ${configPath}
96−
97− # make config file readable by service
98− chown -R --reference=$HOME $(dirname ${configPath})
99− '');
100− startScript = pkgs.writeShellScriptBin "gitlab-runner-start" ''
101− export CONFIG_FILE=${configPath}
102− exec gitlab-runner run --working-directory $HOME
103− '';
10420in
10521{
10622 options.services.gitlab-runner = {
10723 enable = mkEnableOption "Gitlab Runner";
24+
10825 configFile = mkOption {
109− type = types.nullOr types.path;
11026 default = null;
11127 description = ''
11228 Configuration file for gitlab-runner.
29+ Use this option in favor of configOptions to avoid placing CI tokens in the nix store.
11330
114− <option>configFile</option> takes precedence over <option>services</option>.
115− <option>checkInterval</option> and <option>concurrent</option> will be ignored too.
31+ <option>configFile</option> takes precedence over <option>configOptions</option>.
11632
117− This option is deprecated, please use <option>services</option> instead.
118− You can use <option>registrationConfigFile</option> and
119− <option>registrationFlags</option>
120− for settings not covered by this module.
121− '';
122− };
123− checkInterval = mkOption {
124− type = types.int;
125− default = 0;
126− example = literalExample "with lib; (length (attrNames config.services.gitlab-runner.services)) * 3";
127− description = ''
128− Defines the interval length, in seconds, between new jobs check.
129− The default value is 3;
130− if set to 0 or lower, the default value will be used.
131− See <link xlink:href="https://docs.gitlab.com/runner/configuration/advanced-configuration.html#how-check_interval-works">runner documentation</link> for more information.
33+ Warning: Not using <option>configFile</option> will potentially result in secrets
34+ leaking into the WORLD-READABLE nix store.
13235 '';
36+ type = types.nullOr types.path;
13337 };
134− concurrent = mkOption {
135− type = types.int;
136− default = 1;
137− example = literalExample "config.nix.maxJobs";
38+
39+ configOptions = mkOption {
13840 description = ''
139− Limits how many jobs globally can be run concurrently.
140− The most upper limit of jobs using all defined runners.
141− 0 does not mean unlimited.
41+ Configuration for gitlab-runner
42+ <option>configFile</option> will take precedence over this option.
43+
44+ Warning: all Configuration, especially CI token, will be stored in a
45+ WORLD-READABLE file in the Nix Store.
46+
47+ If you want to protect your CI token use <option>configFile</option> instead.
14248 '';
49+ type = types.attrs;
50+ example = {
51+ concurrent = 2;
52+ runners = [{
53+ name = "docker-nix-1.11";
54+ url = "https://CI/";
55+ token = "TOKEN";
56+ executor = "docker";
57+ builds_dir = "";
58+ docker = {
59+ host = "";
60+ image = "nixos/nix:1.11";
61+ privileged = true;
62+ disable_cache = true;
63+ cache_dir = "";
64+ };
65+ }];
66+ };
14367 };
68+
14469 gracefulTermination = mkOption {
145− type = types.bool;
14670 default = false;
71+ type = types.bool;
14772 description = ''
148− Finish all remaining jobs before stopping.
149− If not set gitlab-runner will stop immediatly without waiting
150− for jobs to finish, which will lead to failed builds.
73+ Finish all remaining jobs before stopping, restarting or reconfiguring.
74+ If not set gitlab-runner will stop immediatly without waiting for jobs to finish,
75+ which will lead to failed builds.
15176 '';
15277 };
78+
15379 gracefulTimeout = mkOption {
154− type = types.str;
15580 default = "infinity";
81+ type = types.str;
15682 example = "5min 20s";
157− description = ''
158− Time to wait until a graceful shutdown is turned into a forceful one.
159− '';
83+ description = ''Time to wait until a graceful shutdown is turned into a forceful one.'';
84+ };
85+
86+ workDir = mkOption {
87+ default = "/var/lib/gitlab-runner";
88+ type = types.path;
89+ description = "The working directory used";
16090 };
91+
16192 package = mkOption {
162− type = types.package;
93+ description = "Gitlab Runner package to use";
16394 default = pkgs.gitlab-runner;
16495 defaultText = "pkgs.gitlab-runner";
96+ type = types.package;
16597 example = literalExample "pkgs.gitlab-runner_1_11";
166− description = "Gitlab Runner package to use.";
16798 };
168− extraPackages = mkOption {
99+
100+ packages = mkOption {
101+ default = [ pkgs.bash pkgs.docker-machine ];
102+ defaultText = "[ pkgs.bash pkgs.docker-machine ]";
169103 type = types.listOf types.package;
170− default = [ ];
171104 description = ''
172− Extra packages to add to PATH for the gitlab-runner process.
105+ Packages to add to PATH for the gitlab-runner process.
173106 '';
174107 };
175− services = mkOption {
176− description = "GitLab Runner services.";
177− default = { };
178− example = literalExample ''
179− {
180− # runner for building in docker via host's nix-daemon
181− # nix store will be readable in runner, might be insecure
182− nix = {
183− # File should contain at least these two variables:
184− # `CI_SERVER_URL`
185− # `REGISTRATION_TOKEN`
186− registrationConfigFile = "/run/secrets/gitlab-runner-registration";
187− dockerImage = "alpine";
188− dockerVolumes = [
189− "/nix/store:/nix/store:ro"
190− "/nix/var/nix/db:/nix/var/nix/db:ro"
191− "/nix/var/nix/daemon-socket:/nix/var/nix/daemon-socket:ro"
192− ];
193− dockerDisableCache = true;
194− preBuildScript = pkgs.writeScript "setup-container" '''
195− mkdir -p -m 0755 /nix/var/log/nix/drvs
196− mkdir -p -m 0755 /nix/var/nix/gcroots
197− mkdir -p -m 0755 /nix/var/nix/profiles
198− mkdir -p -m 0755 /nix/var/nix/temproots
199− mkdir -p -m 0755 /nix/var/nix/userpool
200− mkdir -p -m 1777 /nix/var/nix/gcroots/per-user
201− mkdir -p -m 1777 /nix/var/nix/profiles/per-user
202− mkdir -p -m 0755 /nix/var/nix/profiles/per-user/root
203− mkdir -p -m 0700 "$HOME/.nix-defexpr"
204−
205− . ''${pkgs.nix}/etc/profile.d/nix.sh
206108
207− ''${pkgs.nix}/bin/nix-env -i ''${concatStringsSep " " (with pkgs; [ nix cacert git openssh ])}
208−
209− ''${pkgs.nix}/bin/nix-channel --add https://nixos.org/channels/nixpkgs-unstable
210− ''${pkgs.nix}/bin/nix-channel --update nixpkgs
211− ''';
212− environmentVariables = {
213− ENV = "/etc/profile";
214− USER = "root";
215− NIX_REMOTE = "daemon";
216− PATH = "/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/default/sbin:/bin:/sbin:/usr/bin:/usr/sbin";
217− NIX_SSL_CERT_FILE = "/nix/var/nix/profiles/default/etc/ssl/certs/ca-bundle.crt";
218− };
219− tagList = [ "nix" ];
220− };
221− # runner for building docker images
222− docker-images = {
223− # File should contain at least these two variables:
224− # `CI_SERVER_URL`
225− # `REGISTRATION_TOKEN`
226− registrationConfigFile = "/run/secrets/gitlab-runner-registration";
227− dockerImage = "docker:stable";
228− dockerVolumes = [
229− "/var/run/docker.sock:/var/run/docker.sock"
230− ];
231− tagList = [ "docker-images" ];
232− };
233− # runner for executing stuff on host system (very insecure!)
234− # make sure to add required packages (including git!)
235− # to `environment.systemPackages`
236− shell = {
237− # File should contain at least these two variables:
238− # `CI_SERVER_URL`
239− # `REGISTRATION_TOKEN`
240− registrationConfigFile = "/run/secrets/gitlab-runner-registration";
241− executor = "shell";
242− tagList = [ "shell" ];
243− };
244− # runner for everything else
245− default = {
246− # File should contain at least these two variables:
247− # `CI_SERVER_URL`
248− # `REGISTRATION_TOKEN`
249− registrationConfigFile = "/run/secrets/gitlab-runner-registration";
250− dockerImage = "debian:stable";
251− };
252− }
253− '';
254− type = types.attrsOf (types.submodule {
255− options = {
256− registrationConfigFile = mkOption {
257− type = types.path;
258− description = ''
259− Absolute path to a file with environment variables
260− used for gitlab-runner registration.
261− A list of all supported environment variables can be found in
262− <literal>gitlab-runner register --help</literal>.
263−
264− Ones that you probably want to set is
265−
266− <literal>CI_SERVER_URL=&lt;CI server URL&gt;</literal>
267−
268− <literal>REGISTRATION_TOKEN=&lt;registration secret&gt;</literal>
269− '';
270− };
271− registrationFlags = mkOption {
272− type = types.listOf types.str;
273− default = [ ];
274− example = [ "--docker-helper-image my/gitlab-runner-helper" ];
275− description = ''
276− Extra command-line flags passed to
277− <literal>gitlab-runner register</literal>.
278− Execute <literal>gitlab-runner register --help</literal>
279− for a list of supported flags.
280− '';
281− };
282− environmentVariables = mkOption {
283− type = types.attrsOf types.str;
284− default = { };
285− example = { NAME = "value"; };
286− description = ''
287− Custom environment variables injected to build environment.
288− For secrets you can use <option>registrationConfigFile</option>
289− with <literal>RUNNER_ENV</literal> variable set.
290− '';
291− };
292− executor = mkOption {
293− type = types.str;
294− default = "docker";
295− description = ''
296− Select executor, eg. shell, docker, etc.
297− See <link xlink:href="https://docs.gitlab.com/runner/executors/README.html">runner documentation</link> for more information.
298− '';
299− };
300− buildsDir = mkOption {
301− type = types.nullOr types.path;
302− default = null;
303− example = "/var/lib/gitlab-runner/builds";
304− description = ''
305− Absolute path to a directory where builds will be stored
306− in context of selected executor (Locally, Docker, SSH).
307− '';
308− };
309− dockerImage = mkOption {
310− type = types.nullOr types.str;
311− default = null;
312− description = ''
313− Docker image to be used.
314− '';
315− };
316− dockerVolumes = mkOption {
317− type = types.listOf types.str;
318− default = [ ];
319− example = [ "/var/run/docker.sock:/var/run/docker.sock" ];
320− description = ''
321− Bind-mount a volume and create it
322− if it doesn't exist prior to mounting.
323− '';
324− };
325− dockerDisableCache = mkOption {
326− type = types.bool;
327− default = false;
328− description = ''
329− Disable all container caching.
330− '';
331− };
332− dockerPrivileged = mkOption {
333− type = types.bool;
334− default = false;
335− description = ''
336− Give extended privileges to container.
337− '';
338− };
339− dockerExtraHosts = mkOption {
340− type = types.listOf types.str;
341− default = [ ];
342− example = [ "other-host:127.0.0.1" ];
343− description = ''
344− Add a custom host-to-IP mapping.
345− '';
346− };
347− dockerAllowedImages = mkOption {
348− type = types.listOf types.str;
349− default = [ ];
350− example = [ "ruby:*" "python:*" "php:*" "my.registry.tld:5000/*:*" ];
351− description = ''
352− Whitelist allowed images.
353− '';
354− };
355− dockerAllowedServices = mkOption {
356− type = types.listOf types.str;
357− default = [ ];
358− example = [ "postgres:9" "redis:*" "mysql:*" ];
359− description = ''
360− Whitelist allowed services.
361− '';
362− };
363− preCloneScript = mkOption {
364− type = types.nullOr types.path;
365− default = null;
366− description = ''
367− Runner-specific command script executed before code is pulled.
368− '';
369− };
370− preBuildScript = mkOption {
371− type = types.nullOr types.path;
372− default = null;
373− description = ''
374− Runner-specific command script executed after code is pulled,
375− just before build executes.
376− '';
377− };
378− postBuildScript = mkOption {
379− type = types.nullOr types.path;
380− default = null;
381− description = ''
382− Runner-specific command script executed after code is pulled
383− and just after build executes.
384− '';
385− };
386− tagList = mkOption {
387− type = types.listOf types.str;
388− default = [ ];
389− description = ''
390− Tag list.
391− '';
392− };
393− runUntagged = mkOption {
394− type = types.bool;
395− default = false;
396− description = ''
397− Register to run untagged builds; defaults to
398− <literal>true</literal> when <option>tagList</option> is empty.
399− '';
400− };
401− limit = mkOption {
402− type = types.int;
403− default = 0;
404− description = ''
405− Limit how many jobs can be handled concurrently by this service.
406− 0 (default) simply means don't limit.
407− '';
408− };
409− requestConcurrency = mkOption {
410− type = types.int;
411− default = 0;
412− description = ''
413− Limit number of concurrent requests for new jobs from GitLab.
414− '';
415− };
416− maximumTimeout = mkOption {
417− type = types.int;
418− default = 0;
419− description = ''
420− What is the maximum timeout (in seconds) that will be set for
421− job when using this Runner. 0 (default) simply means don't limit.
422− '';
423− };
424− protected = mkOption {
425− type = types.bool;
426− default = false;
427− description = ''
428− When set to true Runner will only run on pipelines
429− triggered on protected branches.
430− '';
431− };
432− debugTraceDisabled = mkOption {
433− type = types.bool;
434− default = false;
435− description = ''
436− When set to true Runner will disable the possibility of
437− using the <literal>CI_DEBUG_TRACE</literal> feature.
438− '';
439− };
440− };
441− });
442− };
443109 };
110+
444111 config = mkIf cfg.enable {
445− warnings = optional (cfg.configFile != null) "services.gitlab-runner.`configFile` is deprecated, please use services.gitlab-runner.`services`.";
446− environment.systemPackages = [ cfg.package ];
447112 systemd.services.gitlab-runner = {
113+ path = cfg.packages;
114+ environment = config.networking.proxy.envVars // {
115+ # Gitlab runner will not start if the HOME variable is not set
116+ HOME = cfg.workDir;
117+ };
448118 description = "Gitlab Runner";
449− documentation = [ "https://docs.gitlab.com/runner/" ];
450119 after = [ "network.target" ]
451120 ++ optional hasDocker "docker.service";
452121 requires = optional hasDocker "docker.service";
453122 wantedBy = [ "multi-user.target" ];
454− environment = config.networking.proxy.envVars // {
455− HOME = "/var/lib/gitlab-runner";
456− };
457− path = with pkgs; [
458− bash
459− gawk
460− jq
461− moreutils
462− remarshal
463− utillinux
464− cfg.package.bin
465− ] ++ cfg.extraPackages;
466123 reloadIfChanged = true;
124+ restartTriggers = [
125+ config.environment.etc."gitlab-runner/config.toml".source
126+ ];
467127 serviceConfig = {
468− # Set `DynamicUser` under `systemd.services.gitlab-runner.serviceConfig`
469− # to `lib.mkForce false` in your configuration to run this service as root.
470− # You can also set `User` and `Group` options to run this service as desired user.
471− # Make sure to restart service or changes won't apply.
472− DynamicUser = true;
473128 StateDirectory = "gitlab-runner";
474− SupplementaryGroups = optional hasDocker "docker";
475− ExecStartPre = "!${configureScript}/bin/gitlab-runner-configure";
476− ExecStart = "${startScript}/bin/gitlab-runner-start";
477− ExecReload = "!${configureScript}/bin/gitlab-runner-configure";
478− } // optionalAttrs (cfg.gracefulTermination) {
129+ ExecReload= "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
130+ ExecStart = ''${cfg.package.bin}/bin/gitlab-runner run \
131+ --working-directory ${cfg.workDir} \
132+ --config /etc/gitlab-runner/config.toml \
133+ --service gitlab-runner \
134+ --user gitlab-runner \
135+ '';
136+
137+ } // optionalAttrs (cfg.gracefulTermination) {
479138 TimeoutStopSec = "${cfg.gracefulTimeout}";
480139 KillSignal = "SIGQUIT";
481140 KillMode = "process";
482141 };
483142 };
484− # Enable docker if `docker` executor is used in any service
485− virtualisation.docker.enable = mkIf (
486− any (s: s.executor == "docker") (attrValues cfg.services)
487− ) (mkDefault true);
143+
144+ # Make the gitlab-runner command availabe so users can query the runner
145+ environment.systemPackages = [ cfg.package ];
146+
147+ # Make sure the config can be reloaded on change
148+ environment.etc."gitlab-runner/config.toml".source = configFile;
149+
150+ users.users.gitlab-runner = {
151+ group = "gitlab-runner";
152+ extraGroups = optional hasDocker "docker";
153+ uid = config.ids.uids.gitlab-runner;
154+ home = cfg.workDir;
155+ createHome = true;
156+ };
157+
158+ users.groups.gitlab-runner.gid = config.ids.gids.gitlab-runner;
488159 };
489− imports = [
490− (mkRenamedOptionModule [ "services" "gitlab-runner" "packages" ] [ "services" "gitlab-runner" "extraPackages" ] )
491− (mkRemovedOptionModule [ "services" "gitlab-runner" "configOptions" ] "Use services.gitlab-runner.services option instead" )
492− (mkRemovedOptionModule [ "services" "gitlab-runner" "workDir" ] "You should move contents of workDir (if any) to /var/lib/gitlab-runner" )
493− ];
494160}