Skip to content

Instantly share code, notes, and snippets.

@nhojpatrick
Forked from micw/install_jenkins_plugin.sh
Last active February 21, 2021 14:09
Show Gist options
  • Save nhojpatrick/224248b30fe2f03792ff39597170f25b to your computer and use it in GitHub Desktop.
Save nhojpatrick/224248b30fe2f03792ff39597170f25b to your computer and use it in GitHub Desktop.
Script to install one or more jenkins plugins including dependencies while jenkins is offline
#!/bin/bash
set -e
if [ $# -eq 0 ]; then
echo "USAGE: $0 plugin1 plugin2 ..."
exit 1
fi
plugin_dir=/var/lib/jenkins/plugins
file_owner=jenkins.jenkins
mkdir -p /var/lib/jenkins/plugins
installPlugin() {
if [ -f ${plugin_dir}/${1}.hpi -o -f ${plugin_dir}/${1}.jpi ]; then
if [ "$2" == "1" ]; then
return 1
fi
echo "Skipped: $1 (already installed)"
return 0
else
echo "Installing: $1"
# checks http response code
status_code=$(curl -L --silent --write-out %{http_code} --output ${plugin_dir}/${1}.hpi https://updates.jenkins-ci.org/latest/${1}.hpi );
if [[ "${status_code}" -ne 200 && "${status_code}" -ne 301 && "${status_code}" -ne 302 ]]; then
echo "status_code=${status_code} for https://updates.jenkins-ci.org/latest/${1}.hpi"
exit -1;
fi
return 0
fi
}
for plugin in $*
do
installPlugin "$plugin"
done
changed=1
maxloops=100
while [ "$changed" == "1" ]; do
echo "Check for missing dependecies ..."
if [ $maxloops -lt 1 ] ; then
echo "Max loop count reached - probably a bug in this script: $0"
exit 1
fi
((maxloops--))
changed=0
for f in ${plugin_dir}/*.hpi ; do
# without optionals
#deps=$( unzip -p ${f} META-INF/MANIFEST.MF | tr -d '\r' | sed -e ':a;N;$!ba;s/\n //g' | grep -e "^Plugin-Dependencies: " | awk '{ print $2 }' | tr ',' '\n' | grep -v "resolution:=optional" | awk -F ':' '{ print $1 }' | tr '\n' ' ' )
# with optionals
#deps=$( unzip -p ${f} META-INF/MANIFEST.MF | tr -d '\r' | sed -e ':a;N;$!ba;s/\n //g' | grep -e "^Plugin-Dependencies: " | awk '{ print $2 }' | tr ',' '\n' | awk -F ':' '{ print $1 }' | tr '\n' ' ' )
# fixes issues noticed dur to line wrapping, either ending at 80 characters or last line has just '1.16'
deps=$( unzip -p ${f} META-INF/MANIFEST.MF | grep -A1 -e "[a-z-]:[0-9]" | grep -E "^ |^Plugin\-Dependencies" | tr -d "\r\n " | sed "s/Plugin-Dependencies://" | tr ',' '\n' | awk -F ':' '{ print $1 }' )
for plugin in $deps; do
installPlugin "$plugin" 1 && changed=1
done
done
done
echo "fixing permissions"
chown ${file_owner} ${plugin_dir} -R
echo "all done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment