scripts/app/jenkins_update_plugins

61 lines
2.3 KiB
Plaintext
Raw Permalink 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
2021-07-07 12:42:13 +02:00
rm --force -- "${JENKINS_PLUGIN_LIST_FILE}"
2018-05-04 16:28:47 +02:00
fi
# Move to the directory where authentication file is stored
cd "$(dirname ${JENKINS_AUTH_FILE})" || exit
# Verify if updates are available for plugins
2021-07-07 12:42:13 +02:00
if java -jar "${JENKINS_CLI_JAR}" -s http://127.0.0.1:8080 -auth @.jenkins.auth list-plugins | grep --ignore-case --quiet -- "(.*)$"
2018-05-04 16:28:47 +02:00
then
# Store the list of plugins to upgrade in a file, one by line
2021-07-07 12:42:13 +02:00
java -jar "${JENKINS_CLI_JAR}" -s http://127.0.0.1:8080 -auth @.jenkins.auth list-plugins | grep --ignore-case -- "(.*)$" | cut --delimiter=" " --fields=1 > "${JENKINS_PLUGIN_LIST_FILE}"
2018-05-04 16:28:47 +02:00
# 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
2021-07-07 12:42:13 +02:00
while ps axg | grep --invert-match --word-regexp grep | grep --word-regexp "install-plugin ${plugin}" > /dev/null; do sleep 1; done
2018-05-04 16:28:47 +02:00
# 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
2021-07-07 12:42:13 +02:00
rm --force -- "${JENKINS_PLUGIN_UPDATE_LOG}"
2018-05-04 16:28:47 +02:00
exit 0