使用groovy在Jenkins中设置上游项目 [英] Setting an upstreamproject in Jenkins using groovy

查看:435
本文介绍了使用groovy在Jenkins中设置上游项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个FreeStyleProject复制到一个新的作业中,然后使用groovy为这个新作业设置一个新的上游项目。
我在类FreeStyleProject 上找不到任何方法让我这样做:
设置一个新的上游项目。有没有其他的方法或技巧可以实现这一点?

  def x =testproj
def hi = hi .getItem(x)
hi.copy(x,Copy+ x);
def newjob = hi.getItem(Copy+ x)
//newjob.setUpstreamProject(\"bar)//<< - ?
def di = newjob.getUpstreamProjects()

for(y in di){

println(y.name)
}

更新:
澄清:

工作A ====>工作B(A是上游)



我想复制两份工作,所以我得到我



工作A'和工作B',然后设置A'或B',所以我得到
A'=====>工作B'(A'在上游)



我想我需要删除一个构建触发器并创建一个新的构建触发器(这就是创建上游/下游概念的原因)


通过两个FreeStyle项目和一个下游样本,这个剪辑将找到TestTop并将它们设置为现有的TopTest2。


这个概念是通过 getPublishersList 获得一个项目的构建器,然后
获得这些构件的hudson.tasks.BuildTrigger.class。



对于其中的每一个都执行

publishlist。删除(aa)
publishlist。添加( new hudson.tasks.BuildTrigger(TestTop2,false))

  def hudsonInstance = hudson.model.Hudson.instance 
hudsonInstance.getItems(hudson.model.Project).each {project - >

if(project.displayName ==TestTop){
println(project.displayName)
def di = project.getUpstreamProjects()
def triggers = project .getBuildTriggerUpstreamProjects()
for(z in triggers){
println(triggers+ z.getDisplayName())
$ b $ def pubList = z.getPublishersList()
(for pub列表中的w){

println(w)

def buildTrigger = w.grep(hudson.tasks.BuildTrigger.class)

println(BT+ buildTrigger)
(aa in buildTrigger){
println(aa+ aa.getChildProjectsValue())
def newtop = hudsonInstance.getItem(TestTop2 )
pubList.remove(aa)
pubList.add(new hudson.tasks.BuildTrigger(TestTop2,false))
}
}
}


hudsonInstance.rebuildDependencyGraph()


I would like to copy a FreeStyleProject to a new job and then set a new upstreamproject to this new job using groovy. I can't find any method on Class FreeStyleProject that let me do that: set a new upstreamproject. Is there some other method or trick that can accomplish this?

def x = "testproj"
def hi=hi.getItem(x)
hi.copy(x, "Copy"+x); 
def newjob=hi.getItem("Copy"+x)
//newjob.setUpstreamProject("bar") //<<--?
  def di = newjob.getUpstreamProjects()

     for ( y in di ) {

      println(y.name)
}

Update: For clarification:

Job A ====> Job B (A is upstream)

I want to copy Both jobs so i get i

Job A' and Job B' and then set A' or B' so I get A' =====> Job B' (A' is upstream)

I think I need to remove a build trigger and create a new build trigger (that is what creates the upstream/downstream concept it seems)

解决方案

With a sample of Two FreeStyle projects and one downstream, this snip will found the TestTop and set them to the existing TopTest2. The concept is to get the buildtriggers with the help of getPublishersList on a project and then get the hudson.tasks.BuildTrigger.class of these.

For each of these do a
publishlist.remove(aa) publishlist.add( new hudson.tasks.BuildTrigger("TestTop2", false))

def hudsonInstance = hudson.model.Hudson.instance 
hudsonInstance.getItems(hudson.model.Project).each {project ->

  if (project.displayName== "TestTop"){
    println(project.displayName)
    def di = project.getUpstreamProjects()
    def triggers=project.getBuildTriggerUpstreamProjects()
    for (z in triggers){
      println("triggers " + z.getDisplayName())

      def pubList = z.getPublishersList()
      for(w in pubList){

        println(w)

        def buildTrigger = w.grep(hudson.tasks.BuildTrigger.class) 

        println("BT " + buildTrigger)
        for(aa in buildTrigger){
          println("aa " + aa.getChildProjectsValue())
          def newtop =  hudsonInstance.getItem("TestTop2")
          pubList.remove(aa)
          pubList.add( new hudson.tasks.BuildTrigger("TestTop2", false)) 
        }               
      }
    }   
  }
}
hudsonInstance.rebuildDependencyGraph()

这篇关于使用groovy在Jenkins中设置上游项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆