5

I'm trying DSL job plugin to create new jobs in Jenkins. Is there a way to specify the view when creating the job?

For example, I have a view NewJobsView. I want to create a DSL job called dsl-job and it is creating a new job "dsl-created-job1"

DSL like this:

job {
    name 'dsl-created-job1'
    //view 'NewJobsView'
    //or view {...} to specify the view
}
Jesper Rønn-Jensen
  • 106,591
  • 44
  • 118
  • 155
Aimee
  • 51
  • 1
  • 3
  • If you create a job manually with the jenkins inteface and look at the config with e.g. localhost:8080//config.xml so long as there is a section in there detailing the view you can add it using a config block in the dsl https://github.com/jenkinsci/job-dsl-plugin/wiki/The%20Configure%20Block – KeepCalmAndCarryOn Feb 25 '14 at 19:26
  • Are you using a view plugin? – KeepCalmAndCarryOn Feb 25 '14 at 19:29
  • I couldn't find the view section in config.xml. – Aimee Feb 25 '14 at 21:24
  • I have nested view plugin install. I can install other plugins if it helps. – Aimee Feb 25 '14 at 21:24
  • The job XML doesn't have any entries for any view it belongs to so you wont be able to use the DSL for it. You might be able to use the REST API to add it in https://issues.jenkins-ci.org/browse/JENKINS-8927 or alternatively, use a regex based view and name your jobs accordingly https://wiki.jenkins-ci.org/display/JENKINS/View+Job+Filters – KeepCalmAndCarryOn Feb 25 '14 at 23:20

3 Answers3

3

What if you do:

def myJob=job{name('test1')}
def myJob2=job{name('test2')}
view {
  name('view1')
  jobs{
     name(myJob.name)
    name(myJob2.name)
  }   
}

Or even use a regex at the view.

UPDATE

About the discussion. The nested view is just a different kind of view. The job config.xml doesn't have reference to the view because jenkins has a different abstraction: a view references to jobs.

nerdioculos
  • 106
  • 7
1

I got this working. It creates a job, then creates a view and adds the job to the view. This solution recreates the view every time. You can add multiple jobs using name('jobname1') or names('jobname1','jobname2'). You can also add existing jobs referencing them by name in the same manner.

job{
    name('DSL JOB')
    description('This is a Test Job')
    triggers{
        cron('H/20 7-20 * * 1-5')
    }    
}

view(type:ListView){
    name('DSL-JOBS')
    description('Test View of DSL Job')
    filterBuildQueue()
    filterExecutors()
    jobs{
        name('DSL JOB')
    }
    columns{
        status()
        weather()
        name()
        lastSuccess()
        lastFailure()
        lastDuration()
        buildButton()
        lastBuildConsole()
    }
}
moglimcgrath
  • 357
  • 3
  • 5
0

If you just want to place the generated job in an existing view instead of having to look for it within tens of jobs and without having to recreate views each time, here is a very simple workaround:

Workaround:

  • delete the job generated by your DSL
  • copy (or remember) the exact name of the generated job from the DSL
  • go to the view where you want the new job to reside in
  • create a new empty job with the same name as the new generated job from the DSL.
  • check Add to current view when saving the new empty job
  • run the DSL script and it will update your existing (empty) job with the correct content while leaving it in the desired view.

You might also want to check this answer.

Community
  • 1
  • 1
bosch
  • 1,089
  • 11
  • 16