I'm trying to get a Set of installed Jenkins plugins through the API using curl
and jq
but can't seem to flatten nested arrays correctly. An example of the output JSON I'm fetching:
{
"plugins": [
{
"shortName": "foo",
"version": "6.9",
"dependencies": [
{
"shortName": "bar",
"version": "4.2.0"
},
{
"shortName": "baz",
"version": "6.6.6"
}
]
},
{
"shortName": "fah",
"version": "4.2",
"dependencies": [
{
"shortName": "bah",
"version": "1.3"
},
{
"shortName": "bar",
"version": "2.1.0"
},
{
"shortName": "baz",
"version": "6.6.6"
]
}
]
}
And here's the result that I'd like to get:
[
{
"shortName": "bah",
"version": "1.3"
},
{
"shortName": "bar",
"version": "2.1.0"
},
{
"shortName": "bar",
"version": "4.2.0"
},
{
"shortName": "baz",
"version": "6.6.6"
},
{
"shortName": "fah",
"version": "4.2"
},
{
"shortName": "foo",
"version": "6.9"
},
]
I've made various attempts at using map
and flatten
but can't seem to preserve the parent's keys, let alone getting a comprehensive array of each plugin and its version. The payload is easy enough to get with curl
:
curl -u username:token --globoff -H "${JENKINS_CRUMB}" "${JENKINS_URL}/pluginManager/api/json?tree=plugins[shortName,version,dependencies[shortName,version]]"
I'd like to just pipe that output straight into jq
to transform it into the desired results. Am I able to do this within jq
or would I need to have some sort of wrapper for these transformations like Python?
I'm making this way more difficult than it needs to be. I can just get the names of the plugins from the API:
curl -u $USERNAME:$TOKEN --globoff -H "${JENKINS_CRUMB}" "${JENKINS_URL}/pluginManager/api/json?tree=plugins[shortName]" | jq '.plugins[] | .shortName'
and then install them and their dependencies with jenkins-cli
:
java -jar jenkins-cli.jar -s $JENKINS_URL install-plugin $PLUGIN
I ended up opting for the SSH option to interact with Jenkins' API, it's cleaner and easier to use:
ssh.exe -l "${USER}" -p 53801 "${HOST}" list-plugins | awk '{print $1}' > plugins.txt
for line in $(cat plugins.txt)
do
echo "Installing ${line} plugin"
ssh.exe -l "${USER}" -p 53801 "${HOST}" install-plugin $line
done