Create a Permanent Agent from Groovy Console

Issue

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

Environment

  • CloudBees Jenkins Enterprise - Managed Master (CJEMM)
  • CloudBees Jenkins Enterprise- Operations Center (CJEOC)
  • CloudBees Jenkins Team (CJT)
  • CloudBees Jenkins Platform - Client Master (CJPCM)
  • CloudBees Jenkins Platform - Operations Center (CJPOC)
  • Jenkins LTS > 2.7.1
  • Jenkins > 2.7
  • Jenkins SSH Slaves plugin

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 a node to Jenkins is below. It is followed by the examples of a programmatic way of configuring “Launch slave agents via SSH” launch method. It assumes that you have created a Credential required to authenticate with your agent node.

import hudson.model.*
import jenkins.model.*
import hudson.slaves.*
import hudson.slaves.EnvironmentVariablesNodeProperty.Entry
import hudson.plugins.sshslaves.SSHLauncher

// INSERT "Launch Method" SNIPPET HERE

// Define a "Permanent Agent"
Slave agent = new DumbSlave(
        "agent-node",
        "/home/jenkins",
        launcher)
agent.nodeDescription = "Agent node description"
agent.numExecutors = 1
agent.labelString = "agent-node-label"
agent.mode = Node.Mode.NORMAL
agent.retentionStrategy = new RetentionStrategy.Always()

List<Entry> env = new ArrayList<Entry>();
env.add(new Entry("key1","value1"))
env.add(new Entry("key2","value2"))
EnvironmentVariablesNodeProperty envPro = new EnvironmentVariablesNodeProperty(env);

agent.getNodeProperties().add(envPro)

// Create a "Permanent Agent"
Jenkins.instance.addNode(agent)

return "Node has been created successfully."

SSH Slaves Plugin between 1.7.1 and 1.15

// Define a "Launch method": "Launch slave agents via SSH"
ComputerLauncher launcher = new SSHLauncher(
        "host", // Host
        22, // Port
        "credentialsId", // Credentials
        (String)null, // JVM Options
        (String)null, // JavaPath
        (String)null, // Prefix Start Slave Command
        (String)null, // Suffix Start Slave Command
        (Integer)null, // Connection Timeout in Seconds
        (Integer)null, // Maximum Number of Retries
        (Integer)null // The number of seconds to wait between retries
)

SSH Slave Plugin 1.15 and newer

import hudson.plugins.sshslaves.verifiers.*

// Pick one of the strategies from the comments below this line
SshHostKeyVerificationStrategy hostKeyVerificationStrategy = new KnownHostsFileKeyVerificationStrategy()
    //= new KnownHostsFileKeyVerificationStrategy() // Known hosts file Verification Strategy
    //= new ManuallyProvidedKeyVerificationStrategy("<your-key-here>") // Manually provided key Verification Strategy
    //= new ManuallyTrustedKeyVerificationStrategy(false /*requires initial manual trust*/) // Manually trusted key Verification Strategy
    //= new NonVerifyingKeyVerificationStrategy() // Non verifying Verification Strategy

// Define a "Launch method": "Launch slave agents via SSH"
ComputerLauncher launcher = new SSHLauncher(
        "host", // Host
        22, // Port
        "credentialsId", // Credentials
        (String)null, // JVM Options
        (String)null, // JavaPath
        (String)null, // Prefix Start Slave Command
        (String)null, // Suffix Start Slave Command
        (Integer)null, // Connection Timeout in Seconds
        (Integer)null, // Maximum Number of Retries
        (Integer)null, // The number of seconds to wait between retries
        hostKeyVerificationStrategy // Host Key Verification Strategy
)

References

SSHLauncher (Jenkins SSH Slaves plugin API)

DumbSlave (Jenkins core API)

Have more questions? Submit a request

1 Comments

  • 0
    Avatar
    Arjs Work

    Uses a method which is deprecated... Is there a newer version or an other groovy method? 

Please sign in to leave a comment.