groovy - 使用 Jenkins Groovy 脚本创建 Unix Slave

标签 groovy jenkins jenkins-job-dsl

我想知道如何使用 Jenkins Groovy 脚本创建一个 unix 从站并启动该从站。我有以下代码,效果很好。但是,它不会在从站中创建 ssh 选项,也不会启动从站。我看到了 JNLPLauncher(),我认为我需要将它更改为某种 ssh 启动器。即使它指向我似乎找不到的文档,我也将不胜感激。此外,此代码旨在在构建时启动从站,并在构建结束后删除从站。我需要根据用户选择的参数进行动态从属分配。因此,对于如何实现这一点的任何其他想法表示赞赏。

import jenkins.model.*
import hudson.model.*
import hudson.slaves.*

Jenkins.instance.addNode(
  new DumbSlave(
    "test-script",
    "test slave description",
    "/export/home/pe-deploy/",
    "1",
    Node.Mode.NORMAL,
    "test-slave-label",
    new JNLPLauncher(),
    new RetentionStrategy.Always(),
    new LinkedList()))

最佳答案

这是我在 Cloudbees 支持网站上找到的答案,它让我到达了我需要的地方。重要的一行是 import hudson.plugins.sshslaves.* 因为 SSHLauncher 是插件的一部分。

来源:https://support.cloudbees.com/hc/en-us/articles/218154667-create-agent-node-from-groovy

import jenkins.model.*
import hudson.model.*
import hudson.slaves.*
import hudson.plugins.sshslaves.*
import java.util.ArrayList;
import hudson.slaves.EnvironmentVariablesNodeProperty.Entry;

  List<Entry> env = new ArrayList<Entry>();
  env.add(new Entry("key1","value1"))
  env.add(new Entry("key2","value2"))
  EnvironmentVariablesNodeProperty envPro = new EnvironmentVariablesNodeProperty(env);
  Slave slave = new DumbSlave(
                    "agent-node","Agent node description",
                    "/home/jenkins",
                    "1",
                    Node.Mode.NORMAL,
                    "agent-node-label",
                    new SSHLauncher("agenNode",22,"user","password","","","","",""),
                    new RetentionStrategy.Always(),
                    new LinkedList())
  slave.getNodeProperties().add(envPro)
  Jenkins.instance.addNode(slave)

关于groovy - 使用 Jenkins Groovy 脚本创建 Unix Slave,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27550348/

相关文章:

groovy - 使用IntegrationSpec类时,nebula-test无法加载项目插件类

Jenkins 获取中止构建的用户

git - 有没有办法提高 Azure DevOps 的速率限制?

Jenkins Jobs DSL - 如何定义属性文件

jenkins - 如何使用 Jenkins DSL 插件启用 SCM 轮询

groovy - 如何重构常见的Jenkins JobDSL代码?

groovy - @TypeChecked 和 @CompileStatic 之间的区别

maven - 在我的工作站上重复 Jenkins groovy yaml 读写

jenkins - 如何从基于 Groovy 的 Job DSL 执行/运行初始管道

git - Jenkins 也为共享库更改运行轮询或推送触发作业