-
-
Save tfennelly/c53953b98850304deb405591063a6e85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mad shell scripting skillz there