在 Jenkins 上 Groovy 重新启动 Windows 台计算机

Reboot Windows Computers in Groovy on Jenkins

我想在 Groovy 中写一个重启命令来重启 Jenkins 上的 Windows 机器。

我知道关机命令是 shutdown /r /f 但我如何在 Groovy 中使用它?同样,这些服务器将被远程访问。

我不确定它是否有帮助,但您应该能够使用 Jenkins DSL 'bat' 命令在 windows 代理上执行该命令。

def agentNameOrLabelGroup = 'windows'

node (agentNameOrLabelGroup) {
  bat 'shutdown /r /f'
}

我建议提供延迟,以便 Jenkins 的执行上下文有时间释放代理。否则,我预计在代理 运行 时关闭代理会导致作业失败。

如果你需要多台机器,我想我会使用 nodesByLabel 的 jenkins 插件来获取所有 'windows' 台机器,然后循环遍历它们。

def agents = nodesByLabel(label: 'windows')

for (agent in agents) {
  node (agent) {
    bat 'shutdown /r /f'
  }
}

https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#nodesbylabel-list-of-nodes-by-label-by-default-excludes-offline-nodes

祝你好运