Skip to content

Instantly share code, notes, and snippets.

@ulidtko
Created April 22, 2025 20:53
Show Gist options
  • Save ulidtko/aa39b970bf2af97daf29e54734119957 to your computer and use it in GitHub Desktop.
Save ulidtko/aa39b970bf2af97daf29e54734119957 to your computer and use it in GitHub Desktop.
List plugins compatible with the given jenkins-core
#!/usr/bin/env python
"""
To get installed plugins list, open Script Console on /script, and run:
def pluginList = new ArrayList(Jenkins.instance.pluginManager.plugins)
pluginList.sort { it.getShortName() }.each{
plugin ->
println ("${plugin.getShortName()}:${plugin.getVersion()} -- ${plugin.getDisplayName()}")
}
Dump the list of (only) the short-names to a plugins.list file.
Run this script.
Apply with:
jenkins-plugin-cli --plugin-file output.txt
Or with: https://github.com/jenkinsci/plugin-installation-manager-tool
"""
import argparse
import io
import sys
from pprint import pprint
import lxml.html
import requests
from packaging.version import Version
UPDATES = 'https://updates.jenkins.io'
def fetch_plugin_versions(plugin):
resp = requests.get(f'{UPDATES}/download/plugins/{plugin}')
resp.raise_for_status()
doc = lxml.html.document_fromstring(resp.text)
return [
(li.attrib['id'],
li.xpath('./div/div[@class="core-dependency"]/text()')[0].replace('Requires Jenkins ', ''),
# li.xpath('./a[@class="version"]/@href'),
)
for li in doc.xpath('//li[@id]')
]
def main(opts):
jenkinsV = Version(opts.jenkins_version)
plugins = opts.plugin + opts.plugins_list.read().split('\n')
for p in plugins:
try:
pvs = fetch_plugin_versions(p)
# versions are sorted reverse-chronologically, stop on first suitable
for pluginV, coreV in pvs:
if Version(coreV) <= jenkinsV:
print(f"{p}:{pluginV}")
break
except requests.exceptions.HTTPError as e:
print("Got", e, file=sys.stderr)
def args():
p = argparse.ArgumentParser()
p.add_argument('jenkins_version', metavar='JENKINS_VERSION',
help="Jenkins version to target")
p.add_argument('--plugin', metavar='NAMES', nargs='*', default=[],
help="plugin names")
p.add_argument('--plugins-list',
metavar='FILE', type=argparse.FileType('r'), default=io.StringIO(),
help="read from file, 1 per line")
return p
if __name__ == "__main__":
opts = args().parse_args()
sys.exit(main(opts))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment