Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to install one or more jenkins plugins including dependencies while jenkins is offline
#!/usr/bin/env bash
set -e
set -o pipefail
plugin_repo_url="http://updates.jenkins-ci.org/download/plugins"
plugin_dir="/var/lib/jenkins/plugins"
include_optionals=false
showUsage() {
echo "\
$0 [OPTIONS] plugin@version ...
OPTIONS:
-d,--dir DIR Install dir. Default is $plugin_dir
-a,--all Install also optional dependencies
-u,--url URL Change to plugin repo URL. Default is $plugin_repo_url
-h,--help Print this help"
}
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-u|--url)
plugin_repo_url="$2"
shift
;;
-d|--dir)
plugin_dir="$2"
shift
;;
-a|--all)
include_optionals=true
;;
-h|--help)
showUsage
exit
;;
-*|--*)
echo "Unknown Option"
exit 1
;;
*)
break
;;
esac
shift
done
download_plugin() {
url="${plugin_repo_url}/${1}/${2}/${1}.hpi"
echo "Downloading: $1@$2"
curl -L --silent --output "${plugin_dir}/${1}.hpi" "$url"
}
get_dependencies() {
hpi_file=$1
manifest="$(unzip -p "${hpi_file}" META-INF/MANIFEST.MF | tr -d '\r' | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n //g' )"
if line=$( echo "$manifest" | grep -e "^Plugin-Dependencies" ); then
deps=$( echo "$line" | awk '{ print $2 }' | tr ',' '\n' )
if ! $include_optionals; then
deps=$( echo "$deps" | grep -v "resolution:=optional" )
fi
sed 's/;.*$//' <<< "$deps"
else
echo ""
fi
}
vercomp () {
if [[ "$1" == "$2" ]]
then
echo 0
return
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
echo 1
return
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
echo -1
return
fi
done
echo 0
}
installed_plugins=()
install_plugin() {
plugin_id="$1"
plugin_version="$2"
if grep -q "$plugin_id" <<< "$installed_plugins[*]"; then
return 0
fi
dest="${plugin_dir}/${1}.hpi"
if [ -f "$dest" ]; then
installed_version=$(unzip -p "${dest}" META-INF/MANIFEST.MF | tr -d '\r' | grep "^Plugin-Version" | awk '{ print $2 }' | tr -d '\n' )
if [ "$(vercomp "$installed_version" "$plugin_version")" -lt "0" ]; then
echo "Updating $plugin_id from $installed_version to $plugin_version"
download_plugin "$plugin_id" "$plugin_version" "$plugin_dir"
fi
else
download_plugin "$plugin_id" "$plugin_version" "$plugin_dir"
fi
installed_plugins+=("$plugin_id")
deps="$(get_dependencies "$dest")"
echo "$deps" | tr ' ' '\n' |
while IFS=: read -r plugin version; do
install_plugin "$plugin" "$version"
done
}
for plugin in "$@"; do
IFS="@" read -r plugin version <<< "$plugin"
#escape comments
if [[ "$plugin" =~ ^# ]]; then
continue
fi
#install the plugin
install_plugin "$plugin" "$version"
done
echo "Done"
@hoesler

This comment has been minimized.

Copy link
Owner Author

@hoesler hoesler commented May 12, 2017

Usage:

./install_jenkins_plugin.sh -d ./plugins -a git@3.3.0 xunit@1.102
@batwicket

This comment has been minimized.

Copy link

@batwicket batwicket commented May 19, 2017

FROM bitnami/jenkins:2

USER root

RUN sudo apt-get update && apt-get install bash zip

ADD install_jenkins_plugins.sh /usr/local/bin/
RUN chmod ugo+x /usr/local/bin/install_jenkins_plugins.sh && mkdir ./plugins && bash /usr/local/bin/install_jenkins_plugins.sh -d ./plugins -a git@3.3.0 xunit@1.102


I find that it can freeze during install of some of the plugins, perhaps due to a dependency issue. Thx for script.

@hoesler

This comment has been minimized.

Copy link
Owner Author

@hoesler hoesler commented May 22, 2017

This works fine for me:

FROM bitnami/jenkins:2

USER root

RUN sudo apt-get update && apt-get install bash zip

RUN curl -O https://gist.githubusercontent.com/hoesler/ed289c9c7f18190b2411e3f2286e23c3/raw/f6da774fe06eaf0e761133618d37968b5a3e5f27/install_jenkins_plugin.sh \
	&& chmod a+x install_jenkins_plugin.sh
RUN mkdir ./plugins \
	&& ./install_jenkins_plugin.sh -d ./plugins -a git@3.3.0 xunit@1.102
@FlowsenAusMonotown

This comment has been minimized.

Copy link

@FlowsenAusMonotown FlowsenAusMonotown commented Jul 7, 2017

It is not possible to use latest version here:
E.g.: ./installPlugins -d /var/jenkins/plugins/ -a greenballs@latest

Error is:
./installPlugins: line 94: 10#1 > 10#latest: value too great for base (error token is "10#latest")
./installPlugins: line 99: 10#1 < 10#latest: value too great for base (error token is "10#latest")

@sderidder-starz

This comment has been minimized.

Copy link

@sderidder-starz sderidder-starz commented Jul 20, 2017

@FlowsenAusMonotown If you always want the latest version of plugins I would think you could change the plugin_repo_url to https://updates.jenkins-ci.org/latest/

@mohammedna

This comment has been minimized.

Copy link

@mohammedna mohammedna commented Aug 1, 2017

Hi,
Can I update the plugins already exit by tweaking the script above? Thanks

@Eldadc

This comment has been minimized.

Copy link

@Eldadc Eldadc commented Aug 30, 2017

Do you support pulling from an offline folder with the hpi files ?
Thanks

@Eldadc

This comment has been minimized.

Copy link

@Eldadc Eldadc commented Sep 5, 2017

Does not operate , It copy files to plugin folder but the plugin was not installed in Jenkins,

@hoesler

This comment has been minimized.

Copy link
Owner Author

@hoesler hoesler commented Sep 9, 2017

Sorry for the silence. It seems that I didn't got notified of your comments.
@FlowsenAusMonotown No, latest is not supported. But it should be easy to add this feature. Maybe you want to try this script instead: https://github.com/jenkinsci/docker/blob/master/install-plugins.sh
@Eldadc This script, against it's name, does not really "install" the plugins. It just resolves dependencies and downloads. Downloading the hpi files to the jenkins plugin folder, however, should be sufficient.

@nhojpatrick

This comment has been minimized.

Copy link

@nhojpatrick nhojpatrick commented Dec 17, 2017

@hoesler @FlowsenAusMonotown i've done a fork that handles @latest
also fixes a looping issue if you try a plugin like artifactory@2.13.1

it uses the plugin version from the downloaded hpi
skips version upgrade check is version is latest
it stores plugin@version instead of just plugin so it short circuits when working out if i needs to download a dependency, also so it knows what it's already downloaded so doesn't look at it again

@mur-me

This comment has been minimized.

Copy link

@mur-me mur-me commented May 15, 2019

Man, great thank you for your work and saving my time, I faced task with installation of certain versions of plugins and it was painful, but your work saved me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment