Jenkinsfile 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 = "aliyun_docker_registry"
  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 = "/home/app/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. // git分支tag
  36. def GIT_BRANCH_TAG = ""
  37. // docker镜像仓库认证命令
  38. def DOCKER_REGISTRY_AUTH_CMD = ""
  39. stage('初始化准备') {
  40. echo '****************************** 初始化准备 ******************************'
  41. // 网卡值设置 [注:正常liunx取eth0的内网ip,局域网取enp5s0的内网ip]
  42. ETH_VALUE = sh(script: "( [[ \"${JAVA_OPTS}\" = *'-Dspring.cloud.nacos.discovery.ip'* ]] && echo enp5s0 || echo eth0 )", returnStdout: true).trim()
  43. CURRENT_TIME = sh(script: "echo `date +\"%Y-%m-%d %H:%M:%S\"`", returnStdout: true).trim()
  44. script {
  45. GIT_BRANCH_TAG = GIT_BRANCH.replaceAll('origin/', '')
  46. }
  47. echo "当前工作空间:${JENKINS_WORKSPACE}"
  48. echo "使用分支:${GIT_BRANCH}"
  49. echo "使用分支tag:${GIT_BRANCH_TAG}"
  50. echo "使用网卡:${ETH_VALUE}"
  51. echo "是否跳过构建,直接运行docker容器(tips:适用于之前已经进行过构建打包的情景):${IS_SKIP_BUILD}"
  52. echo "当前时间:${CURRENT_TIME}"
  53. sh "mkdir -p ${PROJECT_SERVICE_LOG_HOME}"
  54. sh "mkdir -p ${PROJECT_JAR_HOME}"
  55. sh "mkdir -p ${PROJECT_DOCKER_HISTORY_JAR_HOME}"
  56. echo '****************************** 镜像仓库认证 ******************************'
  57. withCredentials([usernamePassword(credentialsId: "${DOCKER_REGISTRY_AUTH}", passwordVariable: 'password', usernameVariable: 'username')]) {
  58. // 登录镜像仓库
  59. DOCKER_REGISTRY_AUTH_CMD = "docker login -u ${username} -p ${password} ${DOCKER_REGISTRY_URL}"
  60. sh "${DOCKER_REGISTRY_AUTH_CMD}"
  61. }
  62. }
  63. stage('拉取代码') {
  64. echo '****************************** 拉取代码 ******************************'
  65. checkout([$class: 'GitSCM', branches: [[name: "${GIT_BRANCH}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${GIT_AUTH}", url: "${PROJECT_GIT_URL}"]]])
  66. sh "pwd"
  67. }
  68. stage('公共工程打包') {
  69. if ("${IS_SKIP_BUILD}" == "true") {
  70. echo '****************************** 公共工程打包 - 跳过 ******************************'
  71. } else {
  72. echo '****************************** 公共工程打包 ******************************'
  73. sh "mvn -f ${PROJECT_SERVICE_COMMON_NAME} clean install -Dmaven.test.skip=true"
  74. }
  75. }
  76. stage('微服务打包&删除旧容器和镜像&推送镜像&部署远程服务器') {
  77. echo '****************************** 微服务打包&删除旧容器和镜像&推送镜像&部署远程服务器 ******************************'
  78. for (int i = 0; i < PROJECT_SERVICE_NAME_SELECT.length; i++) {
  79. // 当前待处理项目
  80. def current_app_name = PROJECT_SERVICE_NAME_SELECT[i];
  81. def current_app_jar = "${current_app_name}.jar";
  82. // 父工程
  83. def current_app_parent = ""
  84. // 端口号
  85. def current_app_port = ""
  86. echo "当前操作项目: ${current_app_name}"
  87. script {
  88. switch (current_app_name) {
  89. case "admin":
  90. current_app_parent = ""
  91. current_app_port = "60667"
  92. break
  93. case "gateway":
  94. current_app_port = "8443"
  95. current_app_parent = ""
  96. break
  97. }
  98. }
  99. echo "当前操作项目: ${current_app_name} 端口:${current_app_port}"
  100. if ("${IS_SKIP_BUILD}" != "true") {
  101. sh "mvn -f ${current_app_parent}/${current_app_name} clean install -Dmaven.test.skip=true"
  102. sh "cp ${current_app_parent}/${current_app_name}/target/${current_app_jar} ${JENKINS_WORKSPACE}/docker"
  103. }
  104. // app镜像
  105. def app_docker_image_name = "weichi-crm-${current_app_name}"
  106. // def app_docker_image_tag = "${current_app_name}_${GIT_BRANCH_TAG}"
  107. def app_docker_image_tag = "latest"
  108. def app_docker_image = "${DOCKER_REGISTRY_URL}/${DOCKER_REGISTRY_NAMESPACE}/${app_docker_image_name}:${app_docker_image_tag}"
  109. echo "app镜像: ${app_docker_image}"
  110. if ("${IS_SKIP_BUILD}" != "true") {
  111. // 删除旧容器
  112. 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 {}"
  113. // 删除旧镜像
  114. sh "docker images | grep -E ${app_docker_image_name} | grep ${app_docker_image_tag}| awk '{print \$3}' | uniq | xargs -I {} docker rmi --force {}"
  115. // 构建新镜像 -f:指定Dockerfile文件路径
  116. 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"
  117. echo '****************************** 推送镜像 ******************************'
  118. // 推送镜像
  119. sh "docker push ${app_docker_image}"
  120. sh "echo 镜像推送成功:${app_docker_image}"
  121. // 操作完后移除docker目录下的jar文件,防止下次docker将多个jar一起构建导致镜像过大问题
  122. sh "mv -f ${JENKINS_WORKSPACE}/docker/*.jar ${PROJECT_DOCKER_HISTORY_JAR_HOME}/"
  123. }
  124. if ("${PUBLISH_SSH_SERVER_SELECT}" != "[]") {
  125. echo '****************************** 项目部署 ******************************'
  126. // 遍历所有服务器,分别部署
  127. for (int j = 0; j < PUBLISH_SSH_SERVER_SELECT.length; j++) {
  128. // 获取当前服务器名称
  129. def currentServerName = PUBLISH_SSH_SERVER_SELECT[j]
  130. echo '开始发布远程机器'
  131. sshPublisher(publishers: [sshPublisherDesc(
  132. // `Manage Jenkins` -> `Configure System` -> `Publish over SSH` -> `SSH Servers` 中的name值
  133. configName: "${currentServerName}",
  134. transfers: [sshTransfer(cleanRemote: false, excludes: '',
  135. // ===================== ↓↓↓↓↓↓ 执行命令 ↓↓↓↓↓↓ =====================
  136. execCommand: """
  137. // 传输部署脚本到远程服务器之后,执行该脚本运行程序
  138. cd ${PROJECT_SERVICE_LOG_HOME}
  139. // 传输脚本参数时请使用双引号包裹单个参数值,避免单个参数中存在空格等特殊符号被解析为多个参数!
  140. sh ssh-publish.sh \
  141. "${DOCKER_REGISTRY_AUTH_CMD}" \
  142. "${PROJECT_SERVICE_LOG_HOME}" \
  143. "${current_app_name}" \
  144. "${app_docker_image_name}" \
  145. "${app_docker_image_tag}" \
  146. "${app_docker_image}" \
  147. "${ETH_VALUE}" \
  148. "${current_app_port}" \
  149. "${JAVA_OPTS}"
  150. """,
  151. // 超时时间1000ms*60*2=2分钟
  152. execTimeout: 120000,
  153. flatten: false, makeEmptyDirs: false, noDefaultExcludes: false,
  154. patternSeparator: '[, ]+',
  155. // ===================== ↓↓↓↓↓↓ 传输文件 ↓↓↓↓↓↓ =====================
  156. // 传输文件
  157. sourceFiles: 'ssh-publish.sh',
  158. // 远程服务器目录
  159. remoteDirectory: "${PROJECT_SERVICE_LOG_HOME}",
  160. remoteDirectorySDF: false,
  161. // 传输时移除前缀路径
  162. removePrefix: '')],
  163. usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
  164. }
  165. } else {
  166. echo '****************************** 项目不部署 ******************************'
  167. }
  168. }
  169. }
  170. }