Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()))
like image 456
Nicholas Smith Avatar asked Dec 05 '25 10:12

Nicholas Smith


1 Answers

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)
like image 140
dragon788 Avatar answered Dec 06 '25 23:12

dragon788



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!