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")
	}

這樣只需要再對生產的構建任務修改一下配置即可

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章