jenkins复制视图

作者:Garany 发布于:2022-12-20 分类:破万卷书
import hudson.model.*
	//源view
	def str_view = "测试"
	//目标view
	def str_new_view = "线上"
	//源job名称(模糊匹配)
	def str_search = "abc-"
	//目标job名称(模糊匹配后替换)
	def str_replace = "xyz-" 
	def view = Hudson.instance.getView(str_view)
	//copy all projects of a view
	for(item in view.getItems())
	{
	  //跳过未模糊匹配到的构建任务
      if (!item.getName().contains(str_search)) {
      // 说明文字,表示跳过未匹配到的job,可加可不加
        continue
      }
      //create the new project name
	  newName = item.getName().replace(str_search, str_replace)
	  def job
	  try {
	  	//因为第一次导入后报错,所以添加了try-catch 跳过已存在的job
	  	job = Hudson.instance.copy(item, newName)
	  } catch(IllegalArgumentException e) {
	     println("$newName job is exists")
	     continue
	  } catch(Exception e) {
	    println(e.toString())
	    continue
	  }
	  job.disabled = true
	  job.save() 
	  Hudson.instance.getView(str_new_view).add(job)
	  println(" $item.name copied as $newName is success")
	}
标签: linux Jenkins

我来说说