You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Jenkins节点列表导入Active Choice参数并仅展示当前用户可用节点

Jenkins Active Choice 参数动态获取用户专属Desktop节点方案

需求说明

现有Jenkins任务配置了名为Computer的Active Choice参数,需要实现以下能力:

  • 拉取所有带有Desktop标签的节点名称
  • 仅向当前登录用户展示和他账号匹配的可用节点,过滤其他用户的节点

原失效脚本

此前使用的脚本仅实现了Desktop标签节点的全量拉取,缺少登录用户匹配逻辑,无法实现用户专属节点过滤,原代码如下:

import hudson.slaves.DumbSlave

def users = []
for (DumbSlave it : Hudson.instance.getSlaves()) {
  if(it.getLabelString().contains("Desktop")) {
    def currAgent = it?.computer?.name
    if(currAgent != null) {
      users.add(currAgent)
     }
  }
}

return users

修正后完整可用脚本

以下脚本新增了当前登录用户校验逻辑,会匹配节点系统属性中存储的用户名与当前登录Jenkins的用户名,仅返回匹配的节点名称:

import jenkins.model.*
import hudson.model.User


def users = []
Jenkins.instance.nodes.each { node ->
  if(node.labelString.contains("Desktop")) {
    def currAgent = node?.computer
    def currAgentName = node?.computer?.name
    if(currAgentName != null) {
      def user = currAgent.systemProperties['user.name']?.toLowerCase()
      if (!users.contains(user) && user==User.current().toString()){
        users.add(currAgentName)
      }
    }
  }
}

return users

核心逻辑说明

  • 遍历Jenkins所有节点,先过滤带有Desktop标签的节点
  • 读取节点系统属性中的user.name字段,转换为小写避免大小写匹配问题
  • 和User.current()获取到的当前登录用户做匹配,仅将匹配成功的节点名称加入返回列表

内容的提问来源于stack exchange,提问作者arielma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 06:15:10