在创建Jenkins集群时需要通过Manage Node菜单手动创建节点,Jenkins也提供了API可以动态地创建/更新/删除节点等操作,这篇文章通过具体的示例来进行说明节点的创建方法。
这篇文章示例中,将会创建如下类型的节点:
设定项 | 设定项说明 | 设定值 |
---|---|---|
name | 节点名称 | agent001 |
description | 节点描述 | jenkins agent 001 |
of executor | 并行可执行的Job数量 | 1(缺省值) |
Remote root directory | Agent的远程数据 | /tmp/jenkins/agent001(必填字段) |
Launch method | 启动方式 | Launch agent by connecting it to the master |
Availability | 可用模式 | Keep this agent online as much as possible |
使用如下示例代码获取Jenkins-Crumb,为使用API方式生成三个Stage的Pipeline示例作准备。
liumiaocn:jenkins liumiao$ jenkins_host_url=http://localhost:32002
liumiaocn:jenkins liumiao$ user_passwd="root:liumiaocn"
liumiaocn:jenkins liumiao$ jenkins_crumb=`curl -u $user_passwd ${jenkins_host_url}'/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)' 2>/dev/null`
liumiaocn:jenkins liumiao$ echo $jenkins_crumb
Jenkins-Crumb:d5b90aaebb5c75b56e24f3bfebc8e43d
liumiaocn:jenkins liumiao$
根据上述设定要求,创建节点的json文件如下所示,请根据环境和需要进行更改,比如Jenkins-Crumb的设定值。
liumiaocn:jenkins liumiao$ cat demo/node/json/agent.json
{
"name": "agent001",
"nodeDescription": "jenkins agent 001",
"numExecutors": "1",
"remoteFS": "/tmp/jenkins/agent001",
"labelString": "",
"mode": "NORMAL",
"": [
"hudson.slaves.JNLPLauncher",
"hudson.slaves.RetentionStrategy$Always"
],
"launcher": {
"stapler-class": "hudson.slaves.JNLPLauncher",
"$class": "hudson.slaves.JNLPLauncher",
"workDirSettings": {
"disabled": false,
"workDirPath": "",
"internalDir": "remoting",
"failIfWorkDirIsMissing": false
},
"tunnel": "",
"vmargs": ""
},
"retentionStrategy": {
"stapler-class": "hudson.slaves.RetentionStrategy$Always",
"$class": "hudson.slaves.RetentionStrategy$Always"
},
"nodeProperties": {
"stapler-class-bag": "true"
},
"type": "hudson.slaves.DumbSlave",
"Jenkins-Crumb": "d5b90aaebb5c75b56e24f3bfebc8e43d"
}
liumiaocn:jenkins liumiao$
使用如下命令创建节点
liumiaocn:jenkins liumiao$ curl -u "${user_passwd}" -H "Content-Type:application/x-www-form-urlencoded" -H "${jenkins_crumb}" -X POST -d "json=${json_agent}" "${jenkins_host_url}/computer/doCreateItem?name=agent001&type=hudson.slaves.DumbSlave"
liumiaocn:jenkins liumiao$ echo $?
0
liumiaocn:jenkins liumiao$
再次使用api进行查询,可以看到已经生成了agent001的节点信息了,详细如下所示
liumiaocn:jenkins liumiao$ curl "${jenkins_host_url}/computer/api/json?pretty=true"
{
"_class" : "hudson.model.ComputerSet",
"busyExecutors" : 0,
"computer" : [
{
"_class" : "hudson.model.Hudson$MasterComputer",
"actions" : [
{
}
],
"assignedLabels" : [
{
"name" : "master"
}
],
"description" : "the master Jenkins node",
"displayName" : "master",
"executors" : [
{
},
{
}
],
"icon" : "computer.png",
"iconClassName" : "icon-computer",
"idle" : true,
"jnlpAgent" : false,
"launchSupported" : true,
"loadStatistics" : {
"_class" : "hudson.model.Label$1"
},
"manualLaunchAllowed" : true,
"monitorData" : {
},
"numExecutors" : 2,
"offline" : false,
"offlineCause" : null,
"offlineCauseReason" : "",
"oneOffExecutors" : [
],
"temporarilyOffline" : false
},
{
"_class" : "hudson.slaves.SlaveComputer",
"actions" : [
{
}
],
"assignedLabels" : [
{
"name" : "agent001"
}
],
"description" : "jenkins agent 001",
"displayName" : "agent001",
"executors" : [
{
}
],
"icon" : "computer-x.png",
"iconClassName" : "icon-computer-x",
"idle" : true,
"jnlpAgent" : true,
"launchSupported" : false,
"loadStatistics" : {
"_class" : "hudson.model.Label$1"
},
"manualLaunchAllowed" : true,
"monitorData" : {
},
"numExecutors" : 1,
"offline" : true,
"offlineCause" : null,
"offlineCauseReason" : "",
"oneOffExecutors" : [
],
"temporarilyOffline" : false,
"absoluteRemotePath" : null
}
],
"displayName" : "Nodes",
"totalExecutors" : 2
}liumiaocn:jenkins liumiao$