Reputation: 271
I want to create a Jenkins folder using Jenkins DSL. While creating the folder I want to add a few custom properties. I am not able to add any property using the below syntax. What should be the syntax
folder('example-2') {
displayName("testFolder")
description("desc")
properties{
'key','value'
}
}
Upvotes: 2
Views: 3421
Reputation: 157
I face the same issue, needed to create a folder with DSL but with some config, to be more concrete I need to add the SharedLibrary params. As there is no specific methods to add that properties I need to do it manually.
Below code will create a folder from a DSL Jenkins code configuring the SharedLibrary to the specified values I need:
// DSL to generate the release folder structure
folder( 'MyFolderName' ) {
description('Common Tool for My Project')
// there is no doc for configure this https://jenkinsci.github.io/job-dsl-plugin/#path/folder-properties
// it is supossed that you can manually do it https://stackoverflow.com/a/53528905/3922034
// the configure scructure I saw it at https://github.com/jenkinsci/job-dsl-plugin/wiki/The-Configure-Block
// and in order to find the proper tags to be used I configure the job manually at jenkins, then look
// into the config.xml file for the folder and replicate the structure with the help of the
// Jenkins Job DSL Playground https://github.com/sheehan/job-dsl-playground
properties {
configure {
it / 'properties'/'org.jenkinsci.plugins.workflow.libs.FolderLibraries'/'libraries'/'org.jenkinsci.plugins.workflow.libs.LibraryConfiguration' / 'name' << 'pipeline_library'
def retrieval = it / 'properties'/'org.jenkinsci.plugins.workflow.libs.FolderLibraries'/'libraries'/'org.jenkinsci.plugins.workflow.libs.LibraryConfiguration' / retriever(class: "org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever")
def myscm = retrieval / scm(class: 'jenkins.plugins.git.GitSCMSource')
myscm << remote('ssh://MY_SERVER/my_repo.git')
myscm << credentialsId('My_Credential_ID')
it / 'properties'/'org.jenkinsci.plugins.workflow.libs.FolderLibraries'/'libraries'/'org.jenkinsci.plugins.workflow.libs.LibraryConfiguration' / 'defaultVersion' << branch
it / 'properties'/'org.jenkinsci.plugins.workflow.libs.FolderLibraries'/'libraries'/'org.jenkinsci.plugins.workflow.libs.LibraryConfiguration' / 'implicit' << 'true'
it / 'properties'/'org.jenkinsci.plugins.workflow.libs.FolderLibraries'/'libraries'/'org.jenkinsci.plugins.workflow.libs.LibraryConfiguration' / 'allowVersionOverride' << 'true'
it / 'properties'/'org.jenkinsci.plugins.workflow.libs.FolderLibraries'/'libraries'/'org.jenkinsci.plugins.workflow.libs.LibraryConfiguration' / 'includeInChangesets' << 'true'
}
}
}
Upvotes: 0
Reputation: 2146
Here you go:
folder('example-2') {
displayName("testFolder")
description("desc")
properties{
configure {
it / 'properties' / key1 << 'value1'
it / 'properties' / key2 << 'value2'
}
}
}
The Folder's properties documentation is empty which suggests it doesn't support any in-built syntax underneath. Using configure
to directly manipulate XML using closure.
You can test any DSL using their excellent playground.
Upvotes: 0
Reputation: 5490
That's probably because the ability to define properties for a folder has been removed from the open source Folders Plugin and is now only available in Cloudbees Enterprise Jenkins.
we have decided to release the basic functionality of Folders as an open-source plugin hosted on GitHub, like other Jenkins plugins. This 4.0 release is fully functional on its own, but some of the advanced features have been retained in Jenkins Enterprise (in the Folders Plus plugin): the Move action, some health reports and icons, the Environment variables property, the Pull information from nested job list view column, and the Restrict the kind of children in this folder option. The core folder functionality and all of the APIs and extension points are available in the open-source plugin.
Read the full announcement here.
I've been struggling with the need for folder-level properties myself, so I've started my own plugin to fill this need. I've called it Folder Properties Plugin. If you face any issues, please file them in the Git repo and if you just have questions you can post them to the plugin's wiki page.
I hope this helps.
Upvotes: 2
Reputation: 428
public static void addFolderVariable(Folder folder, String variableName, String variableValue) {
String elementName = 'com.cloudbees.hudson.plugins.folder.properties.EnvVarsFolderProperty'
String variables = "$variableName=$variableValue"
folder.with {
configure {
// Get previous values
Node node = it / 'properties' / "$elementName" / 'properties'
if (node.value()) {
// Join them together
String existingVariables = node.value()
variables = "$existingVariables\n$variables"
}
// Assign it
it / 'properties' / "$elementName" / 'properties' << "$variables"
}
}
}
It seems this works with Enterprise version. (Sorry, I'm not a Groovy developer as you can see)
Upvotes: 0