From 51668e06a4274987367f7f31ab3cecedc34b19e7 Mon Sep 17 00:00:00 2001 From: Fredrik Andersson Date: Thu, 7 Apr 2016 10:44:52 +0200 Subject: [PATCH] Add support for CloudBees Folder plugin Backup jobs recursivly by stepping into all folders. --- jenkins-backup.sh | 26 ++++++++++++--- .../jobs/example_folder/config.xml | 32 +++++++++++++++++++ .../jobs/example_job_in_folder/config.xml | 3 ++ .../jobs/example_folder/config.xml | 32 +++++++++++++++++++ .../jobs/example_job_in_folder/config.xml | 3 ++ test/test_jenkins-backup.rb | 8 +++++ 6 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 test/jenkins_home1/jobs/example_folder/config.xml create mode 100644 test/jenkins_home1/jobs/example_folder/jobs/example_job_in_folder/config.xml create mode 100644 test/jenkins_home2/jobs/example_folder/config.xml create mode 100644 test/jenkins_home2/jobs/example_folder/jobs/example_job_in_folder/config.xml diff --git a/jenkins-backup.sh b/jenkins-backup.sh index 0ea1db1..7a5ebbb 100755 --- a/jenkins-backup.sh +++ b/jenkins-backup.sh @@ -43,13 +43,29 @@ if [ "$(ls -A $JENKINS_HOME/nodes/)" ] ; then cp -R "$JENKINS_HOME/nodes/"* "$ARC_DIR/nodes" fi -if [ "$(ls -A $JENKINS_HOME/jobs/)" ] ; then - cd "$JENKINS_HOME/jobs/" - ls -1 | while read job_name ; do - mkdir -p "$ARC_DIR/jobs/$job_name/" - find "$JENKINS_HOME/jobs/$job_name/" -maxdepth 1 -name "*.xml" | xargs -I {} cp {} "$ARC_DIR/jobs/$job_name/" +function backup_jobs { + local run_in_path=$1 + local rel_depth=${run_in_path#$JENKINS_HOME/jobs/} + cd $run_in_path + find . -maxdepth 1 -type d | while read job_name ; do + [ "$job_name" = "." ] && continue + [ "$job_name" = ".." ] && continue + [ -d $JENKINS_HOME/jobs/$rel_depth/$job_name ] && mkdir -p "$ARC_DIR/jobs/$rel_depth/$job_name/" + find "$JENKINS_HOME/jobs/$rel_depth/$job_name/" -maxdepth 1 -name "*.xml" | xargs -I {} cp {} "$ARC_DIR/jobs/$rel_depth/$job_name/" + if [ -f $JENKINS_HOME/jobs/$rel_depth/$job_name/config.xml ] && [ "$(grep -c "com.cloudbees.hudson.plugins.folder.Folder" $JENKINS_HOME/jobs/$rel_depth/$job_name/config.xml)" -ge 1 ] ; then + #echo "Folder! $JENKINS_HOME/jobs/$rel_depth/$job_name/jobs" + backup_jobs $JENKINS_HOME/jobs/$rel_depth/$job_name/jobs + else + true + #echo "Job! $JENKINS_HOME/jobs/$rel_depth/$job_name" + fi done + #echo "Done in $(pwd)" cd - +} + +if [ "$(ls -A $JENKINS_HOME/jobs/)" ] ; then + backup_jobs $JENKINS_HOME/jobs/ fi cd "$TMP_DIR" diff --git a/test/jenkins_home1/jobs/example_folder/config.xml b/test/jenkins_home1/jobs/example_folder/config.xml new file mode 100644 index 0000000..4647175 --- /dev/null +++ b/test/jenkins_home1/jobs/example_folder/config.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + All + false + false + + + + + + + + + + diff --git a/test/jenkins_home1/jobs/example_folder/jobs/example_job_in_folder/config.xml b/test/jenkins_home1/jobs/example_folder/jobs/example_job_in_folder/config.xml new file mode 100644 index 0000000..7bd2cb7 --- /dev/null +++ b/test/jenkins_home1/jobs/example_folder/jobs/example_job_in_folder/config.xml @@ -0,0 +1,3 @@ + + + diff --git a/test/jenkins_home2/jobs/example_folder/config.xml b/test/jenkins_home2/jobs/example_folder/config.xml new file mode 100644 index 0000000..4647175 --- /dev/null +++ b/test/jenkins_home2/jobs/example_folder/config.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + All + false + false + + + + + + + + + + diff --git a/test/jenkins_home2/jobs/example_folder/jobs/example_job_in_folder/config.xml b/test/jenkins_home2/jobs/example_folder/jobs/example_job_in_folder/config.xml new file mode 100644 index 0000000..7bd2cb7 --- /dev/null +++ b/test/jenkins_home2/jobs/example_folder/jobs/example_job_in_folder/config.xml @@ -0,0 +1,3 @@ + + + diff --git a/test/test_jenkins-backup.rb b/test/test_jenkins-backup.rb index 989efeb..0bf6715 100644 --- a/test/test_jenkins-backup.rb +++ b/test/test_jenkins-backup.rb @@ -35,6 +35,10 @@ def expected_files_without_pinned jenkins-backup/jobs/ jenkins-backup/jobs/example_job/ jenkins-backup/jobs/example_job/config.xml + jenkins-backup/jobs/example_folder/ + jenkins-backup/jobs/example_folder/config.xml + jenkins-backup/jobs/example_folder/jobs/example_job_in_folder/ + jenkins-backup/jobs/example_folder/jobs/example_job_in_folder/config.xml jenkins-backup/nodes/ jenkins-backup/nodes/slave/ jenkins-backup/nodes/slave/config.xml @@ -55,6 +59,10 @@ def expected_files_with_pinned jenkins-backup/jobs/ jenkins-backup/jobs/example_job/ jenkins-backup/jobs/example_job/config.xml + jenkins-backup/jobs/example_folder/ + jenkins-backup/jobs/example_folder/config.xml + jenkins-backup/jobs/example_folder/jobs/example_job_in_folder/ + jenkins-backup/jobs/example_folder/jobs/example_job_in_folder/config.xml jenkins-backup/nodes/ jenkins-backup/nodes/slave/ jenkins-backup/nodes/slave/config.xml