使用 Groovy 修改节点标签的 Jenkins/Hudson CLI API
有谁知道如何以非手动方式修改 Jenkins/Hudson 节点标签?我的意思是,彻底的 API,如该工具提供的 CLI API(当然无需重新启动 Jenkins/Hudson)。
我的猜测是,最好的选择是使用 Groovy 脚本进入 Jenkins/Hudson 的内部。执行如下内容:
java -jar -s HUDSON_URL:8080 groovy /path/to/groovy.groovy
该脚本的内容如下:
for (aSlave in hudson.model.Hudson.instance.slaves) {
labels = aSlave.getAssignedLabels()
println labels
**aSlave.setLabel("blabla")** // this method doesn't exist, is there any other way???
}
提前致谢!
胜利者
Does anyone know how to modify the Jenkins/Hudson node labels in a non-manually way? I mean, thorough an API like the CLI API that this tool offers (without restarting Jenkins/Hudson of course).
My guess is that the best option is using a Groovy script to enter into the Jenkins/Hudson guts. Executing something like:
java -jar -s HUDSON_URL:8080 groovy /path/to/groovy.groovy
Being the content of that script something like:
for (aSlave in hudson.model.Hudson.instance.slaves) {
labels = aSlave.getAssignedLabels()
println labels
**aSlave.setLabel("blabla")** // this method doesn't exist, is there any other way???
}
Thanks in advance!
Victor
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注意:其他答案有点旧,所以 API 可能是从那时起就出现的。
节点标签在 API 中作为单个字符串进行访问,就像在“配置”屏幕中一样。
读取和写入标签: Node.getLabelString() 和 Node.setLabelString(String)。
请注意,您也可以通过以下方式获取有效标签: Node.getAssignedLabels (),它返回一个 LabelAtom 集合,其中包括动态计算的标签,例如“自标签”(代表节点名称本身)。
最后,Node 类上的这些方法可以直接从从属对象访问另外,例如作为系统 Groovy 脚本:
Note: the other answers are a bit old, so it could be that the API has appeared since then.
Node labels are accessed in the API as a single string, just like in the Configure screen.
To read and write labels: Node.getLabelString() and Node.setLabelString(String).
Note that you can get the effective labels as well via: Node.getAssignedLabels(), which returns a Collection of LabelAtom that includes dynamically computed labels such as the 'self-label' (representing the node name itself).
Last, these methods on the Node class are directly accessible from the slave objects also, e.g. as a System Groovy Script:
我找到了一种使用 Groovy Postbuild 插件来做到这一点的方法。
我有一个 Jenkins 作业,它需要一些参数(NodeToUpdate、LabelName、DesiredState)并在 Groovy Postbuild 插件中执行此内容:
I've found a way to do this using the Groovy Postbuild Plugin.
I have a Jenkins job that takes a few parameters (NodeToUpdate, LabelName, DesiredState) and executes this content in the Groovy Postbuild Plugin:
我还没有找到改变奴隶标签的方法。
我已经开始编辑主 config.xml 文件并从 CLI 发出重新加载。
但这有它自己的问题 - 当前运行的任何作业都会丢失,直到下一次 jenkins 重新启动 - 请参阅 https ://issues.jenkins-ci.org/browse/JENKINS-3265
I've not seen a way yet to change the slave label either.
I've taken to editing the main config.xml file and issuing a reload from the CLI.
This has it's own problems though - any jobs currently running are lost until the next jenkins restart - see https://issues.jenkins-ci.org/browse/JENKINS-3265