Skip to content

Instantly share code, notes, and snippets.

@thara
Forked from hishida/copy_jenkins_jobs.sh
Last active July 8, 2016 07:40
Show Gist options
  • Save thara/6c077a2c65946a88a93fda8a08b4717a to your computer and use it in GitHub Desktop.
Save thara/6c077a2c65946a88a93fda8a08b4717a to your computer and use it in GitHub Desktop.
Copy all of Jenkins jobs that is included in certain directory.
#!/bin/bash
# ex: copy_jenkins_jobs.sh /tmp/jenkinsjobs
JENKINS_URL="http://localhost:8080"
AUTH="<username>:<API token>"
CONFIG_TMP_XML="config_tmp.xml"
DIR=$1
files="$DIR/*"
for filepath in $files; do
if [ -d $filepath ]; then
PROJECT=${filepath##*/}
# copy config file
cp $DIR/$PROJECT/config.xml $CONFIG_TMP_XML
# create a new job
curl -f -s -X POST -o /dev/null --user $AUTH --data-binary "@$CONFIG_TMP_XML" -H "Content-Type:text/xml" "$JENKINS_URL/createItem?name=${PROJECT}"
if [ $? -ne 0 ]; then
echo "WARN: $PROJECT already exists on the jenkins server. No action taken."
continue
fi
# disable job
curl -s -o /dev/null --user $AUTH --data disable "$JENKINS_URL/job/$PROJECT/disable"
# remove temporary config file
rm $CONFIG_TMP_XML
echo "SUCCESS: $PROJECT copied successfully."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment