Search code examples
jenkinsgroovyyamljenkins-pluginsjenkins-groovy

Jenkins Active Choices Plugin, Dynamically fill checkbox parameters from yaml file


I am new to Jenkins and keep exploring different plugins, articles everyday to learn something new about Jenkins.

I found that, the parameters can be filled before the build using the active choice parameter plugin from the json file uploaded to the Jenkins server. I had googled it but couldn't find the right explanation. The hint I got was the use of the Groovy script in the Active Choice parameter. If its possible, please let me know.

I appreciate @Noam Helmer for the reference, but how can i capture the group1, group2, group3, group4,... from below file. all, children, hosts no matter how many groups are added to this file, it will remain the same.

all:
  children:
    group1:
      hosts:
        xyz4.axs: 
    group2:
      hosts:
        xyz5.adf: 
        xyz8.asf: 
    group3:
      hosts:
        xyz3.asd: 
        xyz6.ads: 
        xyz7.asd: 

I tried the following script but was unable to get the group name.

import org.yaml.snakeyaml.Yaml
List groups = []
Yaml parser = new Yaml()
def example = parser.load(("/var/lib/jenkins/workspace/groups/hosts" as File).text)
for (details in example){
  groups.add(details)
}
println(groups)

Now, I don't know if I'm on the right track, please give me a hint. Any help in advance would be appreciated.


Solution

  • Finally found the answer, once again thanks @NoamHelmer for replying.

    import org.yaml.snakeyaml.Yaml
    List groups = []
    Yaml parser = new Yaml()
    def example = parser.load(("path to file" as File).text)
    
    for(anf in example.keySet()){
        groups.add(anf)
    } 
            
    for(inf in example.all.children.keySet()){
        groups.add(inf)  
    }
    
    return(groups)
    

    the output will be :[all, group1, group2, group3]