Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 89 lines (65 sloc) 2.5 KB
#!/bin/bash
# Script to copy a dummy Jenkins job and build a new one.
# Takes a set of arguments to build the new job with relevant parameters
TO_PROJECT=$1
PROFILE=$2
AEGIRSERVER=$3
WEBSERVER=$4
DBSERVER=$5
MAKEFILE=$6
REPO=$7
BRANCH=$8
if [[ -z $TO_PROJECT ]] || [[ -z $PROFILE ]] || [[ -z $AEGIRSERVER ]] || [[ -z $WEBSERVER ]] || [[ -z $WEBSERVER ]] || [[ -z $DBSERVER ]] || [[ -z $MAKEFILE ]] || [[ -z $REPO ]] || [[ -z $BRANCH ]]
then
echo "Missing args!"
exit 1
fi
JENKINS_URL="http://example.com:8080"
USERNAME="jenkinsbot"
PASSWORD="changeme"
TMP_DIR="/tmp/jenkinsJobs"
CURL="curl --user $USERNAME:$PASSWORD -s "
FROM_PROJECT="Dummy_Deploy_Job"
FROM_CONFIG_URL="$JENKINS_URL/job/$FROM_PROJECT/config.xml"
TO_CONFIG_URL="$JENKINS_URL/job/$TO_PROJECT/config.xml"
FROM_CONFIG_XML="$TMP_DIR/$FROM_PROJECT-config.xml"
TO_CONFIG_XML="$TMP_DIR/$TO_PROJECT-config.xml"
# Make our temp dir
mkdir -p $TMP_DIR
# check for existing job
`$CURL -f -o /dev/null $TO_CONFIG_URL`
if [ $? -eq 0 ]; then
echo "WARN: $TO_PROJECT already exists on the jenkins server. No action taken."
exit 1
fi
# download the config.xml for the job, continue if it doesn't exist
`$CURL -o $FROM_CONFIG_XML $FROM_CONFIG_URL`
grep "Error 404" "$FROM_CONFIG_XML" > /dev/null
if [ $? -eq 0 ]; then
echo "FAIL: $FROM_CONFIG_URL is not a valid project url"
exit 1
fi
# replace some details
# Aegir server
sed "s/dummyaegirserver/$AEGIRSERVER/g" "$FROM_CONFIG_XML" > "$TO_CONFIG_XML"
# Profile
sed -i "s/dummyprofile/$PROFILE/g" $TO_CONFIG_XML
# Web server
sed -i "s/dummywebserver/$WEBSERVER/g" "$TO_CONFIG_XML"
# DB server
sed -i "s/dummydbserver/$DBSERVER/g" "$TO_CONFIG_XML"
# Makefile
sed -i "s/dummymakefile/$(echo $MAKEFILE | sed -e 's/\\/\\\\/g' -e 's/\//\\\//g' -e 's/&/\\\&/g')/g" "$TO_CONFIG_XML"
# Repo name
sed -i "s/git\@dummyrepo\.com\:dummy/$(echo $REPO | sed -e 's/\\/\\\\/g' -e 's/\//\\\//g' -e 's/&/\\\&/g')/g" "$TO_CONFIG_XML"
# Branch name
sed -i "s/dummybranch/$BRANCH/g" "$TO_CONFIG_XML"
# create a new job based on the existing job
`$CURL -o /dev/null --data " " "$JENKINS_URL/createItem?name=${TO_PROJECT}&mode=copy&from=${FROM_PROJECT}"`
# upload new job data
`$CURL -o /dev/null --data "@$TO_CONFIG_XML" "$JENKINS_URL/job/$TO_PROJECT/config.xml"`
# enable the job, there seems to be a bug where we need to first disable and then enable
`$CURL -o /dev/null --data disable "$JENKINS_URL/job/$TO_PROJECT/disable"`
`$CURL -o /dev/null --data enable "$JENKINS_URL/job/$TO_PROJECT/enable"`
# Remove our temp dir
rm -rf $TMP_DIR