Script to upgrade Jenkins's plugins

This commit is contained in:
Jeremy Gardais 2018-05-04 16:28:47 +02:00
parent f2211f9580
commit 3dc74ad15f
2 changed files with 64 additions and 0 deletions

View File

@ -89,6 +89,12 @@ A daily cron to check [Jenkins][jenkins website]'s plugins updates.
* If an update is available, it will create an empty file (${JENKINS_HOME}/.update_plugin), else it will ensure the "update" file is absent.
* Be sure to monitore if this file **${JENKINS_HOME}/.update_plugin** exist!
#### jenkins_update_plugins
A script to upgrade Jenkins's plugins.
* It need [jenkins-cli.jar][jenkins cli].
* Can also be set in cron.daily to get automatic upgrades.
[jenkins website]: https://jenkins.io/
[jenkins cli]: https://jenkins.io/doc/book/managing/cli/#downloading-the-client

58
jenkins_update_plugins Executable file
View File

@ -0,0 +1,58 @@
#!/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)"
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
exit 0