Jenkinsfile 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. properties([
  2. parameters([
  3. [$class: 'ChoiceParameter',
  4. choiceType: 'PT_SINGLE_SELECT',
  5. description: '选择要部署 API or UI',
  6. name: 'modulePrefix',
  7. randomName: 'choice-parameter-5631314439613978',
  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-5631314456178619',
  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-centre-api"]
  44. }
  45. else if(modulePrefix.equals("ui")){
  46. return["sp-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 = 10060
  108. API_REMOTE_DEBUG_PORT = 10061
  109. SERVER_HOST_NAME = "test1"
  110. } else if (params.profile == "prod") {
  111. SERVER_HOST = "110.41.82.21"
  112. API_PORT = 10060
  113. API_REMOTE_DEBUG_PORT = 10061
  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 build:${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/sales/${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. sh "mvn clean package -U -am -pl ${params.module} -P${params.profile} -Dmaven.test.skip=true"
  199. }
  200. }
  201. // 项目打包到镜像并推送到镜像仓库
  202. stage('构建Docker镜像') {
  203. when {
  204. environment name: 'modulePrefix', value: 'java'
  205. }
  206. steps {
  207. echo "构建Dcoker镜像-------------------"
  208. script {
  209. docker.withRegistry(DOCKER_LOGIN_REGISTRY, DOCKER_REGISTRY) {
  210. BASE_IMAGE = 'registry.cn-shenzhen.aliyuncs.com/storlead/openjdk:11'
  211. sh "docker pull ${BASE_IMAGE}"
  212. sh """
  213. echo '
  214. FROM ${BASE_IMAGE}
  215. COPY ${params.module}/target/${params.module}.jar app.jar
  216. EXPOSE ${API_PORT}
  217. EXPOSE ${API_REMOTE_DEBUG_PORT}
  218. HEALTHCHECK --interval=1m --timeout=10s CMD curl -f http://localhost:${API_PORT}/${API_NAMESPACE_RESTFUL}/actuator
  219. 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
  220. docker build -t ${DOCKER_IMG_NAME} .
  221. """
  222. sh "docker push ${DOCKER_IMG_NAME}"
  223. }
  224. }
  225. }
  226. }
  227. stage('启动服务') {
  228. when {
  229. environment name: 'modulePrefix', value: 'java'
  230. }
  231. steps {
  232. withCredentials([
  233. usernamePassword(credentialsId: 'storleadHW', usernameVariable: 'usernameHW', passwordVariable: 'passwordHW'),
  234. usernamePassword(credentialsId: 'storlead', usernameVariable: 'username', passwordVariable: 'password'),
  235. usernamePassword(credentialsId: 'reg-aliyun', usernameVariable: 'aliUsername', passwordVariable: 'aliPassword')
  236. ]) {
  237. script {
  238. echo "启动服务"
  239. echo "${aliPassword}"
  240. echo "docker login -u ${aliUsername} -p ${aliPassword} ${DOCKER_LOGIN_REGISTRY}"
  241. def remote = [:]
  242. remote.name = "${SERVER_HOST}"
  243. remote.host = "${SERVER_HOST}"
  244. remote.port = 53023
  245. remote.user = username
  246. remote.password = password
  247. remote.allowAnyHosts = true
  248. //因为使用 docker swarm 发布 只能在 manage node 执行部署
  249. if (params.profile == "prod") {
  250. remote.port = 22
  251. remote.user = "root"
  252. remote.user = usernameHW
  253. remote.password = passwordHW
  254. }
  255. if (PUSH_DOCK_IMG) {
  256. //docker 认证
  257. sshCommand remote: remote, command: "docker login -u ${aliUsername} -p ${aliPassword} ${DOCKER_LOGIN_REGISTRY}"
  258. //如果选择推送才使用远程的版本 如果不选择推送到远程则直接运行本机刚打包的镜像版本即可
  259. sshCommand remote: remote, command: "docker pull ${DOCKER_IMG_NAME}"
  260. }
  261. sh """
  262. echo '
  263. version: "3.7"
  264. services:
  265. ${params.module}-${params.profile}:
  266. image: ${DOCKER_IMG_NAME}
  267. deploy:
  268. replicas: ${SWARM_INIT_REPLICAS_NUM}
  269. placement:
  270. constraints:
  271. - 'node.hostname == ${SERVER_HOST_NAME}'
  272. update_config:
  273. parallelism: 1
  274. delay: 1m
  275. restart_policy:
  276. condition: on-failure
  277. # 最多连续重试3次如果仍然失败则放弃重试
  278. max_attempts: 3
  279. healthcheck:
  280. test: ["CMD", "curl", "-f", "http://localhost:${API_PORT}/${API_NAMESPACE_RESTFUL}/actuator"]
  281. interval: 1m
  282. timeout: 5s
  283. retries: 3
  284. start_period: 30s
  285. environment:
  286. - "TZ=Asia/Shanghai"
  287. - "SPRING_PROFILES_ACTIVE=${params.profile}"
  288. volumes:
  289. - /app:/app
  290. - /mnt/vdb/storlead:/mnt/vdb/storlead
  291. ports:
  292. - "${API_PORT}:${API_PORT}"
  293. - "${API_REMOTE_DEBUG_PORT}:${API_REMOTE_DEBUG_PORT}"
  294. networks:
  295. - vonedao_net
  296. extra_hosts:
  297. - "www.storlead.com:172.18.194.161"
  298. - "node1:110.41.82.21"
  299. - "node2:110.41.174.46"
  300. - "node3:121.37.226.174"
  301. - "test1.storlead.com:172.18.194.168"
  302. - "test2.storlead.com:172.18.194.163"
  303. networks:
  304. vonedao_net:
  305. external: true
  306. ' > ${params.module}-${params.profile}-stack.yml
  307. """
  308. // if (params.profile =="dev") {
  309. // sh "docker stack deploy -c ${ARTIFACTID}-${params.profile}-stack.yml ${ARTIFACTID}-${params.profile}"
  310. // sh "docker ps -f name=${ARTIFACTID} -q | xargs --no-run-if-empty docker container stop"
  311. // sh "docker container ls -a -fname=${ARTIFACTID} -q | xargs -r docker container rm"
  312. // 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}"
  313. // } else {
  314. // 全部都以swarm 模式启动
  315. sshPut remote: remote, from: "${params.module}-${params.profile}-stack.yml", into: "/docker/sp/${params.module}-${params.profile}-stack.yml"
  316. 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}"
  317. // }
  318. }
  319. }
  320. }
  321. }
  322. }
  323. }