Creating a reusable set of job parameters

997 views
Skip to first unread message

Stephen Rufle

unread,
Mar 3, 2016, 5:37:31 PM3/3/16
to job-dsl-plugin
I have been trying to following
https://github.com/jenkinsci/job-dsl-plugin/wiki/The-Configure-Block

to create a reusable set of job parameters. I started at 'Reusable Configure Blocks' and pushing to my repo and regenerating. Then I found the playgorund (+1 for that site), but I only got so far as my 'Current Output' example


Groovy from ' Jenkins Job DSL Playground'


Closure myParams() {
  return {
    it / 'properties' / 'hudson.model.ParametersDefinitionProperty' {
        'parameterDefinitions'  {
          'hudson.model.StringParameterDefinition'{
            'name'('AA')
          }
        }
    }
  }    
}
Closure myParams2() {
  return {
    it / 'properties' / 'hudson.model.ParametersDefinitionProperty' {
        'parameterDefinitions'  {
          'hudson.model.StringParameterDefinition'{
            'name'('BB')
          }
        }
    }
  }    
}
job('c1') {
  description 'Build and test the app.'
  keepDependencies(true)
 
  configure  myParams()
  configure  myParams2()
 
}

job('c2') {
  description 'Build and test the app.'
  keepDependencies(true)
 
  configure  myParams()
  configure  myParams2()
}

Current output
<!-- 1. c1 -->
<project>
    <actions></actions>
    <description>Build and test the app.</description>
    <keepDependencies>true</keepDependencies>
    <properties>
        <hudson.model.ParametersDefinitionProperty>
            <parameterDefinitions>
                <hudson.model.StringParameterDefinition>
                    <name>BB</name>
                </hudson.model.StringParameterDefinition>
            </parameterDefinitions>
        </hudson.model.ParametersDefinitionProperty>
    </properties>
    <scm class='hudson.scm.NullSCM'></scm>
    <canRoam>true</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <triggers class='vector'></triggers>
    <concurrentBuild>false</concurrentBuild>
    <builders></builders>
    <publishers></publishers>
    <buildWrappers></buildWrappers>
</project>

<!-- 2. c2 -->
<project>
    <actions></actions>
    <description>Build and test the app.</description>
    <keepDependencies>true</keepDependencies>
    <properties>
        <hudson.model.ParametersDefinitionProperty>
            <parameterDefinitions>
                <hudson.model.StringParameterDefinition>
                    <name>BB</name>
                </hudson.model.StringParameterDefinition>
            </parameterDefinitions>
        </hudson.model.ParametersDefinitionProperty>
    </properties>
    <scm class='hudson.scm.NullSCM'></scm>
    <canRoam>true</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <triggers class='vector'></triggers>
    <concurrentBuild>false</concurrentBuild>
    <builders></builders>
    <publishers></publishers>
    <buildWrappers></buildWrappers>
</project>

What I wanted
<!-- 1. c1 -->
<project>
    <actions></actions>
    <description>Build and test the app.</description>
    <keepDependencies>true</keepDependencies>
    <properties>
        <hudson.model.ParametersDefinitionProperty>
            <parameterDefinitions>
                <hudson.model.StringParameterDefinition>
                    <name>AA</name>
                </hudson.model.StringParameterDefinition>             
                <hudson.model.StringParameterDefinition>
                    <name>BB</name>
                </hudson.model.StringParameterDefinition>
            </parameterDefinitions>
        </hudson.model.ParametersDefinitionProperty>
    </properties>
    <scm class='hudson.scm.NullSCM'></scm>
    <canRoam>true</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <triggers class='vector'></triggers>
    <concurrentBuild>false</concurrentBuild>
    <builders></builders>
    <publishers></publishers>
    <buildWrappers></buildWrappers>
</project>

<!-- 2. c2 -->
<project>
    <actions></actions>
    <description>Build and test the app.</description>
    <keepDependencies>true</keepDependencies>
    <properties>
        <hudson.model.ParametersDefinitionProperty>
            <parameterDefinitions>
                <hudson.model.StringParameterDefinition>
                    <name>AA</name>
                </hudson.model.StringParameterDefinition>             
                <hudson.model.StringParameterDefinition>
                    <name>BB</name>
                </hudson.model.StringParameterDefinition>
            </parameterDefinitions>
        </hudson.model.ParametersDefinitionProperty>
    </properties>
    <scm class='hudson.scm.NullSCM'></scm>
    <canRoam>true</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <triggers class='vector'></triggers>
    <concurrentBuild>false</concurrentBuild>
    <builders></builders>
    <publishers></publishers>
    <buildWrappers></buildWrappers>
</project>

Daniel Spilker

unread,
Mar 4, 2016, 1:05:27 AM3/4/16
to job-dsl...@googlegroups.com
You do not need to use a configure block if built-in DSL methods are available. And you can create any closure within a function or assign any closure to a variable. So you reuse a closure for the parameters method (https://jenkinsci.github.io/job-dsl-plugin/#path/job-parameters):

def aParam = {
  stringParam('aa')
}

def moreParams = {
  stringParam('bb')
  stringParam('cc')
}

job('one') {
  parameters(aParam)
  parameters(moreParams)
}

job('two') {
  parameters(aParam)
  parameters(moreParams)
}


Daniel

--
You received this message because you are subscribed to the Google Groups "job-dsl-plugin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to job-dsl-plugi...@googlegroups.com.
To post to this group, send email to job-dsl...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/job-dsl-plugin/f2f5e159-a6df-487f-9dbf-a48f869fe929%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stephen Rufle

unread,
Mar 4, 2016, 9:40:27 AM3/4/16
to job-dsl-plugin
Thanks that worked, I should have posted my issue earlier :)

Do I understand aParam is a closure?

def aParam = {
  stringParam('aa')
}

This is using parameters as a method that takes the previously defined closure as a method parameter.
parameters(aParam)

What is this called?
parameters{
   booleanParam('FLAG', true)
}

from the DSL docs
To reuse an configure block for many scripts, the configure block can be moved to a helper class.
So when using JobHelper 

Helper Class
package helpers

class JobHelper {
static Closure myCommon() {

def aParam = {
stringParam('aa')
            stringParam('bb')
}
return aParam
}
}


Usage
import static helpers.JobHelper.myCommon

job('example-1') {
parameters(myCommon())
}

 

Stephen Rufle

unread,
Mar 4, 2016, 4:16:38 PM3/4/16
to job-dsl-plugin
What do I do if I create a custom tag using a closure and want to then use it
https://gist.github.com/srufle/0ef0b03cfcd3ca1f58a7



On Thursday, March 3, 2016 at 11:05:27 PM UTC-7, Daniel Spilker wrote:

Matt Sheehan

unread,
Mar 5, 2016, 4:21:06 PM3/5/16
to job-dsl-plugin
I usually pull reusable chunks into a static helper method like:

class DslUtil {

    static configureTfsParams(jobContext) {
        configureValidatingParam(jobContext, [
            name:'TFS_SERVER_URL',
            description:'Example Desc A',
            regex:'.+',
            failedValidationMessage:'Give me A'
        ])
        configureValidatingParam(jobContext, [
            name:'EXAMPLE_PROJECT_PATH',
            description:'Example Desc B',
            regex:'.+',
            failedValidationMessage:'Give me B'
        ])
    }

    static configureValidatingParam(jobContext, params) {
        jobContext.with {
            configure {
                it / 'properties' / 'hudson.model.ParametersDefinitionProperty' / 'parameterDefinitions' << 'hudson.plugins.validating__string__parameter.ValidatingStringParameterDefinition' {
                    'name'(params.name)
                    'description'(params.description)
                    'defaultValue'(params.defaultValue)
                    'regex'(params.regex)
                    'failedValidationMessage'(params.failedValidationMessage)
                }
            }
        }
    }
}

job('test') {
    DslUtil.configureTfsParams delegate

    DslUtil.configureValidatingParam(delegate, [
        name:'APP_NAME',
        description:'Name of the application to deploy',
        regex:'.+',
        failedValidationMessage:'Must supply an application to deploy'
    ])
    DslUtil.configureValidatingParam(delegate, [
        name:'APP_VERSION',
        description:'Version of the application to deploy',
        regex:'.+',
        failedValidationMessage:'Must supply a version to deploy'
    ])
    DslUtil.configureValidatingParam(delegate, [
        name:'TARGET_VM',
        description:'Server that should have application after deploy',
        regex:'.+',
        failedValidationMessage:'Must supply a server to deploy'
    ])
Reply all
Reply to author
Forward
0 new messages