#!/bin/bash
echo "get real Ip..."
currentIp=`hostname -I`
jenkinsuser="jenkins用户名"
jenkinspassword="jenkins密码"
jenkinsurl="http://jenkinsRootUrl"
echo "currentIp is ${currentIp}"
currentIps=(${currentIp})
echo "the first ip is : ${currentIps[0]}"
localIp=${currentIps[0]}
jenkinsnodetesturl="http://jenkinsRootUrl/computer/${localIp}/api/json"
status_code=$(curl --write-out %{http_code} --silent --output /dev/null ${jenkinsnodetesturl})
NODE_NAME=${localIp}
JSON_OBJECT="{ 'name':+'${NODE_NAME}',+'nodeDescription':+'Android pkg docker agent',+'numExecutors':+'5',+'remoteFS':+'/root/jenkinsspace',+'labelString':+'docker-android-pkg',+'mode':+'NORMAL',+'':+['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}},+'retentionStrategy':+{'stapler-class':+'hudson.slaves.RetentionStrategy\$Always',+'\$class':+'hudson.slaves.RetentionStrategy\$Always'},+'nodeProperties':+{'stapler-class-bag':+'true'}}"
echo "jsonObject is ${JSON_OBJECT}"
if [[ "$status_code" -ne 200 ]] ; then
curl -L -s -o /dev/null -v -k -w "%{http_code}" -u "${jenkinsuser}:${jenkinspassword}" -H "Content-Type:application/x-www-form-urlencoded" -X POST -d "json=${JSON_OBJECT}" "${jenkinsurl}/computer/doCreateItem?name=${NODE_NAME}&type=hudson.slaves.DumbSlave"
secret=`curl -L -s -u ${jenkinsuser}:${jenkinspassword} ${jenkinsurl}computer/${localIp}/slave-agent.jnlp | sed "s/.*<application-desc main-class=\"hudson.remoting.jnlp.Main\"><argument>\([a-z0-9]*\).*/\1/"`
echo "secret is ${secret}"
wget -O ~/agent.jar http://xxx/agent.jar
mkdir -p ~/jenkinsspace
nohup java -jar ~/agent.jar -jnlpUrl "${jenkinsurl}/computer/${localIp}/slave-agent.jnlp" -secret "${secret}" -workDir ~/jenkinsspace > /root/jnlp.log &
echo "create node and start agent success..."
tail -f /root/jnlp.log
else
testResp=`curl ${jenkinsnodetesturl} | jq ".offline"`
if [[ "${testResp}" == 'true' ]];then
echo "agent is offline , staring..."
secret=`curl -L -s -u ${jenkinsuser}:${jenkinspassword} ${jenkinsurl}computer/${localIp}/slave-agent.jnlp | sed "s/.*<application-desc main-class=\"hudson.remoting.jnlp.Main\"><argument>\([a-z0-9]*\).*/\1/"`
echo "secret is ${secret}"
wget -O ~/agent.jar http://xxx/agent.jar
mkdir -p ~/jenkinsspace
nohup java -jar ~/agent.jar -jnlpUrl "${jenkinsurl}/computer/${localIp}/slave-agent.jnlp" -secret "${secret}" -workDir ~/jenkinsspace > /root/jnlp.log &
echo "starting agent success..."
tail -f /root/jnlp.log
fi
exit 0
fi
持续集成-shell自动创建jenkins节点并通过jnlp链接jenkins服务器
于 2023-08-17 17:03:48 首次发布