Jenkinsfile-uat-build-deploy.groovy 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * UAT: Checkout -> Maven -> (optional) push images to Harbor -> deploy jar + docker restart.
  3. *
  4. * Jenkins Job: Pipeline script from SCM
  5. * Script Path: docs/jenkins/Jenkinsfile-uat-build-deploy.groovy
  6. *
  7. * Harbor (153.68): when PUSH_TO_HARBOR=true, push e.g.
  8. * 39.105.153.68/alien_cloud/gateway:uat-build-<BUILD_NUMBER>
  9. * Production promote jobs use SOURCE_TAG=uat-build-<same number>.
  10. */
  11. pipeline {
  12. agent any
  13. options {
  14. buildDiscarder(logRotator(numToKeepStr: '15'))
  15. timestamps()
  16. timeout(time: 90, unit: 'MINUTES')
  17. }
  18. parameters {
  19. string(
  20. name: 'GIT_BRANCH',
  21. defaultValue: 'uat-20260202',
  22. trim: true,
  23. description: 'Git branch, must match remote (e.g. uat-20260202)'
  24. )
  25. booleanParam(name: 'FORCE_UPDATE', defaultValue: true, description: 'mvn -U')
  26. booleanParam(name: 'ALLOW_SNAPSHOTS', defaultValue: true, description: 'allow SNAPSHOT deps')
  27. booleanParam(
  28. name: 'PUSH_TO_HARBOR',
  29. defaultValue: false,
  30. description: 'After Maven: docker build + push to 39.105.153.68/alien_cloud (tag uat-build-<BUILD_NUMBER>)'
  31. )
  32. choice(
  33. name: 'HARBOR_PUSH_SCOPE',
  34. choices: ['gateway-only', 'all-java-services'],
  35. description: 'Only used when PUSH_TO_HARBOR=true'
  36. )
  37. string(name: 'HARBOR_REGISTRY', defaultValue: '39.105.153.68', trim: true)
  38. string(name: 'HARBOR_PROJECT', defaultValue: 'alien_cloud', trim: true)
  39. }
  40. environment {
  41. MAVEN_HOME = tool '3.6.3'
  42. PATH = "${MAVEN_HOME}/bin:${env.PATH}"
  43. GIT_URL = 'http://8.152.195.41:3000/alien/alien_cloud'
  44. GIT_CREDENTIALS = 'zhanghaomimapingzheng'
  45. HARBOR_CREDENTIALS = 'harbor-robot-alien'
  46. UAT_HARBOR_IMAGE_TAG = "uat-build-${env.BUILD_NUMBER}"
  47. DOCKERFILE_JAVA = 'docs/jenkins/produ/docker/Dockerfile.java-service'
  48. }
  49. stages {
  50. stage('Checkout') {
  51. steps {
  52. script {
  53. def branch = (params.GIT_BRANCH ?: 'uat-20260202').trim()
  54. if (!branch) {
  55. error('GIT_BRANCH is required')
  56. }
  57. env.GIT_BRANCH = branch
  58. echo ">>> Checkout branch: ${env.GIT_BRANCH}"
  59. git branch: "${env.GIT_BRANCH}",
  60. credentialsId: "${env.GIT_CREDENTIALS}",
  61. url: "${env.GIT_URL}"
  62. sh """
  63. set -e
  64. git fetch origin
  65. git reset --hard origin/${env.GIT_BRANCH}
  66. git log -1 --oneline
  67. """
  68. }
  69. }
  70. }
  71. stage('Prepare Maven Settings') {
  72. steps {
  73. script {
  74. writeFile file: 'settings.xml', text: """<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  75. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  76. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  77. <profiles>
  78. <profile>
  79. <id>repo-mix</id>
  80. <repositories>
  81. <repository>
  82. <id>central</id>
  83. <name>Maven Central</name>
  84. <url>https://repo.maven.apache.org/maven2</url>
  85. <releases><enabled>true</enabled><updatePolicy>always</updatePolicy></releases>
  86. <snapshots><enabled>false</enabled></snapshots>
  87. </repository>
  88. <repository>
  89. <id>spring-milestones</id>
  90. <name>Spring Milestones</name>
  91. <url>https://repo.spring.io/milestone</url>
  92. <releases><enabled>true</enabled><updatePolicy>always</updatePolicy></releases>
  93. <snapshots><enabled>false</enabled></snapshots>
  94. </repository>
  95. <repository>
  96. <id>spring-snapshots</id>
  97. <name>Spring Snapshots</name>
  98. <url>https://repo.spring.io/snapshot</url>
  99. <releases><enabled>false</enabled></releases>
  100. <snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots>
  101. </repository>
  102. </repositories>
  103. <pluginRepositories>
  104. <pluginRepository>
  105. <id>central</id>
  106. <url>https://repo.maven.apache.org/maven2</url>
  107. <releases><enabled>true</enabled></releases>
  108. <snapshots><enabled>false</enabled></snapshots>
  109. </pluginRepository>
  110. <pluginRepository>
  111. <id>spring-milestones</id>
  112. <url>https://repo.spring.io/milestone</url>
  113. <releases><enabled>true</enabled></releases>
  114. <snapshots><enabled>false</enabled></snapshots>
  115. </pluginRepository>
  116. </pluginRepositories>
  117. </profile>
  118. </profiles>
  119. <activeProfiles>
  120. <activeProfile>repo-mix</activeProfile>
  121. </activeProfiles>
  122. </settings>
  123. """
  124. }
  125. }
  126. }
  127. stage('Maven Build') {
  128. steps {
  129. script {
  130. def updateFlag = params.FORCE_UPDATE ? '-U' : ''
  131. retry(2) {
  132. sh """
  133. set -e
  134. mvn -version
  135. unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY ALL_PROXY all_proxy no_proxy NO_PROXY || true
  136. export MAVEN_OPTS="-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true"
  137. rm -rf /root/.m2/repository/org/springframework/cloud/spring-cloud-dependencies/Hoxton.SR1 || true
  138. rm -rf /root/.m2/repository/org/springframework/boot/spring-boot-dependencies/2.3.2.RELEASE || true
  139. rm -rf ${WORKSPACE}/.m2/repository/org/springframework/cloud/spring-cloud-dependencies/Hoxton.SR1 || true
  140. rm -rf ${WORKSPACE}/.m2/repository/org/springframework/boot/spring-boot-dependencies/2.3.2.RELEASE || true
  141. mvn clean package -DskipTests -s settings.xml ${updateFlag} -e -Dmaven.repo.local=${WORKSPACE}/.m2/repository
  142. """
  143. }
  144. }
  145. }
  146. }
  147. stage('Push images to Harbor') {
  148. when { expression { return params.PUSH_TO_HARBOR == true } }
  149. steps {
  150. script {
  151. def reg = params.HARBOR_REGISTRY.trim()
  152. def proj = params.HARBOR_PROJECT.trim()
  153. def tag = env.UAT_HARBOR_IMAGE_TAG
  154. def baseImage = "${reg}/${proj}/base/openjdk8-ffmpeg:v1"
  155. def dockerfile = env.DOCKERFILE_JAVA
  156. def harborServices = [
  157. [module: 'alien-gateway', repo: 'gateway', port: '8000', withLib: false],
  158. [module: 'alien-store', repo: 'store', port: '50014', withLib: true],
  159. [module: 'alien-second', repo: 'second', port: '50015', withLib: false],
  160. [module: 'alien-store-platform', repo: 'store-platform', port: '50016', withLib: false],
  161. [module: 'alien-lawyer', repo: 'lawyer', port: '50017', withLib: true],
  162. [module: 'alien-job', repo: 'job', port: '50108', withLib: false],
  163. [module: 'alien-dining', repo: 'dining', port: '50019', withLib: false],
  164. ]
  165. if (params.HARBOR_PUSH_SCOPE == 'gateway-only') {
  166. harborServices = harborServices.findAll { it.repo == 'gateway' }
  167. }
  168. withCredentials([usernamePassword(
  169. credentialsId: env.HARBOR_CREDENTIALS,
  170. usernameVariable: 'HARBOR_USER',
  171. passwordVariable: 'HARBOR_PASS',
  172. )]) {
  173. sh """
  174. set -e
  175. echo "\${HARBOR_PASS}" | docker login ${reg} -u "\${HARBOR_USER}" --password-stdin
  176. """
  177. harborServices.each { svc ->
  178. def jarName = "${svc.module}-1.0.0.jar"
  179. def imageRef = "${reg}/${proj}/${svc.repo}:${tag}"
  180. sh """
  181. set -e
  182. test -f ${WORKSPACE}/${svc.module}/target/${jarName}
  183. cd ${WORKSPACE}/${svc.module}
  184. rm -rf .jenkins_docker_ctx && mkdir -p .jenkins_docker_ctx/lib
  185. cp -f target/${jarName} .jenkins_docker_ctx/${jarName}
  186. if [ -d target/lib ]; then
  187. cp -rf target/lib/. .jenkins_docker_ctx/lib/
  188. else
  189. touch .jenkins_docker_ctx/lib/.keep
  190. fi
  191. cd .jenkins_docker_ctx
  192. docker build -f ${WORKSPACE}/${dockerfile} \\
  193. --build-arg BASE_IMAGE=${baseImage} \\
  194. --build-arg JAR_FILE=${jarName} \\
  195. --build-arg SERVER_PORT=${svc.port} \\
  196. --build-arg WITH_LIB=${svc.withLib} \\
  197. -t ${imageRef} .
  198. docker push ${imageRef}
  199. echo ">>> pushed ${imageRef}"
  200. """
  201. }
  202. }
  203. echo ">>> Harbor tag for prod promote: SOURCE_TAG=${tag}"
  204. }
  205. }
  206. }
  207. stage('Deploy Services') {
  208. steps {
  209. script {
  210. def services = [
  211. 'alien-gateway:gateway-uat',
  212. 'alien-job:job-uat',
  213. 'alien-lawyer:lawyer-uat',
  214. 'alien-second:second-uat',
  215. 'alien-store:store-uat',
  216. 'alien-dining:dining-uat',
  217. 'alien-store-platform:store-platform-uat',
  218. ]
  219. for (item in services) {
  220. def parts = item.split(':')
  221. def moduleName = parts[0]
  222. def dirName = parts[1]
  223. def sourceJar = "${env.WORKSPACE}/${moduleName}/target/${moduleName}-1.0.0.jar"
  224. def sourceLib = "${env.WORKSPACE}/${moduleName}/target/lib"
  225. def targetDir = "/app_deploy_uat/${dirName}"
  226. sh """
  227. set -e
  228. echo ">>> Deploy module: ${moduleName}"
  229. if [ -f "${sourceJar}" ]; then
  230. mkdir -p "${targetDir}"
  231. if [ -d "${sourceLib}" ]; then
  232. rm -rf "${targetDir}/lib"
  233. cp -rf "${sourceLib}" "${targetDir}"
  234. fi
  235. cp -f "${sourceJar}" "${targetDir}/"
  236. if docker ps -a --format '{{.Names}}' | grep -wq "${dirName}"; then
  237. docker restart "${dirName}"
  238. echo ">>> [${dirName}] restarted"
  239. else
  240. echo ">>> [${dirName}] container missing, jar copied only"
  241. fi
  242. else
  243. echo ">>> [${dirName}] jar missing, skip"
  244. fi
  245. """
  246. }
  247. }
  248. }
  249. }
  250. }
  251. post {
  252. always {
  253. sh 'rm -f settings.xml || true'
  254. }
  255. success {
  256. script {
  257. if (params.PUSH_TO_HARBOR) {
  258. echo ">>> Harbor images tagged: ${env.UAT_HARBOR_IMAGE_TAG}"
  259. echo ">>> Prod promote: SOURCE_TAG=${env.UAT_HARBOR_IMAGE_TAG}"
  260. }
  261. }
  262. }
  263. }
  264. }