JobDSL : how to define build env var into a configuration class

103 views
Skip to first unread message

JL 6BerYeti

unread,
Nov 2, 2016, 1:38:50 PM11/2/16
to Jenkins Users
Quite new to Job DSL, I am running some tests and trias.
I currently try to factorize some common job configuration part in a library I can reuse within each job.
I know how to do that with DSL, but I face out a problem with adding an env var.

I have this code which run ok :

class jobFramework {
   static commonConf(dslFactory) {
       dslFactory.configure { project ->
           project / 'buildWrappers' {
               'hudson.plugins.ws__cleanup.PreBuildCleanup'{
                   'deleteDirs'(false)
               }
               'trigger'{
                   'cron'('*,*,H/4,*')
               }

           }
           project / 'logRotator' {
                'daysToKeep'('10')
                'numToKeep'('-1')
                'artifactDaysToKeep'('12')
                'artifactNumToKeep'('-1')
            }
       }
   }
}

freeStyleJob("my_job")
{
    environmentVariables {
        env('TOTO','TITI')
    }
    jobFramework.commonConf(it)
}

Now I would like to put the env var definition into the jobFramework class.
But trying the following lines :
           project / 'environmentVariables' {
               'env'('TOTO','TITI')
           }
I obtain the following error :
groovy.lang.MissingMethodException: No signature of method: groovy.util.NodeBuilder.env() is applicable for argument types: (java.lang.String, java.lang.String) values: [TOTO, TITI]
Possible solutions: any(), find(), any(groovy.lang.Closure), wait(), every(), dump()
at jobFramework$_commonConf_closure1$_closure4.doCall(script:20)
at jobFramework$_commonConf_closure1$_closure4.doCall(script)
at javaposse.jobdsl.dsl.MissingPropertyToStringDelegate.methodMissing(MissingPropertyToStringDelegate.groovy:39)
at javaposse.jobdsl.dsl.MissingPropertyToStringDelegate.invokeMethod(MissingPropertyToStringDelegate.groovy)
at jobFramework$_commonConf_closure1.doCall(script:19)
...
where 10 is the line number of the lines related to env vars.

So... after several trials, I am quite lost...

If someone cold help, thanks a lot !

Best Regards
J-L

Victor Martinez

unread,
Nov 2, 2016, 5:51:23 PM11/2/16
to Jenkins Users
Hi J-L,

 I've just seen your jobFramework class and I guess you don't need to use the configure closure unless you those plugins are not supported by JobDSL, otherwise it's worth using the DSL syntax, for instance:

There are some interesting urls/projects:

If you would like to use the configure closure anyway, use the http://job-dsl.herokuapp.com/ then you will be able to understand how your snippet is converted to XML and from there you could create your specific configure closure. the below DSL produce the same Job

freeStyleJob("my_job")
{
    environmentVariables {
        env('TOTO','TITI')
    }
}

freeStyleJob("my_job") {
    configure { project ->
        def properties = project / 'properties' 
        properties << EnvInjectJobProperty {
            info {
                propertiesContent('TOTO=TITI')
                loadFilesFromMaster(false)
            }
            on(true)
            keepJenkinsSystemVariables(true)
            keepBuildVariables(true)
            overrideBuildParameters(false)
            contributors('')
        }
    }
}


Finally, if you use JobDSL version 1.46+ then you can use the autogenerated DSL and get rid off the configure closure.

I hope it helps,

Cheers

Victor Martinez

unread,
Nov 2, 2016, 5:52:03 PM11/2/16
to Jenkins Users
Specific mailing list about Job DSL Plugin:

Cheers

JL 6BerYeti

unread,
Nov 3, 2016, 4:12:56 AM11/3/16
to Jenkins Users
Great !
Thanks a lot. It works !
But I hardly understand why it is so different than other confiugration such as ws cleaner or logrotator.

Using the Job DSL playground, it appears that a little bit simpler code is possible by simply avoiding default config :

def properties = project / 'properties' 
    properties << EnvInjectJobProperty {
        info {
            propertiesContent('TOTO=TITI')
            loadFilesFromMaster(false)
        }
}


Best Regards
J.L.P.
Reply all
Reply to author
Forward
0 new messages