jenkins批量复制已有构建任务脚本

新建很多测试构建任务后,再重复建生产构建任务嫌麻烦,网上找了个脚本测试成功,在系统脚本的

import hudson.model.*
	//源view
	def str_view = "01--test"
	//目标view
	def str_new_view = "02--prod"
	//源job名称(模糊匹配)
	def str_search = "xxx-test"
	//目标job名称(模糊匹配后替换)
	def str_replace = "xxx-prod"
	def view = Hudson.instance.getView(str_view)
	//copy all projects of a view
	for(item in view.getItems())
	{
	  //跳过未模糊匹配到的构建 任务
      if (!item.getName(). contains(str_search)) {
        println(" $item.name ignore ")
        continue
      }
      //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")
	}

这样只需要再对生产的构建任务修改一下配置即可

你可能感兴趣的:(服务器)