简体   繁体   English

使用Jenkins Groovy脚本创建Unix Slave

[英]Create a Unix Slave with a Jenkins Groovy script

I would like to know how to create a unix slave with a Jenkins Groovy script and launch the slave. 我想知道如何使用Jenkins Groovy脚本创建一个unix slave并启动slave。 I have the following code, and it works great. 我有以下代码,它工作得很好。 However, it does not create the ssh option in the slave nor does it launch the slave. 但是,它不会在从属设备中创建ssh选项,也不会启动从属设备。 I see the JNLPLauncher(), I think that I need to change it some kind of ssh launcher. 我看到了JNLPLauncher(),我认为我需要将它改成某种ssh启动器。 I would appreciate any help even if it is pointing to the documentation which I can't seem to find. 即使它指向我似乎无法找到的文档,我将不胜感激任何帮助。 Additionally, this code is meant to start the slave at the time of build and delete the slave after the build is over. 此外,此代码用于在构建时启动从属,并在构建结束后删除从属。 I need to do dynamic slave assignment according to a parameter selected by the user. 我需要根据用户选择的参数进行动态从属分配。 Therefore, any other ideas on how to accomplish this is appreciated. 因此,对于如何实现这一点的任何其他想法表示赞赏。

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()))

This is an answer I found on the Cloudbees support site that got me where I needed to be. 这是我在Cloudbees支持网站上找到的答案,它让我得到了我需要的地方。 The important line is the import hudson.plugins.sshslaves.* as the SSHLauncher is part of a plugin. 重要的一行是import hudson.plugins.sshslaves.*因为SSHLauncher是插件的一部分。

Source: https://support.cloudbees.com/hc/en-us/articles/218154667-create-agent-node-from-groovy 资料来源: 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)

RemoteLauncher可能是你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM