8 jenkinsCfg = config.services.jenkins;
9 cfg = config.services.jenkins.jobBuilder;
14 services.jenkins.jobBuilder = {
15 enable = lib.mkEnableOption ''
16 the Jenkins Job Builder (JJB) service. It
17 allows defining jobs for Jenkins in a declarative manner.
19 Jobs managed through the Jenkins WebUI (or by other means) are left
22 Note that it really is declarative configuration; if you remove a
23 previously defined job, the corresponding job directory will be
26 Please see the Jenkins Job Builder documentation for more info:
27 <https://jenkins-job-builder.readthedocs.io/>
30 accessUser = lib.mkOption {
34 User id in Jenkins used to reload config.
38 accessToken = lib.mkOption {
42 User token in Jenkins used to reload config.
43 WARNING: This token will be world readable in the Nix store. To keep
44 it secret, use the {option}`accessTokenFile` option instead.
48 accessTokenFile = lib.mkOption {
49 default = "${config.services.jenkins.home}/secrets/initialAdminPassword";
50 defaultText = lib.literalExpression ''"''${config.services.jenkins.home}/secrets/initialAdminPassword"'';
52 example = "/run/keys/jenkins-job-builder-access-token";
54 File containing the API token for the {option}`accessUser`
59 yamlJobs = lib.mkOption {
61 type = lib.types.lines;
64 name: jenkins-job-test-1
66 - shell: echo 'Hello world!'
69 Job descriptions for Jenkins Job Builder in YAML format.
73 jsonJobs = lib.mkOption {
75 type = lib.types.listOf lib.types.str;
76 example = lib.literalExpression ''
80 { "name": "jenkins-job-test-2",
81 "builders": [ "shell": "echo 'Hello world!'" ]
89 Job descriptions for Jenkins Job Builder in JSON format.
93 nixJobs = lib.mkOption {
95 type = lib.types.listOf lib.types.attrs;
96 example = lib.literalExpression ''
98 { name = "jenkins-job-test-3";
100 { shell = "echo 'Hello world!'"; }
107 Job descriptions for Jenkins Job Builder in Nix format.
109 This is a trivial wrapper around jsonJobs, using builtins.toJSON
116 config = lib.mkIf (jenkinsCfg.enable && cfg.enable) {
120 if cfg.accessUser != "" then
121 (cfg.accessToken != "" && cfg.accessTokenFile == "")
122 || (cfg.accessToken == "" && cfg.accessTokenFile != "")
126 One of accessToken and accessTokenFile options must be non-empty
127 strings, but not both. Current values:
128 services.jenkins.jobBuilder.accessToken = "${cfg.accessToken}"
129 services.jenkins.jobBuilder.accessTokenFile = "${cfg.accessTokenFile}"
134 systemd.services.jenkins-job-builder = {
135 description = "Jenkins Job Builder Service";
136 # JJB can run either before or after jenkins. We chose after, so we can
137 # always use curl to notify (running) jenkins to reload its config.
138 after = [ "jenkins.service" ];
139 wantedBy = [ "multi-user.target" ];
146 # Q: Why manipulate files directly instead of using "jenkins-jobs upload [...]"?
147 # A: Because this module is for administering a local jenkins install,
148 # and using local file copy allows us to not worry about
152 yamlJobsFile = builtins.toFile "jobs.yaml" cfg.yamlJobs;
153 jsonJobsFiles = map (x: (builtins.toFile "jobs.json" x)) (
154 cfg.jsonJobs ++ [ (builtins.toJSON cfg.nixJobs) ]
156 jobBuilderOutputDir = "/run/jenkins-job-builder/output";
157 # Stamp file is placed in $JENKINS_HOME/jobs/$JOB_NAME/ to indicate
158 # ownership. Enables tracking and removal of stale jobs.
159 ownerStamp = ".config-xml-managed-by-nixos-jenkins-job-builder";
161 echo "Asking Jenkins to reload config"
162 curl_opts="--silent --fail --show-error"
164 if cfg.accessTokenFile != "" then
167 "$RUNTIME_DIRECTORY/jenkins_access_token.txt"
169 if [ "${cfg.accessToken}" != "" ]; then
170 (umask 0077; printf "${cfg.accessToken}" >"$access_token_file")
172 jenkins_url="http://${jenkinsCfg.listenAddress}:${toString jenkinsCfg.port}${jenkinsCfg.prefix}"
173 auth_file="$RUNTIME_DIRECTORY/jenkins_auth_file.txt"
174 trap 'rm -f "$auth_file"' EXIT
175 (umask 0077; printf "${cfg.accessUser}:@password_placeholder@" >"$auth_file")
176 "${pkgs.replace-secret}/bin/replace-secret" "@password_placeholder@" "$access_token_file" "$auth_file"
178 if ! "${pkgs.jenkins}/bin/jenkins-cli" -s "$jenkins_url" -auth "@$auth_file" reload-configuration; then
179 echo "error: failed to reload configuration"
191 printf "%s" "$first" "''${@/#/$separator}"
194 # Map a relative directory path in the output from
195 # jenkins-job-builder (jobname) to the layout expected by jenkins:
196 # each directory level gets prepended "jobs/".
199 IFS='/' read -ra input_dirs <<< "$1"
201 joinByString "/jobs/" "''${input_dirs[@]}"
204 # The inverse of getJenkinsJobDir (remove the "jobs/" prefixes)
207 IFS='/' read -ra input_dirs <<< "$1"
209 local nelem=''${#input_dirs[@]}
210 for e in "''${input_dirs[@]}"; do
211 if [ $((i % 2)) -eq 1 ]; then
213 if [ $i -lt $(( nelem - 1 )) ]; then
221 rm -rf ${jobBuilderOutputDir}
222 cur_decl_jobs=/run/jenkins-job-builder/declarative-jobs
223 rm -f "$cur_decl_jobs"
225 # Create / update jobs
226 mkdir -p ${jobBuilderOutputDir}
227 for inputFile in ${yamlJobsFile} ${lib.concatStringsSep " " jsonJobsFiles}; do
228 HOME="${jenkinsCfg.home}" "${pkgs.jenkins-job-builder}/bin/jenkins-jobs" --ignore-cache test --config-xml -o "${jobBuilderOutputDir}" "$inputFile"
231 find "${jobBuilderOutputDir}" -type f -name config.xml | while read -r f; do echo "$(dirname "$f")"; done | sort | while read -r dir; do
232 jobname="$(realpath --relative-to="${jobBuilderOutputDir}" "$dir")"
233 jenkinsjobname=$(getJenkinsJobDir "$jobname")
234 jenkinsjobdir="${jenkinsCfg.home}/$jenkinsjobname"
235 echo "Creating / updating job \"$jobname\""
236 mkdir -p "$jenkinsjobdir"
237 touch "$jenkinsjobdir/${ownerStamp}"
238 cp "$dir"/config.xml "$jenkinsjobdir/config.xml"
239 echo "$jenkinsjobname" >> "$cur_decl_jobs"
243 find "${jenkinsCfg.home}" -type f -name "${ownerStamp}" | while read -r f; do echo "$(dirname "$f")"; done | sort --reverse | while read -r dir; do
244 jenkinsjobname="$(realpath --relative-to="${jenkinsCfg.home}" "$dir")"
245 grep --quiet --line-regexp "$jenkinsjobname" "$cur_decl_jobs" 2>/dev/null && continue
246 jobname=$(getJobname "$jenkinsjobname")
247 echo "Deleting stale job \"$jobname\""
248 jobdir="${jenkinsCfg.home}/$jenkinsjobname"
252 + (lib.optionalString (cfg.accessUser != "") reloadScript);
255 User = jenkinsCfg.user;
256 RuntimeDirectory = "jenkins-job-builder";