Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple groovy script to upgrade active plugins when new versions are available
jenkins.model.Jenkins.getInstance().getUpdateCenter().getSites().each { site ->
site.updateDirectlyNow(hudson.model.DownloadService.signatureCheck)
}
hudson.model.DownloadService.Downloadable.all().each { downloadable ->
downloadable.updateNow();
}
def plugins = jenkins.model.Jenkins.instance.pluginManager.activePlugins.findAll {
it -> it.hasUpdate()
}.collect {
it -> it.getShortName()
}
println "Plugins to upgrade: ${plugins}"
long count = 0
jenkins.model.Jenkins.instance.pluginManager.install(plugins, false).each { f ->
f.get()
println "${++count}/${plugins.size()}.."
}
if(plugins.size() != 0 && count == plugins.size()) {
jenkins.model.Jenkins.instance.safeRestart()
}
@uncletall

This comment has been minimized.

Copy link

@uncletall uncletall commented Dec 5, 2019

My favourite script

@alecharp

This comment has been minimized.

Copy link
Owner Author

@alecharp alecharp commented Dec 5, 2019

thanks @uncletall! I haven't used it in a very long time, as now I'm using a custom Docker image, based on the official jenkins/jenkins one, and leveraging the install-plugins.sh script.

It's not auto-magic as this one, but at least it's a bit more reproducible and stable.

@echvsg

This comment has been minimized.

Copy link

@echvsg echvsg commented Jun 1, 2020

@alecharp: This script is great.
It would be really helpful to modify the script to check if there is any way to upgrade only the plugins which have no compatibility warnings with the current Jenkins version.

@realies

This comment has been minimized.

Copy link

@realies realies commented Dec 11, 2020

@alecharp, can you share more about using the Docker image to have the latest Jenkins WAR and plugins? Could it be that you flush all existing JARs/plugins and keep appdata before updating the image?

@taherbs

This comment has been minimized.

Copy link

@taherbs taherbs commented Jul 7, 2021

I have made some work starting from this script. I made it detect plugins warnings or if any of them get deprecated before performing the update.
I have created a Jenkins pipeline that invokes the Groovy script and returns an alert if warnings or deprecated plugins are detected.
here is a link to the scripts
https://gist.github.com/taherbs/6d03b4d56ac4f1e7a119e64cf5d17f4c
Thank you @alecharp for the inspiration 🤘

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