scripts/jenkins_update_plugins

61 lines
2.2 KiB
Plaintext
Raw Normal View History

2018-05-04 16:28:47 +02:00
#!/bin/sh
# This script do the following:
# Use jenkins-cli.jar
# Verify if an update is available for any Jenkins plugins.
# Update the plugins.
# Restart Jenkins.
#
# How-to use it?
# Run it with `jenkins_update_plugins
JENKINS_HOME="/var/lib/jenkins"
JENKINS_CLI_JAR="${JENKINS_HOME}/war/WEB-INF/jenkins-cli.jar"
JENKINS_AUTH_FILE="/root/.jenkins.auth"
JENKINS_PLUGIN_LIST_FILE="${JENKINS_HOME}/updates/.plugin_list.$(date +%Y%m%d)"
JENKINS_PLUGIN_UPDATE_LOG="${JENKINS_HOME}/.update_plugin"
2018-05-04 16:28:47 +02:00
if [ ! -f "${JENKINS_CLI_JAR}" ]; then
printf '%b\n' "Jenkins-cli (${JENKINS_CLI_JAR}) does not seem to be available."
exit 1
fi
if [ ! -f "${JENKINS_AUTH_FILE}" ]; then
printf '%b\n' "Jenkins authentication file (${JENKINS_AUTH_FILE}) does not seem to be available."
exit 1
fi
if [ -f "${JENKINS_PLUGIN_LIST_FILE}" ]; then
rm -f -- "${JENKINS_PLUGIN_LIST_FILE}"
fi
# Move to the directory where authentication file is stored
cd "$(dirname ${JENKINS_AUTH_FILE})" || exit
# Verify if updates are available for plugins
if java -jar "${JENKINS_CLI_JAR}" -s http://127.0.0.1:8080 -auth @.jenkins.auth list-plugins | grep -i -q -- "(.*)$"
then
# Store the list of plugins to upgrade in a file, one by line
java -jar "${JENKINS_CLI_JAR}" -s http://127.0.0.1:8080 -auth @.jenkins.auth list-plugins | grep -i -- "(.*)$" | cut -d" " -f1 > "${JENKINS_PLUGIN_LIST_FILE}"
# Upgrade plugins one by one
while IFS= read -r plugin; do
#printf '%b\n' "Upgrading: ${plugin}"
java -jar "${JENKINS_CLI_JAR}" -s http://127.0.0.1:8080 -auth @.jenkins.auth install-plugin "${plugin}" > /dev/null &
# Wait for the end of the upgrade
while ps axg | grep -vw grep | grep -w "install-plugin ${plugin}" > /dev/null; do sleep 1; done
# With pgrep, it still runs 4~5 java commands at the same time
#while pgrep -f "install-plugin ${plugin}" > /dev/null; do sleep 1; done
done < "${JENKINS_PLUGIN_LIST_FILE}"
# Restart Jenkins
java -jar "${JENKINS_CLI_JAR}" -s http://127.0.0.1:8080 -auth @.jenkins.auth restart
#else
#printf '%b\n' "Nothing to do."
fi
cd - > /dev/null || exit
# Purge "log" files older than 30 days
find "${JENKINS_HOME}/updates" -iname ".plugin_list*" -mtime +30 -delete
rm -f -- "${JENKINS_PLUGIN_UPDATE_LOG}"
2018-05-04 16:28:47 +02:00
exit 0