Jenkins DSL custom config file folder

Viewed 581

We are using DSL to build/setup our Jenkins structure. In it, we create our folder structure and then all our jobs within the folders. The jobs end up in the correct folders by including the folder name in the job name

pipelineJob('folder/subfolder/Job Name') {}

While the UI lets me create a config file within a folder, I cannot find a way within the dsl groovy script hierachy to put a custom config file in a folder.

While I can easily create a config file:

configFiles {
  customConfig {
    name('myCustom.yaml')
    id('59f394fc-40fe-489d-989c-7556c1a01153')
    content('yaml content goes here')
  }
}

There seems to be no way to put this file into a folder / subfolder.

1 Answers

While the Job DSL plugin does not offer an easy way to do this, you can use a configure block to directly modify the xml.

folder('Config-File Example') {
    description("Example of a Folder with a Config-File, created via Job DSL")

    configure { folder ->
        folder / 'properties' << 'org.jenkinsci.plugins.configfiles.folder.FolderConfigFileProperty'() { 
            configs(class: 'sorted-set') { 
                comparator(class: 'org.jenkinsci.plugins.configfiles.ConfigByIdComparator')
                'org.jenkinsci.plugins.configfiles.json.JsonConfig'() { 
                    id 'my-config-file-id'
                    providerId 'org.jenkinsci.plugins.configfiles.json.JsonConfig'
                    name 'My Config-File Name'
                    comment 'This contains my awesome configuration data'
                    // Use special characters as-is, they will be encoded automatically
                    content '[ "1", \'2\', "<>$%&" ]'
                } 
            } 
        } 
    } 
}
Related