nixos/jenkins-job-builder: add support for folder jobs
Add support for folder jobs (https://plugins.jenkins.io/cloudbees-folder/) by reworking the service to support nested jobs. This also fixes this deprecation warning (as a happy side effect): WARNING:jenkins_jobs.cli.subcommand.test:(Deprecated) The default output behavior of `jenkins-jobs test` when given the --output flag will change in JJB 3.0. Instead of writing jobs to OUTPUT/jobname; they will be written to OUTPUT/jobname/config.xml. The new behavior can be enabled by the passing `--config-xml` parameter
This commit is contained in:
parent
28a0d6d7a2
commit
4bcb22e17a
@ -165,6 +165,42 @@ in {
|
|||||||
'';
|
'';
|
||||||
in
|
in
|
||||||
''
|
''
|
||||||
|
joinByString()
|
||||||
|
{
|
||||||
|
local separator="$1"
|
||||||
|
shift
|
||||||
|
local first="$1"
|
||||||
|
shift
|
||||||
|
printf "%s" "$first" "''${@/#/$separator}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Map a relative directory path in the output from
|
||||||
|
# jenkins-job-builder (jobname) to the layout expected by jenkins:
|
||||||
|
# each directory level gets prepended "jobs/".
|
||||||
|
getJenkinsJobDir()
|
||||||
|
{
|
||||||
|
IFS='/' read -ra input_dirs <<< "$1"
|
||||||
|
printf "jobs/"
|
||||||
|
joinByString "/jobs/" "''${input_dirs[@]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# The inverse of getJenkinsJobDir (remove the "jobs/" prefixes)
|
||||||
|
getJobname()
|
||||||
|
{
|
||||||
|
IFS='/' read -ra input_dirs <<< "$1"
|
||||||
|
local i=0
|
||||||
|
local nelem=''${#input_dirs[@]}
|
||||||
|
for e in "''${input_dirs[@]}"; do
|
||||||
|
if [ $((i % 2)) -eq 1 ]; then
|
||||||
|
printf "$e"
|
||||||
|
if [ $i -lt $(( nelem - 1 )) ]; then
|
||||||
|
printf "/"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
i=$((i + 1))
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
rm -rf ${jobBuilderOutputDir}
|
rm -rf ${jobBuilderOutputDir}
|
||||||
cur_decl_jobs=/run/jenkins-job-builder/declarative-jobs
|
cur_decl_jobs=/run/jenkins-job-builder/declarative-jobs
|
||||||
rm -f "$cur_decl_jobs"
|
rm -f "$cur_decl_jobs"
|
||||||
@ -172,27 +208,27 @@ in {
|
|||||||
# Create / update jobs
|
# Create / update jobs
|
||||||
mkdir -p ${jobBuilderOutputDir}
|
mkdir -p ${jobBuilderOutputDir}
|
||||||
for inputFile in ${yamlJobsFile} ${concatStringsSep " " jsonJobsFiles}; do
|
for inputFile in ${yamlJobsFile} ${concatStringsSep " " jsonJobsFiles}; do
|
||||||
HOME="${jenkinsCfg.home}" "${pkgs.jenkins-job-builder}/bin/jenkins-jobs" --ignore-cache test -o "${jobBuilderOutputDir}" "$inputFile"
|
HOME="${jenkinsCfg.home}" "${pkgs.jenkins-job-builder}/bin/jenkins-jobs" --ignore-cache test --config-xml -o "${jobBuilderOutputDir}" "$inputFile"
|
||||||
done
|
done
|
||||||
|
|
||||||
for file in "${jobBuilderOutputDir}/"*; do
|
find "${jobBuilderOutputDir}" -type f -name config.xml | while read -r f; do echo "$(dirname "$f")"; done | sort | while read -r dir; do
|
||||||
test -f "$file" || continue
|
jobname="$(realpath --relative-to="${jobBuilderOutputDir}" "$dir")"
|
||||||
jobname="$(basename $file)"
|
jenkinsjobname=$(getJenkinsJobDir "$jobname")
|
||||||
jobdir="${jenkinsCfg.home}/jobs/$jobname"
|
jenkinsjobdir="${jenkinsCfg.home}/$jenkinsjobname"
|
||||||
echo "Creating / updating job \"$jobname\""
|
echo "Creating / updating job \"$jobname\""
|
||||||
mkdir -p "$jobdir"
|
mkdir -p "$jenkinsjobdir"
|
||||||
touch "$jobdir/${ownerStamp}"
|
touch "$jenkinsjobdir/${ownerStamp}"
|
||||||
cp "$file" "$jobdir/config.xml"
|
cp "$dir"/config.xml "$jenkinsjobdir/config.xml"
|
||||||
echo "$jobname" >> "$cur_decl_jobs"
|
echo "$jenkinsjobname" >> "$cur_decl_jobs"
|
||||||
done
|
done
|
||||||
|
|
||||||
# Remove stale jobs
|
# Remove stale jobs
|
||||||
for file in "${jenkinsCfg.home}"/jobs/*/${ownerStamp}; do
|
find "${jenkinsCfg.home}" -type f -name "${ownerStamp}" | while read -r f; do echo "$(dirname "$f")"; done | sort --reverse | while read -r dir; do
|
||||||
test -f "$file" || continue
|
jenkinsjobname="$(realpath --relative-to="${jenkinsCfg.home}" "$dir")"
|
||||||
jobdir="$(dirname $file)"
|
grep --quiet --line-regexp "$jenkinsjobname" "$cur_decl_jobs" 2>/dev/null && continue
|
||||||
jobname="$(basename "$jobdir")"
|
jobname=$(getJobname "$jenkinsjobname")
|
||||||
grep --quiet --line-regexp "$jobname" "$cur_decl_jobs" 2>/dev/null && continue
|
|
||||||
echo "Deleting stale job \"$jobname\""
|
echo "Deleting stale job \"$jobname\""
|
||||||
|
jobdir="${jenkinsCfg.home}/$jenkinsjobname"
|
||||||
rm -rf "$jobdir"
|
rm -rf "$jobdir"
|
||||||
done
|
done
|
||||||
'' + (if cfg.accessUser != "" then reloadScript else "");
|
'' + (if cfg.accessUser != "" then reloadScript else "");
|
||||||
|
Loading…
Reference in New Issue
Block a user