Jenkinsfile 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. image 'maven:3-amazoncorretto-11'
  191. //让docker 使用 host 宿主机的 m2仓库 使用root用户来运行 以让指定的~/.m2/config/setting.xml 阿里加速下载maven 依赖生效
  192. args '-v /app:/app:z -v $HOME/.m2:/root/.m2:z -u root'
  193. reuseNode true
  194. }
  195. }
  196. steps {
  197. echo "编译 API 代码"
  198. // 根 reactor 模块路径为 java/storlead-api,-pl 用 :artifactId 按构件名选择
  199. sh "mvn clean package -U -am -pl :${params.module} -P${params.profile} -Dmaven.test.skip=true"
  200. }
  201. }
  202. // 项目打包到镜像并推送到镜像仓库
  203. stage('构建Docker镜像') {
  204. when {
  205. environment name: 'modulePrefix', value: 'java'
  206. }
  207. steps {
  208. echo "构建Dcoker镜像-------------------"
  209. script {
  210. docker.withRegistry(DOCKER_LOGIN_REGISTRY, DOCKER_REGISTRY) {
  211. BASE_IMAGE = 'registry.cn-shenzhen.aliyuncs.com/storlead/openjdk:11'
  212. sh "docker pull ${BASE_IMAGE}"
  213. sh """
  214. echo '
  215. FROM ${BASE_IMAGE}
  216. COPY java/${params.module}/target/${params.module}-${VERSION}.jar app.jar
  217. EXPOSE ${API_PORT}
  218. EXPOSE ${API_REMOTE_DEBUG_PORT}
  219. HEALTHCHECK --interval=1m --timeout=10s CMD curl -f http://localhost:${API_PORT}/${API_NAMESPACE_RESTFUL}/actuator
  220. 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
  221. docker build -t ${DOCKER_IMG_NAME} .
  222. """
  223. sh "docker push ${DOCKER_IMG_NAME}"
  224. }
  225. }
  226. }
  227. }
  228. stage('启动服务') {
  229. when {
  230. environment name: 'modulePrefix', value: 'java'
  231. }
  232. steps {
  233. withCredentials([
  234. usernamePassword(credentialsId: 'storleadHW', usernameVariable: 'usernameHW', passwordVariable: 'passwordHW'),
  235. usernamePassword(credentialsId: 'storlead', usernameVariable: 'username', passwordVariable: 'password'),
  236. usernamePassword(credentialsId: 'reg-aliyun', usernameVariable: 'aliUsername', passwordVariable: 'aliPassword')
  237. ]) {
  238. script {
  239. echo "启动服务"
  240. echo "${aliPassword}"
  241. echo "docker login -u ${aliUsername} -p ${aliPassword} ${DOCKER_LOGIN_REGISTRY}"
  242. def remote = [:]
  243. remote.name = "${SERVER_HOST}"
  244. remote.host = "${SERVER_HOST}"
  245. remote.port = 53023
  246. remote.user = username
  247. remote.password = password
  248. remote.allowAnyHosts = true
  249. //因为使用 docker swarm 发布 只能在 manage node 执行部署
  250. if (params.profile == "prod") {
  251. remote.port = 22
  252. remote.user = "root"
  253. remote.user = usernameHW
  254. remote.password = passwordHW
  255. }
  256. if (PUSH_DOCK_IMG) {
  257. //docker 认证
  258. sshCommand remote: remote, command: "docker login -u ${aliUsername} -p ${aliPassword} ${DOCKER_LOGIN_REGISTRY}"
  259. //如果选择推送才使用远程的版本 如果不选择推送到远程则直接运行本机刚打包的镜像版本即可
  260. sshCommand remote: remote, command: "docker pull ${DOCKER_IMG_NAME}"
  261. }
  262. sh """
  263. echo '
  264. version: "3.7"
  265. services:
  266. ${params.module}-${params.profile}:
  267. image: ${DOCKER_IMG_NAME}
  268. deploy:
  269. replicas: ${SWARM_INIT_REPLICAS_NUM}
  270. placement:
  271. constraints:
  272. - 'node.hostname == ${SERVER_HOST_NAME}'
  273. resources:
  274. limits:
  275. memory: 2G
  276. reservations:
  277. memory: 512M
  278. update_config:
  279. parallelism: 1
  280. delay: 1m
  281. restart_policy:
  282. condition: on-failure
  283. # 最多连续重试3次如果仍然失败则放弃重试
  284. max_attempts: 3
  285. healthcheck:
  286. test: ["CMD", "curl", "-f", "http://localhost:${API_PORT}/${API_NAMESPACE_RESTFUL}/actuator"]
  287. interval: 1m
  288. timeout: 5s
  289. retries: 3
  290. start_period: 30s
  291. environment:
  292. - "TZ=Asia/Shanghai"
  293. - "SPRING_PROFILES_ACTIVE=${params.profile}"
  294. - "JAVA_OPTS=-Xms512m -Xmx1024m"
  295. volumes:
  296. - /app:/app
  297. - /mnt/vdb/storlead:/mnt/vdb/storlead
  298. ports:
  299. - "${API_PORT}:${API_PORT}"
  300. - "${API_REMOTE_DEBUG_PORT}:${API_REMOTE_DEBUG_PORT}"
  301. networks:
  302. - vonedao_net
  303. extra_hosts:
  304. - "www.storlead.com:172.18.194.161"
  305. - "node1:110.41.82.21"
  306. - "node2:110.41.174.46"
  307. - "node3:121.37.226.174"
  308. - "test1.storlead.com:172.18.194.168"
  309. - "test2.storlead.com:172.18.194.163"
  310. networks:
  311. vonedao_net:
  312. external: true
  313. ' > ${params.module}-${params.profile}-stack.yml
  314. """
  315. // if (params.profile =="dev") {
  316. // sh "docker stack deploy -c ${ARTIFACTID}-${params.profile}-stack.yml ${ARTIFACTID}-${params.profile}"
  317. // sh "docker ps -f name=${ARTIFACTID} -q | xargs --no-run-if-empty docker container stop"
  318. // sh "docker container ls -a -fname=${ARTIFACTID} -q | xargs -r docker container rm"
  319. // 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}"
  320. // } else {
  321. // 全部都以swarm 模式启动
  322. sshPut remote: remote, from: "${params.module}-${params.profile}-stack.yml", into: "/docker/sp/${params.module}-${params.profile}-stack.yml"
  323. 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}"
  324. // }
  325. }
  326. }
  327. }
  328. }
  329. }
  330. }