From 3dc74ad15fb69d902e98d0dd286a6e8362622a3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gardais=20J=C3=A9r=C3=A9my?= Date: Fri, 4 May 2018 16:28:47 +0200 Subject: [PATCH] Script to upgrade Jenkins's plugins --- README.md | 6 +++++ jenkins_update_plugins | 58 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100755 jenkins_update_plugins diff --git a/README.md b/README.md index 601d86b..6273b23 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/jenkins_update_plugins b/jenkins_update_plugins new file mode 100755 index 0000000..a5ca708 --- /dev/null +++ b/jenkins_update_plugins @@ -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