Jenkinsfile 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // ==================== ↓↓↓↓↓↓ docker ↓↓↓↓↓↓ ====================
  2. // 镜像仓库地址
  3. def DOCKER_REGISTRY_URL = "registry.cn-shanghai.aliyuncs.com"
  4. // 镜像仓库命名空间名称
  5. def DOCKER_REGISTRY_NAMESPACE = "advich-group"
  6. // 镜像拉取凭证 => jenkins全局凭证中配置
  7. def DOCKER_REGISTRY_AUTH = "6ec7fc8d-859f-43ba-8ebc-337bedec055b"
  8. // ==================== ↓↓↓↓↓↓ git ↓↓↓↓↓↓ ====================
  9. // git凭证 => jenkins全局凭证中配置
  10. def GIT_AUTH = "git_auth"
  11. // ==================== ↓↓↓↓↓↓ project ↓↓↓↓↓↓ ====================
  12. // 项目-git地址
  13. def PROJECT_GIT_URL = "https://git.dev.advichcloud.com/wangfan/weichi-crm-server.git"
  14. // 项目-公共模块名称
  15. def PROJECT_SERVICE_COMMON_NAME = "core"
  16. // 项目-基础目录
  17. def PROJECT_BASE_HOME = "/var/jenkins_home/workspace/weichi-crm-server"
  18. // 项目-服务日志目录
  19. def PROJECT_SERVICE_LOG_HOME = "${PROJECT_BASE_HOME}/logs"
  20. // 项目-jar文件目录
  21. def PROJECT_JAR_HOME = "${PROJECT_BASE_HOME}/target"
  22. // 项目-docker部署的历史jar目录
  23. def PROJECT_DOCKER_HISTORY_JAR_HOME = "${PROJECT_BASE_HOME}/docker-history-jar"
  24. node {
  25. // jenkins工作空间
  26. def JENKINS_WORKSPACE = "${WORKSPACE}"
  27. // 取哪一块网卡值
  28. def ETH_VALUE = ""
  29. // 当前时间
  30. def CURRENT_TIME = ""
  31. // 获取选择的项目服务名称
  32. def PROJECT_SERVICE_NAME_SELECT = "${PROJECT_SERVICE_NAME}".split(",")
  33. // 获取选择的服务器名称
  34. def PUBLISH_SSH_SERVER_SELECT = "${PUBLISH_SSH_SERVER}".split(",")
  35. // maven 和 jdk 工具路径
  36. def mavenHome = tool 'Maven-3.8.5'
  37. def jdkHome = tool 'JDK8'
  38. // git分支tag
  39. def GIT_BRANCH_TAG = ""
  40. // docker镜像仓库认证命令
  41. def DOCKER_REGISTRY_AUTH_CMD = ""
  42. stage('初始化准备') {
  43. echo '****************************** 初始化准备 ******************************'
  44. echo "JDK路径: ${jdkHome}"
  45. echo "Maven路径: ${mavenHome}"
  46. // 使用JDK工具
  47. env.JAVA_HOME = jdkHome
  48. // 网卡值设置 [注:正常liunx取eth0的内网ip,局域网取enp5s0的内网ip]
  49. ETH_VALUE = sh(script: "( [[ \"${JAVA_OPTS}\" = *'-Dspring.cloud.nacos.discovery.ip'* ]] && echo enp5s0 || echo eth0 )", returnStdout: true).trim()
  50. CURRENT_TIME = sh(script: "echo `date +\"%Y-%m-%d %H:%M:%S\"`", returnStdout: true).trim()
  51. script {
  52. GIT_BRANCH_TAG = GIT_BRANCH.replaceAll('origin/', '')
  53. }
  54. echo "当前工作空间:${JENKINS_WORKSPACE}"
  55. echo "使用分支:${GIT_BRANCH}"
  56. echo "使用分支tag:${GIT_BRANCH_TAG}"
  57. echo "使用网卡:${ETH_VALUE}"
  58. echo "是否跳过构建,直接运行docker容器(tips:适用于之前已经进行过构建打包的情景):${IS_SKIP_BUILD}"
  59. echo "当前时间:${CURRENT_TIME}"
  60. sh "mkdir -p ${PROJECT_SERVICE_LOG_HOME}"
  61. sh "mkdir -p ${PROJECT_JAR_HOME}"
  62. sh "mkdir -p ${PROJECT_DOCKER_HISTORY_JAR_HOME}"
  63. echo '****************************** 镜像仓库认证 ******************************'
  64. withCredentials([usernamePassword(credentialsId: "${DOCKER_REGISTRY_AUTH}", passwordVariable: 'password', usernameVariable: 'username')]) {
  65. // 登录镜像仓库
  66. DOCKER_REGISTRY_AUTH_CMD = "docker login -u ${username} -p ${password} ${DOCKER_REGISTRY_URL}"
  67. sh "${DOCKER_REGISTRY_AUTH_CMD}"
  68. }
  69. }
  70. stage('拉取代码') {
  71. echo '****************************** 拉取代码 ******************************'
  72. checkout([$class: 'GitSCM', branches: [[name: "${GIT_BRANCH}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${GIT_AUTH}", url: "${PROJECT_GIT_URL}"]]])
  73. sh "pwd"
  74. }
  75. stage('公共工程打包') {
  76. if ("${IS_SKIP_BUILD}" == "true") {
  77. echo '****************************** 公共工程打包 - 跳过 ******************************'
  78. } else {
  79. echo '****************************** 公共工程打包 ******************************'
  80. sh "${mavenHome}/bin/mvn -f ${PROJECT_SERVICE_COMMON_NAME} clean install -Dmaven.test.skip=true"
  81. }
  82. }
  83. stage('微服务打包&删除旧容器和镜像&推送镜像&部署远程服务器') {
  84. echo '****************************** 微服务打包&删除旧容器和镜像&推送镜像&部署远程服务器 ******************************'
  85. for (int i = 0; i < PROJECT_SERVICE_NAME_SELECT.length; i++) {
  86. // 当前待处理项目
  87. def current_app_name = PROJECT_SERVICE_NAME_SELECT[i];
  88. def current_app_jar = "wk_${current_app_name}-0.0.1-SNAPSHOT.jar";
  89. // 端口号
  90. def current_app_port = ""
  91. echo "当前操作项目: ${current_app_name}"
  92. script {
  93. switch (current_app_name) {
  94. case "admin":
  95. current_app_port = "60667"
  96. break
  97. case "gateway":
  98. current_app_port = "8443"
  99. break
  100. }
  101. }
  102. echo "当前操作项目: ${current_app_name} 端口:${current_app_port}"
  103. if ("${IS_SKIP_BUILD}" != "true") {
  104. sh "${mavenHome}/bin/mvn -f ${current_app_name} clean install -Dmaven.test.skip=true"
  105. sh "cp ${current_app_name}/target/${current_app_jar} ${JENKINS_WORKSPACE}/docker"
  106. }
  107. // app镜像
  108. def app_docker_image_name = "weichi-crm-${current_app_name}"
  109. // def app_docker_image_tag = "${current_app_name}_${GIT_BRANCH_TAG}"
  110. def app_docker_image_tag = "latest"
  111. def app_docker_image = "${DOCKER_REGISTRY_URL}/${DOCKER_REGISTRY_NAMESPACE}/${app_docker_image_name}:${app_docker_image_tag}"
  112. echo "app镜像: ${app_docker_image}"
  113. if ("${IS_SKIP_BUILD}" != "true") {
  114. // 删除旧容器
  115. sh "docker ps -a | grep ${app_docker_image_name} | grep ${app_docker_image_tag} | awk '{print \$1}' | xargs -I docker stop {} | xargs -I docker rm {}"
  116. // 删除旧镜像
  117. sh "docker images | grep -E ${app_docker_image_name} | grep ${app_docker_image_tag}| awk '{print \$3}' | uniq | xargs -I {} docker rmi --force {}"
  118. // 构建新镜像 -f:指定Dockerfile文件路径
  119. sh "cd ${JENKINS_WORKSPACE}/docker && docker build -f Dockerfile --build-arg JAVA_OPTS=\"${JAVA_OPTS}\" --build-arg APP_NAME=${current_app_name} --build-arg APP_PORT=${current_app_port} -t ${app_docker_image} . --no-cache"
  120. echo '****************************** 推送镜像 ******************************'
  121. // 推送镜像
  122. sh "docker push ${app_docker_image}"
  123. sh "echo 镜像推送成功:${app_docker_image}"
  124. // 操作完后移除docker目录下的jar文件,防止下次docker将多个jar一起构建导致镜像过大问题
  125. sh "mv -f ${JENKINS_WORKSPACE}/docker/*.jar ${PROJECT_DOCKER_HISTORY_JAR_HOME}/"
  126. }
  127. echo '****************************** 项目部署 ******************************'
  128. if ("${PUBLISH_SSH_SERVER_SELECT}" != "[]") {
  129. // 遍历所有服务器,分别部署
  130. for (int j = 0; j < publish_ssh_server_select.length; j++) {
  131. // 获取当前服务器名称
  132. def currentServerName = publish_ssh_server_select[j]
  133. echo '开始发布远程机器'
  134. sshPublisher(publishers: [sshPublisherDesc(configName: "${currentServerName}",
  135. transfers: [sshTransfer(cleanRemote: false, excludes: '',
  136. execCommand: """
  137. // 远程服务器下依次执行如下命令
  138. // 删除旧容器
  139. docker ps -a | grep ${current_app_name} | grep ${app_docker_image_tag} | awk '{print \$1}' | xargs -i docker stop {} | xargs -i docker rm {}
  140. // 删除旧镜像
  141. docker images | grep -E ${current_app_name} | grep ${app_docker_image_tag}| awk '{print \$3}' | uniq | xargs -I {} docker rmi --force {}
  142. // 拉取新镜像
  143. docker pull ${app_docker_image}
  144. // 创建日志文件
  145. mkdir -p ${PROJECT_SERVICE_LOG_HOME}
  146. cd ${PROJECT_SERVICE_LOG_HOME}
  147. touch ${current_app_name}.log
  148. // 运行容器
  149. echo "*** ${CURRENT_TIME} docker run -d -p ${current_app_port}:${current_app_port} -e JAVA_OPTS="${JAVA_OPTS} -Dspring.cloud.nacos.discovery.ip=`ifconfig ${ETH_VALUE} | grep inet | grep -v inet6 | awk '{print \$2}'`" -v ${PROJECT_SERVICE_LOG_HOME}/${current_app_name}.log:/home/${current_app_name}.log --name ${current_app_name} ${app_docker_image}" >> ${PROJECT_SERVICE_LOG_HOME}/run.log
  150. docker run -d -p ${current_app_port}:${current_app_port} -e JAVA_OPTS="${JAVA_OPTS} -Dspring.cloud.nacos.discovery.ip=`ifconfig ${ETH_VALUE} | grep inet | grep -v inet6 | awk '{print \$2}'`" -v ${PROJECT_SERVICE_LOG_HOME}/${current_app_name}.log:/home/${current_app_name}.log --name ${current_app_name} ${app_docker_image}
  151. """,
  152. execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false,
  153. patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '',
  154. sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
  155. }
  156. } else {
  157. echo '****************************** 项目不部署 ******************************'
  158. }
  159. }
  160. }
  161. }