Skip to content

Instantly share code, notes, and snippets.

@hoesler
Forked from micw/install_jenkins_plugin.sh
Last active February 9, 2024 16:28
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save hoesler/ed289c9c7f18190b2411e3f2286e23c3 to your computer and use it in GitHub Desktop.
Save hoesler/ed289c9c7f18190b2411e3f2286e23c3 to your computer and use it in GitHub Desktop.
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"
@sderidder-starz
Copy link

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
Copy link

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

@Eldadc
Copy link

Eldadc commented Aug 30, 2017

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

@Eldadc
Copy link

Eldadc commented Sep 5, 2017

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

@hoesler
Copy link
Author

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
Copy link

@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
Copy link

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