From df3bad3dddc9c99d8701912246a1768ce1cf9457 Mon Sep 17 00:00:00 2001 From: David Galloway Date: Tue, 3 Oct 2017 15:09:01 -0400 Subject: [PATCH] jenkins-job-builder: Delete stale jobs Signed-off-by: David Galloway --- jenkins-job-builder/build/build | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/jenkins-job-builder/build/build b/jenkins-job-builder/build/build index 26cad72a..50675219 100644 --- a/jenkins-job-builder/build/build +++ b/jenkins-job-builder/build/build @@ -9,7 +9,7 @@ set -ex # the following two methods exist in scripts/build_utils.sh -pkgs=( "jenkins-job-builder==2.0.0.0b2" ) +pkgs=( "jenkins-job-builder==2.0.0.0b2" "jq" ) install_python_packages "pkgs[@]" # Wipe out JJB's cache if $FORCE is set. @@ -28,6 +28,9 @@ password=$JOB_BUILDER_PASS url=$JENKINS_URL EOF +# Make a temp dir to store job configs created using `jenkins-jobs test` +TEMPDIR=$(mktemp -d) + # Test every definition if available in the current repository and update the jobs # if they do define one (they should always define their definitions) for dir in `find . -maxdepth 1 -path ./.git -prune -o -type d -print`; do @@ -49,7 +52,7 @@ for dir in `find . -maxdepth 1 -path ./.git -prune -o -type d -print`; do # that started it. This prevents collisions. if [[ "$JJB_CONFIG" == "$HOME/.jenkins_jobs.$JENKINS_FQDN.ini" ]]; then # Test the definitions first - $VENV/jenkins-jobs --log_level DEBUG --conf $JJB_CONFIG test $definitions_dir -o /tmp/output + $VENV/jenkins-jobs --log_level DEBUG --conf $JJB_CONFIG test $definitions_dir -o $TEMPDIR # Update Jenkins with the output if they passed the test phase # Note that this needs proper permissions with the right credentials to the @@ -58,3 +61,12 @@ for dir in `find . -maxdepth 1 -path ./.git -prune -o -type d -print`; do fi fi done + +# Delete jobs our master has that didn't get job xml written during `jenkins-jobs test` +# jenkins-job-builder doesn't get a config written so we `grep -v it` so it doesn't get deleted. +for JOB in $(curl -s https://$JENKINS_FQDN/api/json | jq -r '.jobs[].name' | grep -v jenkins-job-builder | sort); do + if [ ! -f $TEMPDIR/$JOB ]; then + echo "Did not find job definition for $JOB. Deleting!" + $VENV/jenkins-jobs --log_level DEBUG --conf $JJB_CONFIG delete $JOB + fi +done -- 2.39.5