简体   繁体   中英

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. 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. I see the JNLPLauncher(), I think that I need to change it some kind of ssh launcher. 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. The important line is the import hudson.plugins.sshslaves.* as the SSHLauncher is part of a plugin.

Source: 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可能是你想要的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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