美文网首页
jenkins之批量复制job系列

jenkins之批量复制job系列

作者: 杀死一只喵 | 来源:发表于2020-06-12 17:28 被阅读0次

脚本附上

import hudson.model.*
    //源view
    def str_view = "yujian-pre"
    //目标view
    def str_new_view = "Kubernetes-pre"
    //源job名称(模糊匹配)
    def str_search = "lubo-"
    //目标job名称(模糊匹配后替换)
    def str_replace = "k8s-"
    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,可加可不加
    //   println("but $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
      //下面开启后会报错,原因是groovy不用重新创建对象,注释后正常
      //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 is success")
    }
    

相关文章

网友评论

      本文标题:jenkins之批量复制job系列

      本文链接:https://www.haomeiwen.com/subject/fmdntktx.html