jenkins批量复制view中所有job

jenkins批量复制view中所有job

需要将jenkins中某一个view中的所有job都复制到另外一个view中,一个一个复制有点蛋疼,所以查询了一下资料,使用groovy scripts 来实现这个功能

  • 新建view
  • 打开系统管理 -> 脚本命令行
	import hudson.model.*
	//源view
	def str_view = "ZZ"
	//目标view
	def str_new_view = "ZZ_dev"
	//源job名称(模糊匹配)
	def str_search = "demo"
	//目标job名称(模糊匹配后替换)
	def str_replace = "zz_dev"
	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")
	}

参考链接:

  1. Clone all projects in a View
  2. How to programmatically add a job to a view in hudson
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值