gitlab-runner.nix 4.25 KB
Newer Older
1 2 3 4 5 6 7
/*
 * An opinonated Gitlab-runner, that allows for nix builds (with caching)
 * on NixOS build machines
 */
{ config, pkgs, lib, ...}:
with lib;
let
8
  cfg = config.services.gitlabrunner;
9 10
in
  {
11
    options.services.gitlabrunner = {
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
      enable = lib.mkEnableOption "Gitlab Runner";
      registrationConfigFile = lib.mkOption {
        description = ''
          Configuration file used got gitlab-runner registration.
          It is a list of environment variables.
          A list of all supported environment variables can be found
          in
             gitlab-runner register --help

          One that you probably want to set is
          CI_SERVER_URL=<CI server URL>
          REGISTRATION_TOKEN=<registration secret>

        '';
        type = lib.types.path;
      };

      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.
        '';
      };

39 40 41 42 43 44 45
      name = mkOption {
        default = "gitlab-runner";
        type = types.str;
        example = "my-gitlab-runner";
        description = ''Gitlab runner name.'';
      };

46 47 48 49 50 51 52
      gracefulTimeout = mkOption {
        default = "infinity";
        type = types.str;
        example = "5min 20s";
        description = ''Time to wait until a graceful shutdown is turned into a forceful one.'';
      };

53 54 55 56 57 58 59
      concurrency = mkOption {
        default = "1";
        type = types.str;
        example = "2";
        description = ''Runner concurrency.'';
      };

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
      workDir = mkOption {
        default = "/var/lib/gitlab-runner";
        type = types.path;
        description = "The working directory used";
      };

      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";
      };

      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.
        '';
      };
    };
    config = mkIf cfg.enable {
84 85
      systemd.services.gitlabrunner = {
        path = cfg.packages ++ [pkgs.gnused] ;
86 87 88 89 90 91 92
        environment = config.networking.proxy.envVars;
        description = "Gitlab Runner";
        after = [ "network.target" "docker.service"];
        requires = ["docker.service"];
        wantedBy = [ "multi-user.target" ];
        serviceConfig = {
          EnvironmentFile = "${cfg.registrationConfigFile}";
93
          ExecStartPre = [''${cfg.package.bin}/bin/gitlab-runner register \
94
            --non-interactive=true \
95
            --name ${cfg.name} \
96
            --executor "shell" \
97
            --maximum-timeout 1800 \
98
            --request-concurrency ${cfg.concurrency} \
99
            --tag-list "integration,notknl,singularity,kvm,nix"\
100
          ''
Valentin Reis's avatar
Valentin Reis committed
101
          "${pkgs.gnused}/bin/sed -i 's/concurrent = .*$/concurrent = ${cfg.concurrency}/' /etc/gitlab-runner/config.toml"];
102 103
          ExecStart = ''
            ${cfg.package.bin}/bin/gitlab-runner run \
104 105 106 107 108
            --working-directory ${cfg.workDir} \
            --service gitlab-runner \
          '';
            #--user gitlab-runner \
          ExecStopPost = ''${cfg.package.bin}/bin/gitlab-runner unregister \
109
            --name ${cfg.name}
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
          '';

        } //  optionalAttrs (cfg.gracefulTermination) {
          TimeoutStopSec = "${cfg.gracefulTimeout}";
          KillSignal = "SIGQUIT";
          KillMode = "process";
        };
      };

      virtualisation.docker.enable = true;

      # Make the gitlab-runner command availabe so users can query the runner
      environment.systemPackages = [ cfg.package ];

      users.users.gitlab-runner = {
        group = "gitlab-runner";
        extraGroups = ["docker"];
        uid = config.ids.uids.gitlab-runner;
        home = cfg.workDir;
        createHome = true;
      };

      users.groups.gitlab-runner.gid = config.ids.gids.gitlab-runner;
    };
  }