Skip to content

Instantly share code, notes, and snippets.

@rowan-m
Forked from jedi4ever/gist:898114
Created June 15, 2011 11:34
Show Gist options
  • Save rowan-m/1026918 to your computer and use it in GitHub Desktop.
Save rowan-m/1026918 to your computer and use it in GitHub Desktop.
update jenkins Updatecenter from CLI
$ java -jar jenkins-cli.jar -s http://localhost:9000 install-plugin findbugs
findbugs is neither a valid file, URL, nor a plugin artifact name in the update center
No update center data is retrieved yet from: http://updates.jenkins-ci.org/update-center.json
findbugs looks like a short plugin name. Did you mean 'null'?
# Specifying a full URL works!
$ java -jar jenkins-cli.jar -s http://localhost:9020 install-plugin http://updates.jenkins-ci.org/download/plugins/AdaptivePlugin/0.1/AdaptivePlugin.hpi
# Get the update center ourself
$ wget -O default.js http://updates.jenkins-ci.org/update-center.json
# remove first and last line javascript wrapper
sed '1d;$d' default.js > default.json
# Now push it to the update URL
curl -X POST -H "Accept: application/json" -d @default.json http://localhost:9020/updateCenter/byId/default/postBack --verbose
* About to connect() to localhost port 9020 (#0)
* Trying ::1... connected
* Connected to localhost (::1) port 9020 (#0)
> POST /updateCenter/byId/default/postBack HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3
> Host: localhost:9020
> Accept: application/json
> Content-Length: 253822
> Content-Type: application/x-www-form-urlencoded
> Expect: 100-continue
>
* Done waiting for 100-continue
< HTTP/1.1 200 OK
< Server: Winstone Servlet Engine v0.9.10
< Content-Type: text/plain;charset=UTF-8
< Connection: Close
< Date: Fri, 01 Apr 2011 13:03:41 GMT
< X-Powered-By: Servlet/2.5 (Winstone/0.9.10)
# Now it finds the plugin by name
$ java -jar jenkins-cli.jar -s http://localhost:9020 install-plugin findbugs
Installing findbugs from update center
$ java -jar jenkins-cli.jar -s http://localhost:9020 safe-restart
hudson.lifecycle.RestartNotSupportedException: Restart is not supported on Mac OS X
$ java -jar jenkins-cli.jar -s http://localhost:9020 reload-configuration
@ianformanek
Copy link

This was extremely helpful. I derived a bit simpler single line version without the need for saving to a file:

curl  -L http://updates.jenkins-ci.org/update-center.json | sed '1d;$d' | curl -X POST -H 'Accept: application/json' -d @- http://localhost:9020/updateCenter/byId/default/postBack

@bravoecho
Copy link

Many thanks for this, it was very helpful indeed.
Also, as posting the data to the local server wasn't working in my case, I found that the same result can be achieved just creating an updates directory in the Jenkins home (/var/lib/jenkins) and copying the default.json file into it.
Of course the owner of the new dir and file must be jenkins.
Summarising:

wget -O default.js http://updates.jenkins-ci.org/update-center.json
sed '1d;$d' default.js > default.json
mkdir /var/lib/jenkins/updates
mv default.json /var/lib/jenkins/updates/
chown -R jenkins:nogroup /var/lib/jenkins/updates
service jenkins restart

@chr0n1x
Copy link

chr0n1x commented Jul 11, 2013

this is awesome. Thanks so much!

@elgalu
Copy link

elgalu commented Nov 15, 2013

Here a chef recipe to automate this:

include_recipe 'jenkins'

plugins_to_install = node[:jenkins][:server][:plugins]

# Ensure jenkins user home dir exists
directory "#{node[:jenkins][:server][:home]}" do
  owner "#{node[:jenkins][:server][:user]}"
  group "#{node[:jenkins][:server][:user]}"
  action :create
end

# Fixes possible error:
#   STDOUT: `git` is neither a valid file, URL, nor a plugin artifact name in the update center
#   No update center data is retrieved yet from: http://updates.jenkins-ci.org/update-center.json
directory "#{node[:jenkins][:server][:home]}/updates" do
  owner "#{node[:jenkins][:server][:user]}"
  group "#{node[:jenkins][:server][:user]}"
  action :create
end
execute "update jenkins update center" do
  command "wget http://updates.jenkins-ci.org/update-center.json -qO- | sed '1d;$d'  > #{node[:jenkins][:server][:home]}/updates/default.json"
  user "#{node[:jenkins][:server][:user]}"
  group "#{node[:jenkins][:server][:user]}"
  creates "#{node[:jenkins][:server][:home]}/updates/default.json"
end

# Install all plugins and restart once
jenkins_cli "install-plugin #{plugins_to_install.join(' ')}"
jenkins_cli "safe-restart"

@joetde
Copy link

joetde commented Jan 21, 2014

Thank you sooooooooooooo much. The one line command is just perfect.

@gwhitelaw
Copy link

Well done chaps - great fix. Especially ianformanek for the oneliner.

@jabclab
Copy link

jabclab commented Feb 13, 2014

Awesome guys, thanks for this!

@liptga
Copy link

liptga commented Mar 14, 2014

Thanks for the nice workaround.

@clarete
Copy link

clarete commented Mar 20, 2014

This was really helpful, thank you!, I've been working on an ansible role for Jenkins and this was the missing piece: https://github.com/clarete/ansible-jenkins

@languitar
Copy link

I've noticed, that this will not change the URL that is visible in the plugin settings page in the browser. Any way to change this?

@z3ndrag0n
Copy link

Attempting this now, I get an exception-based 404 from the Winstone servlet engine - is this a post size issue? Looks like copying the file into place may be the way to go.

@marslo
Copy link

marslo commented Nov 21, 2016

It's really god damn perfect!!!
I'm using curl -L http://updates.jenkins-ci.org/update-center.json | sed '1d;$d' | curl -u username:password X POST -H 'Accept: application/json' -d @- http://localhost:8080/updateCenter/byId/default/postBack

@ChastinaLi
Copy link

ChastinaLi commented Dec 1, 2016

curl -L http://updates.jenkins-ci.org/update-center.json | sed '1d;$d' | curl -u username:password X POST -H 'Accept: application/json' -d @- http://localhost:8080/updateCenter/byId/default/postBack
Why is this necessary? http://updates.jenkins-ci.org/update-center.json is the default update site that comes ofb.
@marslo since you replied most recently. Any insight would be appreciated

@samwar
Copy link

samwar commented Dec 9, 2020

No need to do the sed if you get the actual json file from: https://updates.jenkins.io/current/update-center.actual.json

@samwar
Copy link

samwar commented Jan 28, 2021

If you go to https://updates.jenkins.io/, there are actually some helpful instructions on how to get the file and get it for your particular installed version of jenkins. That's important because new versions of plugins can be incompatible with the stable versions of jenkins.

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