简体   繁体   中英

Get all managed files available for jenkins job

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 parametrized 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

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

作业参数

托管文件

参数设置

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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM