jenkins自动部署tomcat

关于部署的3种思路:

  • 远程部署(jenkins编译部署到远程服务器):
    • 安装ssh插件
    • ssh插件配置
  • 添加远程jenkins服务器节点:
  • 本地部署(与jenkins在同一服务器):

关于maven构建的两种选择:

    • 直接选择maven插件
      • 自带插件():
      • 插件配置(或直接新建maven项目):
 
 
    • 添加shell编译过程,通过shell调用服务器的maven环境
jenkins配置:
  1. export BUILD_ID=XXXXXX # 必须得加,原因未知,否则无法启动tomcat
  2. export project_dir=$WORKSPACE
  3. export target_dir=/export/App/zgbcms.m.jd.com
  4. export do_dir=/export/Domains/zgbcms.m.jd.com/server1/bin
  5. export war_path="cms-web"
  6. export war_file="cms-web.war"
  7. # 接受6个参数,mvn_parama为编译什么版本,接受参数dovolopment,test
  8. export mvn_parama="test"
  9. /home/lyz/bushu.sh
环境部署shell:
  1. #!/bin/bash
  2. # 自动编译部署脚本,jenkins调用
  3. # 编译后生成文件war,清空并解压到指定文件夹,重启服务器
  4. export M2_HOME=/export/servers/apache-maven-2.2.1
  5. export JAVA_HOME=/export/servers/jdk1.6.0_25
  6. export JAVA_BIN=/export/servers/jdk1.6.0_25/bin
  7. export PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/bin
  8. export CLASSPATH=.:/lib/dt.jar:/lib/tools.jar
  9. export JAVA_OPTS="-Djava.library.path=/usr/local/lib -server -Xms1024m -Xmx1024m -XX:MaxPermSize=256m -Djava.awt.headless=true -Dsun.net.client.defaultConnectTimeout=60000 -Dsun.net.client.defaultReadTimeout=60000 -Djmagick.systemclassloader=no -Dnetworkaddress.cache.ttl=300 -Dsun.net.inetaddr.ttl=300"
  10. export JAVA_HOME JAVA_BIN PATH CLASSPATH JAVA_OPTS
  11. export PATH=$PATH:$M2_HOME/bin
  12. # 测试用传参
  13. #export project_dir=/root/.jenkins/workspace/zgb_server_master
  14. #export target_dir=/export/App/zgb.m.jd.com
  15. #export do_dir=/export/Domains/zgb.m.jd.com/server1/bin
  16. #export war_path="jd-qqgserver-web"
  17. #export war_file="jd-qqgserver-web.war"
  18. project_dir=$project_dir
  19. target_dir=$target_dir
  20. do_dir=$do_dir
  21. war_path=$war_path
  22. war_file=$war_file
  23. mvn_parama=$mvn_parama
  24. echo $project_dir
  25. echo $target_dir
  26. echo $do_dir
  27. echo $war_path
  28. echo $war_file
  29. echo $mvn_parama
  30. function mvn_build()
  31. {
  32. if[ $# -eq 6 ] # 校验参数是否为6个
  33. then
  34. if[-d "$project_dir/"]&&[-d "$target_dir/"]&&[-d "$do_dir/"]# 校验文件夹是否存在
  35. then
  36. cd $project_dir
  37. if["$mvn_parama"=="development"]
  38. then
  39. echo "编译开发版本!"
  40. mvn clean install -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -P artifactory,development
  41. elif["$mvn_parama"=="test"]
  42. then
  43. echo "编译test版本!"
  44. mvn clean install -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -P artifactory,test
  45. else
  46. echo "未指定配置文件参数!会导致服务器无法访问!"
  47. exit 1
  48. fi
  49. rm -rf $target_dir/*
  50. cp -r $project_dir/$war_path/target/$war_file $target_dir/
  51. echo $target_dir/$war_file
  52. cd $target_dir
  53. unzip -oq $war_file
  54. rm -rf $target_dir/$war_file
  55. $do_dir/stop.sh
  56. if[ $?==0]
  57. then
  58. echo "服务器停止成功!"
  59. else
  60. echo "服务器停止失败!"
  61. fi
  62. $do_dir/start.sh
  63. if[ $?==0]
  64. then
  65. echo "服务器启动成功!"
  66. else
  67. echo "服务器启动失败!"
  68. exit 1# 退出状态为1,传给jenkins让任务失败
  69. fi
  70. else
  71. echo "Folder $* is not exist!"
  72. exit 1
  73. fi
  74. else
  75. echo "$0 is Errors! Parama is $# not 6!"
  76. exit 1
  77. fi
  78. }
  79. # 接受6个参数,mvn_parama为编译什么版本,接受参数dovolopment,beta,production,test 或其他,其他为默认
  80. mvn_build $project_dir $target_dir $do_dir $war_path $war_file $mvn_parama
 





附件列表

 

posted @ 2017-01-23 12:21  测试散人  阅读(1482)  评论(0编辑  收藏  举报