以编程方式创建jenkins JLNP从站 [英] Create jenkins JLNP slave programmatically

查看:224
本文介绍了以编程方式创建jenkins JLNP从站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够通过Jenkins Web GUI创建一个新节点,然后让运行在容器中的节点通过名称和-secret值连接回Jenkins主节点

I am able to create a new node via the Jenkins web GUI and then have the node running in a container connect back to the Jenkins master via the name and -secret value

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

是否可以通过编程方式创建Jenkins节点并获取秘密和从属名称,所以我不必通过GUI来做到这一点?

Is there a way to programmatically create a Jenkins node and get the secret and slave name so I don't have to do it via the GUI?

推荐答案

您还可以使用 REST API .当前面有一个Apache代理时,这特别有用(请参阅问题 JENKINS47279 )并且在CLI无效的情况下(例如在公司网络中)无法直接访问jenkins.

You can also create an agent using the REST API. This is especially useful when having an apache proxy in front (see issue JENKINS47279) and no direct access to the jenkins otherwise (e.g. in a corporate network) where CLI will not work.

我建议为此创建API令牌目的.然后你可以做这样的事情

I recommend to create an API token for this purpose. Then you can do something like this

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签出获取代理机密

In order to get the agent secret via REST API checkout this, which would look something like this:

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)

这是我使用Powershell的Windows解决方案:

Windows (PS)

And here my solution for Windows using 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"

这篇关于以编程方式创建jenkins JLNP从站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆