简体   繁体   中英

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

I found the following question regarding job creation and adding to a newly created view with the DSL plugin.

Adding job to newly created view.

How can i add a created job to an existing view with the DSL plugin? I couldn´t find any hint in the documentation. Maybe it is too obvious that i can´t see the solution?

Although i read, that creating a view will cause a re-creation if the view already exists. What means that for the existing projects under this view?

Thanks for your help.

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. Eg 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.

在此输入图像描述

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM