SOLVED: Save Hudson configs to Subversion. Automatically.


Posted:   |  More posts about continuous integration howto hudson ideas software solved

I decided to save Hudson configs to Subversion repository just in case something goes wrong with the server later and would need to restore all projects. A bit of googling first, found that there are plugins to to a backup of workplaces and configs. But why would I need to have them? I am on Solaris, so I can create the same by my own hands. Here are my requirements:

  1. Backup should be a job within Hudson - since Hudson is a best cron you could find and I already have it
  2. Backup should automatically discover added jobs and add then into Subversion - thus I will forget about it forever, no maintenance required

Simples! So I created a custom build job within Hudson, pointed it to a Subversion location, where I will store backups and backup shell script, which will gather all Hudson configs and deal with Subversion. I told Hudson that I don't want to create subfolder for a project within its workplace. Then I set to execute script this way: ${WORKPLACE}/hudsonbackup.sh Here is the script itself:

#!/bin/bash

SRC_FOLDER=${WORKSPACE}

SVN_PARAMS="--config-dir /.subversion/ --non-interactive --trust-server-cert --username f_tc_ci1 --password *"

SVN_COMMAND="/opt/csw/bin/svn"

JOBS=ls -al /.hudson/jobs/|grep -v job1|grep -v job2|awk ' NR>3 {print $9}'

CURR_FOLDER=pwd

echo "Copying Hudson configs"

cp /.hudson/*.xml ${SRC_FOLDER}

echo "Done."

cd ${SRC_FOLDER}

echo "Processing Hudson jobs"

for JOB in ${JOBS}

do echo "Processing ${JOB}" if [ ! -e ${JOB} ] then echo "New job. Creating folder and adding it to SVN" mkdir ${JOB} ${SVN_COMMAND} add ${SVN_PARAMS} ${JOB}
SVN_COMMENT="${SVN_COMMENT} Added ${JOB}" fi

echo "Saving ${JOB}/config.xml" if [ -e ${JOB}/.svn ] then if [ -e ${JOB}/config.xml ] then # config.xml already exist - don't need to add it to subversion cp /.hudson/jobs/${JOB}/config.xml ${JOB}/ else echo "New job - new config.xml" cp /.hudson/jobs/${JOB}/config.xml ${JOB}/ ${SVN_COMMAND} add ${SVN_PARAMS} ${JOB}/config.xml fi else echo "Problem creating and adding folder to Subversion" exit 1 fi done

echo "Job processing is done. Committing..."

${SVN_COMMAND} ci ${SVN_PARAMS} -m "Config saved by Hudson. ${SVN_COMMENT}"

echo "All done."

Note 1: My Subversion is on HTTPS server with the self-signed certificate, so I have to tell Subversion client to trust it explicitly. Its on line 4. On line 5 I constructed the path to Subversion client because Hudson executing shell scripts with quite limited PATH. Note 2: I need to ignore some jobs, so I added them on line 6. Also on this line I have hardcoded Hudson location ("/.hudson") - it needs to be changed if your Hudson somewhere else. After all that I set this job to execute daily. It works!

Contents © 2013 Aleksey Maksimov - Powered by Nikola