create-agent-node-from-groovy

Issue

Could I create a new unix node in Jenkins?
Could I create it with environment variables defined?

Environment

  • CloudBees Jenkins Enterprise > 1.609.1.1
  • Jenkins LTS > 1.609.1
  • Jenkins > 1.600

Resolution

It is possible to manipulate nodes using a groovy script, you could create/edit nodes and add it to Jenkins, also you could define environment variables, tools, … and other properties available from UI.

Example groovy script to add node to Jenkins.

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)
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.