Jenkins DSL Plugin: How to create a job in an existing jenkins View? Jenkins DSL Plugin: How to create a job in an existing jenkins View? jenkins jenkins

Jenkins DSL Plugin: How to create a job in an existing jenkins View?


You can not add a job to a view that is not managed by the Job DSL. But the views managed by the DSL can contain jobs which are not managed by the DSL.

For example you can have a job called project-a which is managed manually and a job called project-b which is managed by the DSL. And a view managed by the DSL can contain both jobs.

job('project-b') {}listView('project-view') {  jobs {    name('project-a')    name('project-b')  }}

It's not possible to use the Jenkins API to add a job to a view from a DSL script. The job has to exist before it can be added to a view. But when the scripts is executed, the job is not created immediately. All DSL items are created after the script has been processed.

If you do not want the manage the view with the DSL (but you should), you can try to use a filter-based view configuration. E.g. include all jobs with a name matching a regular expression. Or you can use the View Job Filters Plugin to create more complex filters.


Thanks to daspilker, with your provided details I found an acceptable solution.

-> The view filters

Requirements: View Job Filter PLugin

Since DSL created jobs canĀ“t be added to non-DSL created views, I set two view filters.

  • one regexp view filter to exclude the DSL created jobs from the default view (where the jobs would be created if you do not create a DSL view and add the job to it) - select the default view->Edit View->Add Job Filter->regexpJobFilter

  • one regexp view filter on the view, where you want to collect the DSL created jobs.

For now, this works fine. I have DSL created jobs in a manually managed view.

But as daspilker says:

If you do not want the manage the view with the DSL (but you should)

Maybe the experience shows, that i have to switch to the DSL managed views.

enter image description here


I just found a very simple workaround to this problem - although we should keep in mind that @daspilker's answer shows the correct way of managing jobs and views. Just that it was too complicated for me at this time (or been too lazy).

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.

I just came to this solution while remembering that I once deleted (overwritten) an existing job from a view by running a DSL job with the same target name, but at least it stayed in the original view :)