1

我想获取以给定名称开头的作业列表,然后更新作业可以运行的标签节点。我做了以下并没有成功。我在这里缺少的任何输入。

import hudson.model.*;
import hudson.util.*;
import hudson.model.labels.*;
import jenkins.model.*;
import hudson.FilePath.FileCallable;
import hudson.slaves.OfflineCause;
import hudson.node_monitors.*;


buildableItems = Jenkins.instance.getAllItems.each {job ->
    job.name.startsWith("Automation -")
    println job.fullName;
}

for(item in buildableItems) {
    job.assignedlabel = new LabelAtom('new-label-name')
    item.save()
}
4

2 回答 2

0

您正在起诉的each声明只是遍历元素并在它们上运行闭包,但它不会返回任何内容。相反,您应该使用过滤您的列表findAll,然后对于返回的过滤列表运行代码更改标签:

import jenkins.*
import hudson.model.labels.*;

filtredJobs =  Jenkins.instance.items.findAll { job ->
    job.name.startsWith("Automation -")
}

// Update the label for the filtered jobs
filtredJobs.each { job ->
    job.assignedlabel = new LabelAtom('new-label-name')
    item.save()
}

或者each在同一迭代中使用并运行条件和配置:

import jenkins.*
import hudson.model.labels.*;

Jenkins.instance.items.each { job ->
    if (job.name.startsWith("Automation -")) {
        job.assignedlabel = new LabelAtom('new-label-name')
        item.save()
    }
}
于 2021-08-18T09:53:59.367 回答
0

谢谢@NoamHelmer 我在输出作业名称值上遇到格式错误。由于标签被修改并在作业名称格式上引发错误,但无法继续下一个作业名称。我能够通过 continue 语句修复它。

import hudson.model.labels.*
import jenkins.model.Jenkins  

def views = ["Automation – DEV", "Automation – PROD", "Automation – QA", "Automation – Staging"]
 
for (view in views) {

  def buildableItems = Jenkins.instance.getView(view).items.each {
  println it.fullName

  }
  for (item in buildableItems) {
    try {
      item.assignedLabel = new LabelAtom('New_Label_name') 
    } catch (Exception e) {
      continue;
    }
    println(item.name + " > " + item.assignedLabel)
}

}

再次感谢您的输入!!

于 2021-08-19T15:43:59.383 回答