比较跨实例安装的 Jenkins 插件

Comparing the Jenkins plugins installed across instances

我有 6 台 Jenkins 主机和一台生产 Jenkins 主机,我们在其中使用了将近 100 个插件。我们要确保所有实例都具有相同的插件及其各自的版本。

我们尝试使用以下 curl 命令来检索特定主机使用的插件列表。我们正在尝试开发实用程序来比较所有主机上的插件版本,并在生产主机上缺少任何插件时向我们报告。

curl 'https://<Jenkins url>/pluginManager/api/xml?depth=1&x‌​path=/*/*/shortName|‌​/*/*/version&wrapper‌​=plugins' | perl -pe 's/.*?<shortName>([\w-]+).*?<version>([^<]+)()(<\/\w+>)+/ \n/g'

我们在shell中这样做:

java -jar jenkins-cli.jar -s <jenkins-url> list-plugins > <outputfile>

然后我们使用不同主机的输出来确定差异。

这不是一个完整的解决方案,但您绝对可以利用 Python 库来比较版本不兼容或缺少插件。

    import xml.etree.ElementTree as ET
import requests
import sys
from itertools import zip_longest
import itertools
from collections import OrderedDict
import collections
import csv

url = sys.argv[1].strip()
filename = sys.argv[2].strip()

response = requests.get(url+'/pluginManager/api/xml?depth=1',stream=True)
response.raw.decode_content = True
tree = ET.parse(response.raw)
root = tree.getroot()
data = {}
for plugin in root.findall('plugin'):
    longName = plugin.find('longName').text
    shortName = plugin.find('shortName').text
    version = plugin.find('version').text
    data[longName] = version
    with open(filename, 'w') as f:
        [f.write('{0},{1}\n'.format(key, value)) for key, value in data.items()]

会给你csv格式的插件列表!

稍后可用于与另一个实例进行比较,所有这些都可以在单个 python 脚本中实现。