Create JLNP Jenkins Slave programmatically

I can create a new node through the Jenkins web GUI and then run the node in the container back to the Jenkins wizard using the name and -secret value

ex. docker run jenkinsci/jnlp-slave -url http://jenkins-server:port <secret> <slave name>

Is there a way to programmatically create a Jenkins node and get the secret and slave name, so I don't need to do this through the GUI?

+6
source share
3 answers

You can also create an agent using the REST API . This is especially useful if you have an Apache proxy server (see JENKINS47279 Release ) and no direct access to jenkins (for example, on a corporate network) where the CLI will not work.

API. -

Linux (Bash)

export JENKINS_URL=https://jenkins.intra
export JENKINS_USER=papanito
export JENKINS_API_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxx
export NODE_NAME=testnode
export JSON_OBJECT="{ 'name':+'${NODE_NAME}',+'nodeDescription':+'Linux+slave',+'numExecutors':+'5',+'remoteFS':+'/home/jenkins/agent',+'labelString':+'SLAVE-DOCKER+linux',+'mode':+'EXCLUSIVE',+'':+['hudson.slaves.JNLPLauncher',+'hudson.slaves.RetentionStrategy\$Always'],+'launcher':+{'stapler-class':+'hudson.slaves.JNLPLauncher',+'\$class':+'hudson.slaves.JNLPLauncher',+'workDirSettings':+{'disabled':+true,+'workDirPath':+'',+'internalDir':+'remoting',+'failIfWorkDirIsMissing':+false},+'tunnel':+'',+'vmargs':+'-Xmx1024m'},+'retentionStrategy':+{'stapler-class':+'hudson.slaves.RetentionStrategy\$Always',+'\$class':+'hudson.slaves.RetentionStrategy\$Always'},+'nodeProperties':+{'stapler-class-bag':+'true',+'hudson-slaves-EnvironmentVariablesNodeProperty':+{'env':+[{'key':+'JAVA_HOME',+'value':+'/docker-java-home'},+{'key':+'JENKINS_HOME',+'value':+'/home/jenkins'}]},+'hudson-tools-ToolLocationNodeProperty':+{'locations':+[{'key':+'hudson.plugins.git.GitTool\$DescriptorImpl@Default',+'home':+'/usr/bin/git'},+{'key':+'hudson.model.JDK\$DescriptorImpl@JAVA-8',+'home':+'/usr/bin/java'},+{'key':+'hudson.tasks.Maven\$MavenInstallation\$DescriptorImpl@MAVEN-3.5.2',+'home':+'/usr/bin/mvn'}]}}}"

curl -L -s -o /dev/null -v -k -w "%{http_code}" -u "${JENKINS_USER}:${JENKINS_API_TOKEN}" -H "Content-Type:application/x-www-form-urlencoded" -X POST -d "json=${JSON_OBJECT}" "${JENKINS_URL}/computer/doCreateItem?name=${NODE_NAME}&type=hudson.slaves.DumbSlave"

REST API, , :

curl -L -s -u ${JENKINS_USER}:${JENKINS_API_TOKEN} -X GET ${JENKINS_URL}/computer/${NODE_NAME}/slave-agent.jnlp | sed "s/.*<application-desc main-class=\"hudson.remoting.jnlp.Main\"><argument>\([a-z0-9]*\).*/\1/"

Windows (PS)

Windows Powershell:

$JENKINS_URL="https://jenkins.intra"
$JENKINS_USER="papanito"
$JENKINS_API_TOKEN="xxxxxxxxxxxxxxxxxxxxxxxx"
$NODE_NAME="testnode-ps"

# https://stackoverflow.com/questions/27951561/use-invoke-webrequest-with-a-username-and-password-for-basic-authentication-on-t
$bytes = [System.Text.Encoding]::ASCII.GetBytes("${JENKINS_USER}:${JENKINS_API_TOKEN}")
$base64 = [System.Convert]::ToBase64String($bytes)
$basicAuthValue = "Basic $base64"
$headers = @{ Authorization = $basicAuthValue;  }

$hash=@{
    name="${NODE_NAME}";
    nodeDescription="Linux slave";
    numExecutors="5";
    remoteFS="/home/jenkins/agent";
    labelString="SLAVE-DOCKER linux";
    mode="EXCLUSIVE";
    ""=@(
            "hudson.slaves.JNLPLauncher";
            'hudson.slaves.RetentionStrategy$Always'
        );
    launcher=@{ 
        "stapler-class"="hudson.slaves.JNLPLauncher";
        "\$class"="hudson.slaves.JNLPLauncher";
        "workDirSettings"=@{
            "disabled"="true";
            "workDirPath"="";
            "internalDir"="remoting";
            "failIfWorkDirIsMissing"="false"
        };
        "tunnel"="";
        "vmargs"="-Xmx1024m"
        };
        "retentionStrategy"=@{
            "stapler-class"= 'hudson.slaves.RetentionStrategy$Always';
           '$class'= 'hudson.slaves.RetentionStrategy$Always'
        };
        "nodeProperties"=@{
            "stapler-class-bag"= "true";
            "hudson-slaves-EnvironmentVariablesNodeProperty"=@{
                "env"=@(
                    @{
                        "key"="JAVA_HOME";
                        "value"="/docker-java-home"
                    };
                    @{
                        "key"="JENKINS_HOME";
                        "value"="/home/jenkins"
                    }
                )
            };
            "hudson-tools-ToolLocationNodeProperty"=@{
                "locations"=@(
                    @{
                        "key"= 'hudson.plugins.git.GitTool$DescriptorImpl@Default';
                        "home"= "/usr/bin/git"
                    };
                    @{
                    "key"= 'hudson.model.JDK\$DescriptorImpl@JAVA-8';
                    "home"= "/usr/bin/java"
                    };
                    @{
                        "key"= 'hudson.tasks.Maven$MavenInstallation$DescriptorImpl@MAVEN-3.5.2';
                        "home"= "/usr/bin/mvn"
                    }
                )
            }
        }
    }

#https://stackoverflow.com/questions/17929494/powershell-convertto-json-with-embedded-hashtable
$JSON_OBJECT = $hash | convertto-json  -Depth 5
$JSON_OBJECT

Invoke-WebRequest -Headers $headers -ContentType "application/x-www-form-urlencoded" -Method POST -Body "json=${JSON_OBJECT}" -Uri "${JENKINS_URL}/computer/doCreateItem?name=${NODE_NAME}&type=hudson.slaves.DumbSlave"
+2

create-node CLI .

, JNLP config.xml:

<slave>
  <remoteFS>/opt/jenkins</remoteFS>
  <numExecutors>2</numExecutors>
  <launcher class="hudson.slaves.JNLPLauncher" />
</slave>

create-node CLI SSH:

cat config.xml | java -jar jenkins-cli.jar -s https://jenkins/ create-node my-agent

, XML- , config.xml URL- , . https://jenkins/computer/some-agent-name/config.xml, CLI get-node.

- Jenkins, script CLI groovy:

echo 'println jenkins.model.Jenkins.instance.nodesObject.getNode("my-agent")?.computer?.jnlpMac' \
  | java -jar ~/Downloads/jenkins-cli.jar -s https://jenkins/ groovy =

. , groovy SSH Jenkins 2.46 . CLI.

+9

, Jenkins Client. , JAR IP- .

, . , , . , slave.jar -based.

0

Source: https://habr.com/ru/post/1671838/


All Articles