-
-
Save micw/e80d739c6099078ce0f3 to your computer and use it in GitHub Desktop.
#!/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" | |
curl -L --silent --output ${plugin_dir}/${1}.hpi https://updates.jenkins-ci.org/latest/${1}.hpi | |
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' ' ' ) | |
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" |
I tries using jenkins rst api but getting issues.
Command tried -:
curl -X POST -data "" --header 'Content-Type: text/xml' http://localhost:8080/pluginManager/installNecessaryPlugins
ERROR -:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: latest'
100 390 100 387 100 3 24187 187 --:--:-- --:--:-- --:--:-- 24187
HTTP ERROR 403
Problem accessing /pluginManager/installNecessaryPlugins. Reason:
No valid crumb was included in the request
Powered by Jetty://
Do you support pulling from an offline folder with the hpi files ?
Thanks
Does not operate , It copy files to plugin folder but the plugin was not installed in Jenkins,
It didn't work. The script downloads, but the plugins are not installed.
See the comment on another gist with how to get the crumb, https://gist.github.com/basmussen/8182784#gistcomment-2261919
Anyway to know if this script can add bitbucket? If so, kindly advise.
Awesome +1.
I installed few plugins using GUI. Am seeing slack.jpi file and slack directory in JENKINS_HOME/plugins directory. But originally the installation is failed due to dependency.
Is there any way to check whether the .hpi is valid or installed correctly before trying this script?
Awesome,
is there any script to check plugins that are installed
@micw Could you be convinced to specify a license on this script? Maybe just an MIT or similar? Without some form of license, it would be all rights reserved, and even the many existing forks are strictly speaking in violation of your copyright, probably even if those forks that are unmodified. Also, no one would be allowed to actually use your really nice script or any of the forks out there.
If you need inspiration, I often find https://choosealicense.com/ very useful.
Awesome,
is there any script to check plugins that are installed
There are multiple ways to list your Jenkins Plugins:
Way 1: Script console(groovy code):
go to http:<Jenkins_URl>/script
Run below command:
Jenkins.instance.pluginManager.plugins.each{
plugin ->
println ("${plugin.getShortName()}")
}
Way 2: Command CLI
Download command-CLI jar from : http://<Jenkins_URL>/jnlpJars/jenkins-cli.jar
Run below command:
java -jar jenkins-cli.jar -s http://<JENKINS_URL>/ list-plugins --username "<Jenkins_USERNAME>" --password "<Jenkins_Password>"
Way 3: Jenkins Python API
import jenkins
import json
server = jenkins.Jenkins('<jenkinsurl>', username='<Jenkins_user>', password='<Jenkins_User_password>')
all_plugins_info=server.get_plugins_info()
for each_plugin in all_plugins_info:
print each_plugin['shortName']
if you are using a file that contains the name of a plugin on each line you can use this command with the above script
./install_jenkins_plugin.sh $(echo $(cat plugins.txt))
For whoever searches for this, make sure you try jenkins rest api first
curl -X POST \ --data "<jenkins><install plugin='${name}@latest' /></jenkins>" \ --header 'Content-Type: text/xml' \ http://localhost:8080/pluginManager/installNecessaryPlugins
You should update your example to include the Crumb Issuer Header as well.
Jenkins' docker build has a script for this: https://github.com/jenkinsci/docker/blob/master/install-plugins.sh
Published an improved version, if anyone is interested:
https://gist.github.com/hoesler/ed289c9c7f18190b2411e3f2286e23c3