signed

QiShunwang

“诚信为本、客户至上”

如何为linux节点配置jenkins-agent,以用于java web启动

2021/6/9 9:15:17   来源:

如何为linux节点配置jenkins-agent,以用于java web启动

1:下载agent.jar到linux某个文件夹下,然后在所在目录运行:

java -jar /data/jenkins_work_space/agent.jar -jnlpUrl ***/slave-agent.jnlp -secret ****workDir /data/jenkins_work_space

2:启动之后断开会关闭且不会开机自启,所以使用脚本配置成服务:

#!/bin/bash



CMD='java启动命令'
JNLPURL=$(echo $CMD | sed 's/.*jnlpUrl \(.*\)/\1/'|  cut -d ' ' -f1 )
WORKDIR=$(echo $CMD | sed 's/.*workDir \(.*\)/\1/'|  cut -d ' ' -f1 |  sed 's/"//g')
if [[ "$CMD" =~ java &&  "$CMD" =~ jnlpUrl &&  "$CMD" =~ secret && "$CMD" =~ workDir ]];then
	echo "开始配置jenkins-agent服务"else
	echo "ERROR:请将节点连接jenkins的java启动命令填入脚本CMD=''的引号中"
	exit 1
fi
mkdir -p $WORKDIR
wget ${JNLPURL%%computer*}jnlpJars/agent.jar -P ${WORKDIR}
chmod 755 ${WORKDIR}/agent.jar

cat>/usr/lib/systemd/system/jenkins-agent.service<<EOF
[Unit]
Description=The Jenkins Agent Server

[Service]
User=root
ExecStart=/usr/local/bin/jenkins-agent start
ExecReload=/usr/local/bin/jenkins-agent restart
ExecStop=/usr/local/bin/jenkins-agent stop
Restart=always

[Install]
WantedBy=multi-user.target

EOF

cat>/usr/local/bin/jenkins-agent<<EOF
#!/bin/bash

start()
{    
    ${CMD/agent.jar/$WORKDIR/agent.jar}
}

case "\$1" in
start)
    start
    ;;
stop)
    stop
    ;;
restart)
    stop
    start
    ;;
*)
    echo "usage: \$0 start|stop|restart"
    exit 0;
esac
exit
EOF

chmod 755 /usr/local/bin/jenkins-agent
systemctl daemon-reload
systemctl start jenkins-agent
systemctl enable jenkins-agent
result=$(systemctl is-active jenkins-agent)
if [[ $result == 'active' ]];then
        echo "success"
else
        nohup ${CMD/agent.jar/$WORKDIR/agent.jar} > /dev/null 2>&1 &
fi

然后运行这个脚本。
最后使用下面命令查看状态。

systemctl status jenkins-agent