Home > Blockchain >  Get all managed files available for jenkins job
Get all managed files available for jenkins job

Time:08-18

I have stored configurations for some jobs in JSON format in the managed file section of Jenkins and it works as expected. I want to expand this by changing these jobs to paralyzed one, where all available managed files (not the global ones) could be selected from a combo box, but I have problems getting all available managed files. Plugins like the configFileProvider are capable to get both the global ones and more workspace/namespace specific ones, so I thought there would be an easy way to achieve this.

Adding a more job-specific not global config file

1

def files = org.jenkinsci.plugins.configfiles.GlobalConfigFiles.get().getConfigs()

works fine for the global ones, but I want to get workspace/namespace specific ones and thought it would go something like this

def files = org.jenkinsci.plugins.configfiles.getConfigsInContext()

It would be amazing if somebody could point me in the right direction how to achieve this.

Thanks

CodePudding user response:

I don't know any direct solution to achieve it as these managed files aren't stored as files in the disk.

My way to achieve it's running a script that parse the ${jenkins_home}/org.jenkinsci.plugins.configfiles.GlobalConfigFiles.xml file and create the managed files in the disk.

Having these files in the disk, you can use the "Filesystem List Parameter Plug-in" to list they in a job parameter.

This way you are

job parameters

managed files

parameter setup

Script

#!/bin/bash

filenames=$(xmllint --xpath '//name/text()' GlobalConfigFiles.xml 2>/dev/null)

while read -r filename
do 

content=$(xmllint --xpath "//name[text()='$filename']/../content/text()" GlobalConfigFiles.xml 2>/dev/null)

if [[ "$filename" == *"xml"* ]]; then
    echo $content | sed -nr 's/&lt;/</gp' | sed -nr 's/&gt;/>/gp' > $filename
else
    echo $content > $filename
fi
done <<< $filenames

CodePudding user response:

Check the following script

// name of the folde you want get configfiles from
def folderName = "Folder2" 
def folderItem = Jenkins.instance.getAllItems(com.cloudbees.hudson.plugins.folder.AbstractFolder.class).find{ (it.name == folderName) }

// If you want to filter a specific configFile type, example Json configs here
def descriptor =  org.jenkinsci.plugins.configfiles.json.JsonConfig.JsonConfigProvider.class 

def allConfigFiles = folderItem.getProperties().get(org.jenkinsci.plugins.configfiles.folder.FolderConfigFileProperty.class).getConfigs();

def jonConfigFiles = folderItem.getProperties().get(org.jenkinsci.plugins.configfiles.folder.FolderConfigFileProperty.class).getConfigs(descriptor);
            
echo "$allConfigFiles"
echo "$jonConfigFiles"
  • Related