Skip to content

Instantly share code, notes, and snippets.

@hagzag hagzag/jobGen.sh Secret
Created Jun 15, 2013

Embed
What would you like to do?
jobGen.sh
#!/bin/bash
set -e
[ "x${JENKINS_URL}" == "x" ] && JENKINS_URL=http://jeki:8080
JENKINS_USER=demo
JENKINS_PASS=qwe123
XML=`which xmlstarlet` || { echo "please install xmlstarlet and try again"; exit 2; }
isJavaAvail(){ # find java
if [ "x${JAVA_HOMa}" != "x" ]; then JAVA_CMD="${JAVA_HOME}/bin/java/"
else
for candidate in /etc/alternatives/java /usr/lib/jvm/java-1.6.0/bin/java /usr/lib/jvm/jre-1.6.0/bin/java /usr/lib/jvm/java-1.5.0/bin/java /usr/lib/jvm/jre-1.5.0/bin/java /usr/bin/java
do
[ -x "$JAVA_CMD" ] && break
JAVA_CMD="$candidate"
done
fi
[ -x "$JAVA_CMD" ] || { printf "[ $FUNCNAME ] no java found, exiting $0 \n"; exit 2; } && printf "[ $FUNCNAME ] Using java executable Found in: $JAVA_CMD \n"
}
execJenkinsCli(){ # excute jenkins-cli jar
jenkins_clinet_jar=./jenkins-cli.jar
# download only if it's newwer than the local version [ curl -z ], the cli jar should be ofthe smae jenkins version ...
curl -s -z ./jenkins-cli.jar ${JENKINS_URL}/jnlpJars/jenkins-cli.jar -o ${jenkins_clinet_jar} || { echo "Echo cannot obtain jenkins-cli.jar exiting"; exit 2;}
cli_cmd=$1
cmd_params=$2
cmd_prefix="${JAVA_CMD} -jar ${jenkins_clinet_jar} -s ${JENKINS_URL}"
${cmd_prefix} who-am-i | grep 'Authenticated as: anonymous' &>/dev/null && anon=0
#anon=$?
if [ "$anon" != "0" ]; then
#printf "[ $FUNCNAME ] using jenkins-cli with private key auth"
exec ${cmd_prefix} ${cli_cmd} ${cmd_params}
elif [ "x" = "x${JENKINS_USER}" ] || [ "x" = "x${JENKINS_PASS}" ]; then
printf "[ $FUNCNAME ] cannot determin jenkins credentials\n";
exit 3;
else
exec ${cmd_prefix} ${cli_cmd} ${cmd_params} --username ${JENKINS_USER} --password ${JENKINS_PASS}
fi
}
generateJob(){ # generates a jenkins job
if [ $# -lt 2 ] ; then
echo ""
exit 1
fi
new_job_name=${1}
tmpl_job_name=${2}
scm_url=${3}
execJenkinsCli get-job ${tmpl_job_name} | ${XML} ed -O -S -P -u '//project/scm/locations/hudson.scm.SubversionSCM_-ModuleLocation/remote' -v ${scm_url} | execJenkinsCli create-job ${new_job_name}
execJenkinsCli enable-job ${new_job_name}
}
isJavaAvail
generateJob "$@"
@jorgepenacotarelo

This comment has been minimized.

Copy link

commented Nov 14, 2013

This is supported only for subversion. What about mercurial?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.