Jenkins [SOLVED]: Jenkins: Automate the creation of a job using DSL

Jenkins [SOLVED]: Jenkins: Automate the creation of a job using DSL

Home Forums Automation Tools Jenkins Jenkins [SOLVED]: Jenkins: Automate the creation of a job using DSL

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
  • #205596

    Cloudy Point


    I’m trying to fully automate the creation of Jenkins. I got most of the setup working using groovy scripts. My last hurdle is creating a job using the DSL. I’m able to create the job using xml via:

    import jenkins.model.*
    def jobName = "jobname"
    String configXml = new File('/jobname.xml').getText('UTF-8')
    def xmlStream = new ByteArrayInputStream( configXml.getBytes() )
    Jenkins.instance.createProjectFromXML(jobName, xmlStream)

    However, the XML file is hard to maintain and not easy to understand. So I wanted to switch to use DSL syntax, but I can’t fix an API to replace createProjectFromXML(…) with something like createPrjectFromDSL?

    My question is how to create a new job using DSL? I got this part working, thank you.

    However my next question is where do I put this file so Jenkins will create the job on startup? When i try to copy it to /var/jenkins_home/init.groovy.d I get the following exception on startup:

    WARNING: Failed to run script file:/var/jenkins_home/init.groovy.d/neoconfig-dsl.groovy
    groovy.lang.MissingMethodException: No signature of method: neoconfig-dsl.job() is applicable for argument types: (java.lang.String, neoconfig-dsl$_run_closure1) values: [neo, neoconfig-dsl$_run_closure1@7d799f93]
    Possible solutions: run(), run(), any(), wait(), grep(), dump()


    Cloudy Point

    Accepted AnswerAnswer

    The Jenkins Job DSL plugin provides exactly, what you seem to be looking for: A (Groovy-based) DSL to define jobs.

    A simple job definition looks as follows:

    def repo = 'DSL-Tutorial-1-Test'
    job(repo) {
        scm {
        triggers {
            scm('H/15 * * * *')
        steps {
            maven('-e clean test')

    EDIT: To automatically start this job after definition, add the following code:

    // automatically queue the job after the initial creation
    if (!jenkins.model.Jenkins.instance.getItemByFullName(repo)) {

    A complete example of a setup that creates a self-bootstrapping docker container can be found in tknerr/jenkins-pipes-infra. I am using (mostly) the same approach here.

    Author: StephenKing
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.