Skip to content

Instantly share code, notes, and snippets.

@tfennelly
Created November 15, 2016 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tfennelly/c53953b98850304deb405591063a6e85 to your computer and use it in GitHub Desktop.
Save tfennelly/c53953b98850304deb405591063a6e85 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
HOST=""
LOGIN=""
JOB=""
CLONE_COUNT=""
for i in "$@"
do
case $i in
--host=*)
HOST="${i#*=}"
;;
--login=*)
LOGIN="${i#*=}"
;;
--job=*)
JOB="${i#*=}"
;;
--count=*)
CLONE_COUNT=${i#*=}
;;
esac
done
echo ""
if [ "${HOST}" == "" ]; then
HOST="localhost:8080/jenkins"
echo "No host specified. Defaulting to '${HOST}'."
echo " Override example: --host=localhost:9090/jenkins"
echo ""
fi
if [ "${LOGIN}" == "" ]; then
JENKINS_URL="http://${HOST}"
echo "No user login specified."
echo " Override example: --login=tfennelly:mypassword"
echo ""
else
JENKINS_URL="http://${LOGIN}@${HOST}"
fi
if [ "${JOB}" == "" ]; then
echo ""
echo " *********************************************************************"
echo " You must specify the name of an existing job on the Jenkins host."
echo " e.g. --job=pipelinexyz"
echo " *********************************************************************"
echo ""
exit 1
fi
if [ "${CLONE_COUNT}" == "" ]; then
CLONE_COUNT=20
echo "No job clone count specified. Defaulting to ${CLONE_COUNT}."
echo " Override example: --count=50"
echo ""
fi
# Get the crumb. I'm sure there's fancier ways of removing the XML tags.
CRUMB_FIELD=$(curl "${JENKINS_URL}/crumbIssuer/api/xml?xpath=//crumbRequestField" -s)
CRUMB_FIELD="${CRUMB_FIELD/\<crumbRequestField\>/}"
CRUMB_FIELD="${CRUMB_FIELD/\<\/crumbRequestField\>/}"
CRUMB_VAL=$(curl "${JENKINS_URL}/crumbIssuer/api/xml?xpath=//crumb" -s)
CRUMB_VAL="${CRUMB_VAL/\<crumb\>/}"
CRUMB_VAL="${CRUMB_VAL/\<\/crumb\>/}"
echo "Creating ${CLONE_COUNT} copies of Job '${JOB}' on ${JENKINS_URL}"
echo ""
LOCAL_FILE_NAME=.job-clone-config.xml
# copy the job from Jenkins
curl -X GET $JENKINS_URL/job/${JOB}/config.xml -o $LOCAL_FILE_NAME -s
for (( i=1; i<=$CLONE_COUNT; i++ ))
do
NEW_JOB_NAME="${JOB}_${i}"
curl -s -XPOST "${JENKINS_URL}/createItem?name=${NEW_JOB_NAME}" --data-binary @$LOCAL_FILE_NAME -H "Content-Type:text/xml" -H "${CRUMB_FIELD}: ${CRUMB_VAL}"
done
rm $LOCAL_FILE_NAME
@michaelneale
Copy link

Mad shell scripting skillz there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment