Jenkinsfile 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. properties([
  2. parameters([
  3. [$class: 'ChoiceParameter',
  4. choiceType: 'PT_SINGLE_SELECT',
  5. description: '选择要部署 API or UI',
  6. name: 'modulePrefix',
  7. randomName: 'choice-parameter-56313144329613978',
  8. script: [
  9. $class: 'GroovyScript',
  10. fallbackScript: [
  11. classpath: [],
  12. sandbox: true,
  13. script:
  14. 'return[\'Could not get Env\']'
  15. ],
  16. script: [
  17. classpath: [],
  18. sandbox: true,
  19. script:
  20. 'return["java","ui"]'
  21. ]
  22. ]
  23. ],
  24. [$class: 'CascadeChoiceParameter',
  25. choiceType: 'PT_SINGLE_SELECT',
  26. description: '选择要部署的项目',
  27. name: 'module',
  28. randomName: 'choice-parameter-56313144562178619',
  29. referencedParameters: 'modulePrefix',
  30. script: [
  31. $class: 'GroovyScript',
  32. fallbackScript: [
  33. classpath: [],
  34. sandbox: true,
  35. script:
  36. 'return[\'Could not get Environment from Env Param\']'
  37. ],
  38. script: [
  39. classpath: [],
  40. sandbox: true,
  41. script:
  42. ''' if (modulePrefix.equals("java")){
  43. return["storlead-api"]
  44. }
  45. else if(modulePrefix.equals("ui")){
  46. return["smarttrade-platform","su-user-center"]
  47. }
  48. '''
  49. ]
  50. ]
  51. ]
  52. ])
  53. ])
  54. pipeline {
  55. options {
  56. timestamps()
  57. }
  58. environment {
  59. GROUPID = readMavenPom().getGroupId()//com.storlead
  60. ARTIFACTID = readMavenPom().getArtifactId()//sp-project
  61. VERSION = readMavenPom().getVersion()//1.0
  62. }
  63. // 参数
  64. parameters {
  65. choice(
  66. name: 'profile',
  67. choices: ['test', 'prod','uat'],
  68. description: '选择要部署的配置文件'
  69. )
  70. }
  71. agent any
  72. stages {
  73. stage('处理参数') {
  74. steps {
  75. script {
  76. echo "处理参数"
  77. script {
  78. //服务器配置
  79. MODULE_PREFIX = "${params.modulePrefix}"
  80. SERVER_SSH_PORT = 53023
  81. SERVER_HOST = "node1.storlead.com"
  82. SWARM_INIT_REPLICAS_NUM = 1
  83. //服务配置
  84. API_PORT = 9200
  85. API_REMOTE_DEBUG_PORT = 9201
  86. API_NAMESPACE_RESTFUL = "${params.module}"
  87. //DOCKER配置
  88. DOCKER_REGISTRY = "reg-aliyun"
  89. DOCKER_HOST = "registry.cn-shenzhen.aliyuncs.com"
  90. //Docker私服登陆url https://registry-vpc.cn-shenzhen.aliyuncs.com
  91. DOCKER_LOGIN_REGISTRY = "https://${DOCKER_HOST}"
  92. BUILD_PREFIX = "storlead-huawei"
  93. //镜像完整名称 registry-vpc.cn-shenzhen.aliyuncs.com/storlead/sp-project-management-prod:1.0
  94. DOCKER_IMG_NAME = "${DOCKER_HOST}/${BUILD_PREFIX}/${params.module}-${params.profile}:${VERSION}"
  95. //镜像名 storlead/sp-project-management-prod:1.0
  96. IMG_NAME = "${BUILD_PREFIX}/${params.module}-${params.profile}:${VERSION}"
  97. PUSH_DOCK_IMG = true
  98. SERVER_HOST_NAME = "test1"
  99. echo "为指定 module 设定对应 服务器及端口"
  100. echo "处理 ${params.module} 项目"
  101. if (params.modulePrefix == "java") {
  102. echo "---- storelad-centre-api"
  103. script {
  104. if (params.profile == "test") {
  105. SWARM_INIT_REPLICAS_NUM = 1
  106. SERVER_HOST = "test1.storlead.com"
  107. API_PORT = 10030
  108. API_REMOTE_DEBUG_PORT = 10031
  109. SERVER_HOST_NAME = "test1"
  110. } else if (params.profile == "prod") {
  111. SERVER_HOST = "110.41.82.21"
  112. API_PORT = 10030
  113. API_REMOTE_DEBUG_PORT = 10031
  114. SERVER_HOST_NAME = "node1"
  115. }
  116. }
  117. } else if (params.modulePrefix == "ui") {
  118. script {
  119. if (params.profile == "test") {
  120. SERVER_HOST = "test1.storlead.com"
  121. } else if (params.profile == "prod") {
  122. SERVER_HOST = "110.41.82.21"
  123. }
  124. }
  125. }
  126. echo "处理MODULE_NAMESPACE_RESTFUL"
  127. //把 module 名 从 sp-xxx 变成 xxx 也就是 项目 的 namespace
  128. // if (API_NAMESPACE_RESTFUL == "storlead-centre-api") {
  129. // API_NAMESPACE_RESTFUL = "api"
  130. // }
  131. echo "处理镜像推送"
  132. }
  133. }
  134. }
  135. }
  136. stage('Node 编译') {
  137. when {
  138. environment name: 'modulePrefix', value: 'ui'
  139. }
  140. agent {
  141. docker {
  142. // image "registry-vpc.cn-shenzhen.aliyuncs.com/storlead/node:19-alpine"
  143. image "node:20-alpine"
  144. args '-v /app:/app/:z -v /etc/localtime:/etc/localtime:ro -u root'
  145. reuseNode true
  146. }
  147. }
  148. steps {
  149. withCredentials([
  150. usernamePassword(credentialsId: 'storleadHW', usernameVariable: 'usernameHW', passwordVariable: 'passwordHW'),
  151. usernamePassword(credentialsId: 'storlead', usernameVariable: 'username', passwordVariable: 'password')
  152. ]) {
  153. script {
  154. echo "发布 UI 代码"
  155. def remote = [:]
  156. remote.allowAnyHosts = true
  157. remote.name = "${SERVER_HOST}"
  158. remote.host = "${SERVER_HOST}"
  159. remote.port = 53023
  160. remote.user = username
  161. remote.password = password
  162. if (params.profile == "prod") {
  163. remote.port = 22
  164. remote.user = "root"
  165. remote.user = usernameHW
  166. remote.password = passwordHW
  167. }
  168. // if (params.module == 'sp-user-mobile-center') {
  169. sh """cd ${params.modulePrefix}/${params.module} && npm install && npm run ${params.profile}"""
  170. // } else {
  171. // sh """cd ${params.modulePrefix}/${params.module} && npm install && npm run ${params.profile}"""
  172. // }
  173. // sh """cd ${params.modulePrefix}/${params.module} && npm run ${params.profile}"""
  174. sshCommand remote: remote, command: "mkdir -p /app/sp/ui/app-centre/${params.profile}/${params.module}"
  175. sshCommand remote: remote, command: "rm -rf /app/sp/ui/app-centre/${params.profile}/${params.module}/dist/*"
  176. echo "/app/sp/ui/app-centre/${params.profile}/${params.module}/*"
  177. sshPut remote: remote, from: "${params.modulePrefix}/${params.module}/dist/", into: "/app/sp/ui/app-centre/${params.profile}/${params.module}/"
  178. echo "${params.modulePrefix}/${params.module}/dist/,/app/sp/ui/app-centre/${params.profile}/${params.module}/"
  179. sshCommand remote: remote, command: "cp -rf /app/sp/ui/app-centre/${params.profile}/${params.module}/dist/* /app/sp/ui/app-centre/${params.profile}/${params.module}/"
  180. }
  181. }
  182. }
  183. }
  184. stage('Maven 编译') {
  185. when {
  186. environment name: 'modulePrefix', value: 'java'
  187. }
  188. agent {
  189. docker {
  190. // storlead-trade/sales 等模块 target 为 17,需 JDK 17 编译
  191. image 'maven:3-amazoncorretto-17'
  192. //让docker 使用 host 宿主机的 m2仓库 使用root用户来运行 以让指定的~/.m2/config/setting.xml 阿里加速下载maven 依赖生效
  193. args '-v /app:/app:z -v $HOME/.m2:/root/.m2:z -u root'
  194. reuseNode true
  195. }
  196. }
  197. steps {
  198. echo "编译 API 代码"
  199. // 根 reactor 模块路径为 java/storlead-api,-pl 用 :artifactId 按构件名选择
  200. sh "mvn clean package -U -am -pl :${params.module} -P${params.profile} -Dmaven.test.skip=true"
  201. }
  202. }
  203. // 项目打包到镜像并推送到镜像仓库
  204. stage('构建Docker镜像') {
  205. when {
  206. environment name: 'modulePrefix', value: 'java'
  207. }
  208. steps {
  209. echo "构建Dcoker镜像-------------------"
  210. script {
  211. docker.withRegistry(DOCKER_LOGIN_REGISTRY, DOCKER_REGISTRY) {
  212. // 与模块 target 17 一致;若私服无此 tag,需先推送或改用 amazoncorretto:17
  213. BASE_IMAGE = 'registry.cn-shenzhen.aliyuncs.com/storlead/openjdk:17'
  214. sh "docker pull ${BASE_IMAGE}"
  215. sh """
  216. echo '
  217. FROM ${BASE_IMAGE}
  218. COPY java/${params.module}/target/${params.module}-${VERSION}.jar app.jar
  219. EXPOSE ${API_PORT}
  220. EXPOSE ${API_REMOTE_DEBUG_PORT}
  221. HEALTHCHECK --interval=1m --timeout=10s CMD curl -f http://localhost:${API_PORT}/${API_NAMESPACE_RESTFUL}/actuator
  222. ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom", "-Djava.awt.headless=true","-Dspring.profiles.active=${params.profile}", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:${API_REMOTE_DEBUG_PORT}","-jar","/app.jar"]' > Dockerfile
  223. docker build -t ${DOCKER_IMG_NAME} .
  224. """
  225. sh "docker push ${DOCKER_IMG_NAME}"
  226. }
  227. }
  228. }
  229. }
  230. stage('启动服务') {
  231. when {
  232. environment name: 'modulePrefix', value: 'java'
  233. }
  234. steps {
  235. withCredentials([
  236. usernamePassword(credentialsId: 'storleadHW', usernameVariable: 'usernameHW', passwordVariable: 'passwordHW'),
  237. usernamePassword(credentialsId: 'storlead', usernameVariable: 'username', passwordVariable: 'password'),
  238. usernamePassword(credentialsId: 'reg-aliyun', usernameVariable: 'aliUsername', passwordVariable: 'aliPassword')
  239. ]) {
  240. script {
  241. echo "启动服务"
  242. echo "${aliPassword}"
  243. echo "docker login -u ${aliUsername} -p ${aliPassword} ${DOCKER_LOGIN_REGISTRY}"
  244. def remote = [:]
  245. remote.name = "${SERVER_HOST}"
  246. remote.host = "${SERVER_HOST}"
  247. remote.port = 53023
  248. remote.user = username
  249. remote.password = password
  250. remote.allowAnyHosts = true
  251. //因为使用 docker swarm 发布 只能在 manage node 执行部署
  252. if (params.profile == "prod") {
  253. remote.port = 22
  254. remote.user = "root"
  255. remote.user = usernameHW
  256. remote.password = passwordHW
  257. }
  258. if (PUSH_DOCK_IMG) {
  259. //docker 认证
  260. sshCommand remote: remote, command: "docker login -u ${aliUsername} -p ${aliPassword} ${DOCKER_LOGIN_REGISTRY}"
  261. //如果选择推送才使用远程的版本 如果不选择推送到远程则直接运行本机刚打包的镜像版本即可
  262. sshCommand remote: remote, command: "docker pull ${DOCKER_IMG_NAME}"
  263. }
  264. sh """
  265. echo '
  266. version: "3.7"
  267. services:
  268. ${params.module}-${params.profile}:
  269. image: ${DOCKER_IMG_NAME}
  270. deploy:
  271. replicas: ${SWARM_INIT_REPLICAS_NUM}
  272. placement:
  273. constraints:
  274. - 'node.hostname == ${SERVER_HOST_NAME}'
  275. resources:
  276. limits:
  277. memory: 2G
  278. reservations:
  279. memory: 512M
  280. update_config:
  281. parallelism: 1
  282. delay: 1m
  283. restart_policy:
  284. condition: on-failure
  285. # 最多连续重试3次如果仍然失败则放弃重试
  286. max_attempts: 3
  287. healthcheck:
  288. test: ["CMD", "curl", "-f", "http://localhost:${API_PORT}/${API_NAMESPACE_RESTFUL}/actuator"]
  289. interval: 1m
  290. timeout: 5s
  291. retries: 3
  292. start_period: 30s
  293. environment:
  294. - "TZ=Asia/Shanghai"
  295. - "SPRING_PROFILES_ACTIVE=${params.profile}"
  296. - "JAVA_OPTS=-Xms512m -Xmx1024m"
  297. volumes:
  298. - /app:/app
  299. - /mnt/vdb/storlead:/mnt/vdb/storlead
  300. ports:
  301. - "${API_PORT}:${API_PORT}"
  302. - "${API_REMOTE_DEBUG_PORT}:${API_REMOTE_DEBUG_PORT}"
  303. networks:
  304. - vonedao_net
  305. extra_hosts:
  306. - "www.storlead.com:172.18.194.161"
  307. - "node1:110.41.82.21"
  308. - "node2:110.41.174.46"
  309. - "node3:121.37.226.174"
  310. - "test1.storlead.com:172.18.194.168"
  311. - "test2.storlead.com:172.18.194.163"
  312. networks:
  313. vonedao_net:
  314. external: true
  315. ' > ${params.module}-${params.profile}-stack.yml
  316. """
  317. // if (params.profile =="dev") {
  318. // sh "docker stack deploy -c ${ARTIFACTID}-${params.profile}-stack.yml ${ARTIFACTID}-${params.profile}"
  319. // sh "docker ps -f name=${ARTIFACTID} -q | xargs --no-run-if-empty docker container stop"
  320. // sh "docker container ls -a -fname=${ARTIFACTID} -q | xargs -r docker container rm"
  321. // sh "docker run -d --restart=always -p ${API_PORT}:${API_PORT} -p ${API_REMOTE_DEBUG_PORT}:${API_REMOTE_DEBUG_PORT} -v /app:/app --name ${ARTIFACTID} ${DOCKER_IMG_NAME}"
  322. // } else {
  323. // 全部都以swarm 模式启动
  324. sshPut remote: remote, from: "${params.module}-${params.profile}-stack.yml", into: "/docker/sp/${params.module}-${params.profile}-stack.yml"
  325. sshCommand remote: remote, command: "docker stack deploy --resolve-image=always --with-registry-auth -c /docker/sp/${params.module}-${params.profile}-stack.yml ${params.module}-${params.profile}"
  326. // }
  327. }
  328. }
  329. }
  330. }
  331. }
  332. }