為研究在kubernetes上的CICD,先在物理環境下安裝個JenKins熱熱身。
- 安裝Jenkins
在官網https://jenkins.io/下載war包,我的是http://mirrors.jenkins.io/war/latest/jenkins.war
然后運行(必須jdk1.8版本)
java -jar jenkins.war
如果端口沖突,加上參數
java -jar jenkins.war --httpPort=9090
- 插件配置
在http://localhost:9090/的界面,選擇Manage Plugins
安裝 ant plugin,git plugin,maven integration plugin,ssh plugin, publish to ssh plugin.
選擇Manage Jenkins->Global Tool Configuration
配置ant,maven,jdk的地址(安裝過程略)
- 構建配置
新建一個item,叫jenkins_test,並選擇maven project.
在Source Code Management中選擇git,輸入網上大俠的代碼地址
https://github.com/bingyue/easy-springmvc-maven
Build Trigger等保持不變,但在Post step中,加入代碼
tomcat_path=/home/ericnie/apache-tomcat-7.0.82/ project=easy-springmvc-maven war_name=easy-springmvc-maven.war war_path=http://192.168.0.104:8080/jenkins/job/jeekins-test/ws/target server_port=9080 file_path=/home/ericnie/.jenkins/workspace/jenkins_test/target/ now=$(date +"%Y%m%d%H%M%S") echo "the shell execute time is ${now}" echo `lsof -n -P -t -i :${server_port}` tomcat_pid=`lsof -n -P -t -i :${server_port}` echo "the tomcat_pid is ${tomcat_pid}" if [ "${tomcat_pid}" != "" ]; then kill -9 $tomcat_pid echo "kill the server" fi echo "rm ${tomcat_path}/webapps/${war_name}" rm ${tomcat_path}/webapps/${war_name} echo "rm -rf ${tomcat_path}/webapps/${project}" rm -rf ${tomcat_path}/webapps/${project} cd $file_path if [ -f ${war_name} ]; then cp ${war_name} ${tomcat_path}/webapps else echo "${war_name} unexists" fi export JAVA_HOME=/home/ericnie/jdk1.8.0_151 export CATALINA_HOME2=/home/ericnie/apache-tomcat-7.0.82/ export CATALINA_BASE2=/home/ericnie/apache-tomcat-7.0.82/ /home/ericnie/apache-tomcat-7.0.82//bin/startup.sh echo "server restarted"
這段腳本還需要修改一下,目前腳本運行成功的條件是需要tomcat已經啟動,並且在webapps下有個easy-springmvc-maven.war的包
然后需要再安裝一個tomcat,監聽在9080.(略)
選擇Build Now
選擇最新的build number,選擇console output
基於日志信息進行問題定位直到成功。
+ echo 'kill the server' kill the server + echo 'rm /home/ericnie/apache-tomcat-7.0.82//webapps/easy-springmvc-maven.war' rm /home/ericnie/apache-tomcat-7.0.82//webapps/easy-springmvc-maven.war + rm /home/ericnie/apache-tomcat-7.0.82//webapps/easy-springmvc-maven.war + echo 'rm -rf /home/ericnie/apache-tomcat-7.0.82//webapps/easy-springmvc-maven' rm -rf /home/ericnie/apache-tomcat-7.0.82//webapps/easy-springmvc-maven + rm -rf /home/ericnie/apache-tomcat-7.0.82//webapps/easy-springmvc-maven + cd /home/ericnie/.jenkins/workspace/jenkins_test/target/ + '[' -f easy-springmvc-maven.war ']' + cp easy-springmvc-maven.war /home/ericnie/apache-tomcat-7.0.82//webapps + export JAVA_HOME=/home/ericnie/jdk1.8.0_151 + JAVA_HOME=/home/ericnie/jdk1.8.0_151 + export CATALINA_HOME2=/home/ericnie/apache-tomcat-7.0.82/ + CATALINA_HOME2=/home/ericnie/apache-tomcat-7.0.82/ + export CATALINA_BASE2=/home/ericnie/apache-tomcat-7.0.82/ + CATALINA_BASE2=/home/ericnie/apache-tomcat-7.0.82/ + /home/ericnie/apache-tomcat-7.0.82//bin/startup.sh Tomcat started. + echo 'server restarted' server restarted Finished: SUCCESS
然后,運行http://localhost:9080/easy-springmvc-maven訪問測試。