Create a Permanent Agent from Groovy Console

Article ID:218154667
3 minute readKnowledge base

Issue

  • Could I create a new unix node in Jenkins?

  • Could I create it with environment variables defined?

Environment

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 agent 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

/**
* 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."

Launch Method

The definition of the launcher depends on the plugin being used to launch SSH Agents and the versions. There are 2 implementations of SSH launchers:

Following are the description of the Launcher definitons for the different plugin implementations and versions:

CloudBees SSH Build Agents plugin 2.0 and newer
import com.cloudbees.jenkins.plugins.sshslaves.verification.*
import com.cloudbees.jenkins.plugins.sshslaves.SSHConnectionDetails

// Pick one of the strategies from the comments below this line
ServerKeyVerificationStrategy serverKeyVerificationStrategy = new TrustInitialConnectionVerificationStrategy(false)
// = new TrustInitialConnectionVerificationStrategy(false /* "Require manual verification of initial connection" */) // "Manually trusted key verification Strategy"
// = new ManuallyConnectionVerificationStrategy("<your-key-here>") // "Manually provided key verification Strategy"
// = new KnownHostsConnectionVerificationStrategy() // "~/.ssh/known_hosts file Verification Strategy"
// = new BlindTrustConnectionVerificationStrategy() // "Non-verifying Verification Strategy"

// Define a "Launch method": "Launch agents via SSH"
ComputerLauncher launcher = new com.cloudbees.jenkins.plugins.sshslaves.SSHLauncher(
        "host", // Host
        new SSHConnectionDetails(
                "credentialsId", // Credentials ID
                22, // port
                (String)null, // JavaPath
                (String)null, // JVM Options
                (String)null, // Prefix Start Agent Command
                (String)null, // Suffix Start Agent Command
                (boolean)false, // Log environment on initial connect
                (ServerKeyVerificationStrategy) serverKeyVerificationStrategy // Host Key Verification Strategy
        )
)
CloudBees SSH Build Agents plugin 1.7 and older
import com.cloudbees.jenkins.plugins.sshslaves.SSHConnectionDetails

// Define a "Launch method": "Launch agents via SSH"
ComputerLauncher launcher = new com.cloudbees.jenkins.plugins.sshslaves.SSHLauncher(
        "host", // Host
        new SSHConnectionDetails(
                "credentialsId", // Credentials ID
                22, // Port
                (String)null, // JavaPath
                (String)null, // JVM Options
                (String)null, // Prefix Start Agent Command
                (String)null, // Suffix Start Agent Command
                (boolean)false, // Log environment on initial connect
        )
)
SSH Build Agents 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 agents via SSH"
ComputerLauncher launcher = new hudson.plugins.sshslaves.SSHLauncher(
        "host", // Host
        22, // Port
        "credentialsId", // Credentials
        (String)null, // JVM Options
        (String)null, // JavaPath
        (String)null, // Prefix Start Agent Command
        (String)null, // Suffix Start Agent 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
)
SSH Build Agents plugin between 1.7.1 and 1.15
// Define a "Launch method": "Launch agents via SSH"
ComputerLauncher launcher = new hudson.plugins.sshslaves.SSHLauncher(
        "host", // Host
        22, // Port
        "credentialsId", // Credentials
        (String)null, // JVM Options
        (String)null, // JavaPath
        (String)null, // Prefix Start Agent Command
        (String)null, // Suffix Start Agent Command
        (Integer)null, // Connection Timeout in Seconds
        (Integer)null, // Maximum Number of Retries
        (Integer)null // The number of seconds to wait between retries
)