Hari Babu
Hari Babu

Reputation: 1

Groovy DSL for Extended choice parameter plugin

I'm converting my Jenkins job configurations into code using groovy DSL. Am able to convert all the code except extended choice parameter plugin configuration.

I've a groovy script which does some API calls and get the values and return as choice to the defied parameter in the job. I've tested it and working fine. But, when I tried to automate/convert the same into Groovy DSL am not getting enough support from the plugin rather I haven't find any document which helps me with this situation.

kindly help.

Upvotes: 0

Views: 5588

Answers (2)

agabrys
agabrys

Reputation: 9116

Job DSL plugins allows you to add XML configuration to jobs config.xml files. You have to use configure closure and next specify whatever you want. For example I have such configuration:

<hudson.model.ParametersDefinitionProperty>
  <parameterDefinitions>
    <com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition plugin="[email protected]">
      <name>PRODUCT_REPO_URL</name>
      <description>ssh URL of the product repository</description>
      <quoteValue>false</quoteValue>
      <saveJSONParameterToFile>false</saveJSONParameterToFile>
      <visibleItemCount>10</visibleItemCount>
      <type>PT_SINGLE_SELECT</type>
      <groovyScript>import hudson.slaves.EnvironmentVariablesNodeProperty
import jenkins.model.Jenkins

Jenkins.get().globalNodeProperties.get(EnvironmentVariablesNodeProperty.class).envVars.get(&apos;PRODUCT_REPOSITORIES&apos;)</groovyScript>
      <bindings></bindings>
      <groovyClasspath></groovyClasspath>
      <defaultGroovyScript>import hudson.slaves.EnvironmentVariablesNodeProperty
import jenkins.model.Jenkins

Jenkins.get().globalNodeProperties.get(EnvironmentVariablesNodeProperty.class).envVars.get(&apos;PRODUCT_REPOSITORY_DEFAULT&apos;)</defaultGroovyScript>
      <defaultBindings></defaultBindings>
      <defaultGroovyClasspath></defaultGroovyClasspath>
      <multiSelectDelimiter>,</multiSelectDelimiter>
      <projectName>try-to-upgrade-dependencies</projectName>
    </com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
  </parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>

Now I can generate it by adding the following code:

configure {
    project -> project / 'properties' << 'hudson.model.ParametersDefinitionProperty' {
        parameterDefinitions {
            'com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition'(plugin: '[email protected]') {
                delegate.name('PRODUCT_REPO_URL')
                delegate.description('ssh URL of the product repository')
                delegate.quoteValue(false)
                delegate.saveJSONParameterToFile(false)
                delegate.visibleItemCount(10)
                delegate.type('PT_SINGLE_SELECT')
                delegate.groovyScript("""import hudson.slaves.EnvironmentVariablesNodeProperty
import jenkins.model.Jenkins

Jenkins.get().globalNodeProperties.get(EnvironmentVariablesNodeProperty.class).envVars.get('PRODUCT_REPOSITORIES')""")
                delegate.defaultGroovyScript("""import hudson.slaves.EnvironmentVariablesNodeProperty
import jenkins.model.Jenkins

Jenkins.get().globalNodeProperties.get(EnvironmentVariablesNodeProperty.class).envVars.get('PRODUCT_REPOSITORY_DEFAULT')""")
                delegate.multiSelectDelimiter(',')
                delegate.projectName('try-to-upgrade-dependencies')
            }
        }
    }
}

The final result:

<hudson.model.ParametersDefinitionProperty>
    <parameterDefinitions>
        <com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition plugin="[email protected]">
            <name>PRODUCT_REPO_URL</name>
            <description>ssh URL of the product repository</description>
            <quoteValue>false</quoteValue>
            <saveJSONParameterToFile>false</saveJSONParameterToFile>
            <visibleItemCount>10</visibleItemCount>
            <type>PT_SINGLE_SELECT</type>
            <groovyScript>import hudson.slaves.EnvironmentVariablesNodeProperty
import jenkins.model.Jenkins

Jenkins.get().globalNodeProperties.get(EnvironmentVariablesNodeProperty.class).envVars.get('PRODUCT_REPOSITORIES')</groovyScript>
            <defaultGroovyScript>import hudson.slaves.EnvironmentVariablesNodeProperty
import jenkins.model.Jenkins

Jenkins.get().globalNodeProperties.get(EnvironmentVariablesNodeProperty.class).envVars.get('PRODUCT_REPOSITORY_DEFAULT')</defaultGroovyScript>
            <multiSelectDelimiter>,</multiSelectDelimiter>
            <projectName>try-to-upgrade-dependencies</projectName>
        </com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
    </parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>

Upvotes: 0

DaveR
DaveR

Reputation: 93

I went through the same process a couple of months ago. I found this article tremendously useful - http://www.devexp.eu/2014/10/26/use-unsupported-jenkins-plugins-with-jenkins-dsl.

Here's a sample code snippet:

configure {
    project->
        project / 'properties' << 'hudson.model.ParametersDefinitionProperty' {
        parameterDefinitions {
            'com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition' {
                name 'TARGET_ENVS'
                quoteValue 'false'
                saveJSONParameterToFile 'false'
                visibleItemCount '15'
                type 'PT_CHECKBOX'
                value "${deployTargets}"
                multiSelectDelimiter ','
                projectName "${jobName}"
            }
        }
    }
}

The article suggests appending the 'configure' code block at the end of your DSL job definition, however that didn't work for me. I ended up putting the code block at the start of the definition.

Good luck

Upvotes: 3

Related Questions