jenkins批量复制view中所有job

2025-10-20 06:42:28

1、新建view

jenkins批量复制view中所有job

2、打开系统管理

jenkins批量复制view中所有job

3、打开脚本命令行

jenkins批量复制view中所有job

4、//源view

def str_view = "dev-XX"

//目标view

def str_new_view = "dev-FF"

//源job名称(模糊匹配)

def str_search = "dev-XX"

//目标job名称(模糊匹配后替换)

def str_replace = "dev-FF"

def view = Hudson.instance.getView(str_view)

//copy all projects of a view

for(item in view.getItems())

{

  //create the new project name

  newName = item.getName().replace(str_search, str_replace)

  // copy the job, disable and save it

  def job

  try {

  //因为第一次导入后报错,所以添加了try-catch 跳过已存在的job

  job = Hudson.instance.copy(item, newName)

  } catch(IllegalArgumentException e) {

     println(e.toString())

     println("$newName job is exists")

     continue

  } catch(Exception e) {

    println(e.toString())

    continue

  }

  job.disabled = true

  job.save() 

  // update the workspace to avoid having two projects point to the same location

  AbstractProject project = job

  def new_workspace = project.getCustomWorkspace().replace(str_search, str_replace)

  project.setCustomWorkspace(new_workspace)

  project.save()

  //add job to view

  Hudson.instance.getView(str_new_view).add(job)

  println(" $item.name copied as $newName")

}

jenkins批量复制view中所有job

5、刷新新建的视图,看看项目被复制过来没有。如果没有,则编辑视图

jenkins批量复制view中所有job

6、复制的项目会出现在列表中,勾选归类到该视图下

jenkins批量复制view中所有job

7、目的已经实现,偷懒成功,耶耶😚

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢