Merge pull request #205526 from newAM/github-runner-oom

nixos/github-runner: add workDir option

authored by

Aaron Andersen and committed by
GitHub
4769274f 3bc50ffb

+35 -14
+12
nixos/modules/services/continuous-integration/github-runner/options.nix
··· 170 170 default = null; 171 171 defaultText = literalExpression "username"; 172 172 }; 173 + 174 + workDir = mkOption { 175 + type = with types; nullOr str; 176 + description = lib.mdDoc '' 177 + Working directory, available as `$GITHUB_WORKSPACE` during workflow runs 178 + and used as a default for [repository checkouts](https://github.com/actions/checkout). 179 + The service cleans this directory on every service start. 180 + 181 + A value of `null` will default to the systemd `RuntimeDirectory`. 182 + ''; 183 + default = null; 184 + }; 173 185 }
+23 -14
nixos/modules/services/continuous-integration/github-runner/service.nix
··· 20 20 21 21 with lib; 22 22 23 + let 24 + workDir = if cfg.workDir == null then runtimeDir else cfg.workDir; 25 + in 23 26 { 24 27 description = "GitHub Actions runner"; 25 28 ··· 28 31 after = [ "network.target" "network-online.target" ]; 29 32 30 33 environment = { 31 - HOME = runtimeDir; 34 + HOME = workDir; 32 35 RUNNER_ROOT = stateDir; 33 36 } // cfg.extraEnvironment; 34 37 ··· 42 45 config.nix.package 43 46 ] ++ cfg.extraPackages; 44 47 45 - serviceConfig = rec { 48 + serviceConfig = { 46 49 ExecStart = "${cfg.package}/bin/Runner.Listener run --startuptype service"; 47 50 48 51 # Does the following, sequentially: ··· 54 57 # - Set up the directory structure by creating the necessary symlinks. 55 58 ExecStartPre = 56 59 let 57 - # Wrapper script which expects the full path of the state, runtime and logs 60 + # Wrapper script which expects the full path of the state, working and logs 58 61 # directory as arguments. Overrides the respective systemd variables to provide 59 62 # unambiguous directory names. This becomes relevant, for example, if the 60 63 # caller overrides any of the StateDirectory=, RuntimeDirectory= or LogDirectory= ··· 65 68 set -euo pipefail 66 69 67 70 STATE_DIRECTORY="$1" 68 - RUNTIME_DIRECTORY="$2" 71 + WORK_DIRECTORY="$2" 69 72 LOGS_DIRECTORY="$3" 70 73 71 74 ${lines} 72 75 ''; 73 - runnerRegistrationConfig = getAttrs [ "name" "tokenFile" "url" "runnerGroup" "extraLabels" "ephemeral" ] cfg; 76 + runnerRegistrationConfig = getAttrs [ "name" "tokenFile" "url" "runnerGroup" "extraLabels" "ephemeral" "workDir" ] cfg; 74 77 newConfigPath = builtins.toFile "${svcName}-config.json" (builtins.toJSON runnerRegistrationConfig); 75 78 currentConfigPath = "$STATE_DIRECTORY/.nixos-current-config.json"; 76 79 newConfigTokenPath= "$STATE_DIRECTORY/.new-token"; ··· 119 122 else 120 123 # The state directory is entirely empty which indicates a first start 121 124 copy_tokens 122 - fi ''; 125 + fi 126 + ''; 123 127 configureRunner = writeScript "configure" '' 124 128 if [[ -e "${newConfigTokenPath}" ]]; then 125 129 echo "Configuring GitHub Actions Runner" 126 130 args=( 127 131 --unattended 128 132 --disableupdate 129 - --work "$RUNTIME_DIRECTORY" 133 + --work "$WORK_DIRECTORY" 130 134 --url ${escapeShellArg cfg.url} 131 135 --labels ${escapeShellArg (concatStringsSep "," cfg.extraLabels)} 132 136 --name ${escapeShellArg cfg.name} ··· 153 157 ln -s '${newConfigPath}' "${currentConfigPath}" 154 158 fi 155 159 ''; 156 - setupRuntimeDir = writeScript "setup-runtime-dirs" '' 160 + setupWorkDir = writeScript "setup-work-dirs" '' 161 + # Cleanup previous service 162 + ${pkgs.findutils}/bin/find -H "$WORK_DIRECTORY" -mindepth 1 -delete 163 + 157 164 # Link _diag dir 158 - ln -s "$LOGS_DIRECTORY" "$RUNTIME_DIRECTORY/_diag" 165 + ln -s "$LOGS_DIRECTORY" "$WORK_DIRECTORY/_diag" 159 166 160 - # Link the runner credentials to the runtime dir 161 - ln -s "$STATE_DIRECTORY"/{${lib.concatStringsSep "," runnerCredFiles}} "$RUNTIME_DIRECTORY/" 167 + # Link the runner credentials to the work dir 168 + ln -s "$STATE_DIRECTORY"/{${lib.concatStringsSep "," runnerCredFiles}} "$WORK_DIRECTORY/" 162 169 ''; 163 170 in 164 - map (x: "${x} ${escapeShellArgs [ stateDir runtimeDir logsDir ]}") [ 171 + map (x: "${x} ${escapeShellArgs [ stateDir workDir logsDir ]}") [ 165 172 "+${unconfigureRunner}" # runs as root 166 173 configureRunner 167 - setupRuntimeDir 174 + setupWorkDir 168 175 ]; 169 176 170 177 # If running in ephemeral mode, restart the service on-exit (i.e., successful de-registration of the runner) ··· 181 188 # Home of persistent runner data, e.g., credentials 182 189 StateDirectory = [ systemdDir ]; 183 190 StateDirectoryMode = "0700"; 184 - WorkingDirectory = runtimeDir; 191 + WorkingDirectory = workDir; 185 192 186 193 InaccessiblePaths = [ 187 194 # Token file path given in the configuration, if visible to the service ··· 231 238 "~sethostname" 232 239 ]; 233 240 RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" "AF_NETLINK" ]; 241 + 242 + BindPaths = lib.optionals (cfg.workDir != null) [ cfg.workDir ]; 234 243 235 244 # Needs network access 236 245 PrivateNetwork = false;