Jenkinsfile-uat-build-deploy.groovy 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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-latest
  9. * Before push: existing uat-latest is archived as uat-build-<BUILD_NUMBER> (same digest).
  10. * New image is pushed only as uat-latest. Prod promote: SOURCE_TAG=uat-latest.
  11. */
  12. /** HARBOR_PUSH_SCOPE: all-java-services | <repo>-only */
  13. def filterHarborPushScope(List allServices, String scope) {
  14. def s = (scope ?: 'all-java-services').trim()
  15. if (s == 'all-java-services') {
  16. return allServices
  17. }
  18. if (s.endsWith('-only')) {
  19. def repo = s.substring(0, s.length() - '-only'.length())
  20. def picked = allServices.findAll { it.repo == repo }
  21. if (picked.isEmpty()) {
  22. error("Unknown HARBOR_PUSH_SCOPE: ${scope}")
  23. }
  24. return picked
  25. }
  26. error("Unknown HARBOR_PUSH_SCOPE: ${scope}")
  27. }
  28. /** Delete oldest uat-build-* tags in Harbor, keep newest KEEP. Never deletes uat-latest or current build tag. */
  29. def pruneHarborUatTags(def script, String reg, String proj, List repoNames, int keepCount, String tagPrefix, String currentBuildTag, String latestTag) {
  30. if (repoNames == null || repoNames.isEmpty() || keepCount < 1) {
  31. return
  32. }
  33. def repos = repoNames.join(' ')
  34. script.sh """
  35. set -e
  36. REG='${reg}'
  37. PROJ='${proj}'
  38. KEEP=${keepCount}
  39. PREFIX='${tagPrefix}'
  40. CURRENT='${currentBuildTag}'
  41. LATEST='${latestTag}'
  42. if ! command -v jq >/dev/null 2>&1; then
  43. echo '>>> Harbor prune skipped: jq not installed on Jenkins agent'
  44. exit 0
  45. fi
  46. for repo in ${repos}; do
  47. enc_repo=\$(printf '%s' "\${repo}" | jq -sRr @uri)
  48. mapfile -t tags < <(curl -fsS -u "\${HARBOR_USER}:\${HARBOR_PASS}" \\
  49. "http://\${REG}/api/v2.0/projects/\${PROJ}/repositories/\${enc_repo}/artifacts?page_size=100" \\
  50. | jq -r '.[] | .tags[]? | .name' | grep "^\${PREFIX}" | sort -t- -k3 -n || true)
  51. count=\${#tags[@]}
  52. echo ">>> prune \${repo}: \${count} tag(s) matching \${PREFIX}*"
  53. if [ "\${count}" -le "\${KEEP}" ]; then
  54. continue
  55. fi
  56. del_count=\$((count - KEEP))
  57. i=0
  58. while [ "\${i}" -lt "\${del_count}" ]; do
  59. t="\${tags[\$i]}"
  60. if [ "\${t}" = "\${CURRENT}" ] || [ "\${t}" = "\${LATEST}" ]; then
  61. i=\$((i + 1))
  62. continue
  63. fi
  64. echo ">>> DELETE Harbor tag \${repo}:\${t}"
  65. if ! curl -fsS -X DELETE -u "\${HARBOR_USER}:\${HARBOR_PASS}" \\
  66. "http://\${REG}/api/v2.0/projects/\${PROJ}/repositories/\${enc_repo}/artifacts/\${t}/tags/\${t}"; then
  67. echo ">>> WARN: delete failed \${repo}:\${t} (check robot delete permission)"
  68. fi
  69. i=\$((i + 1))
  70. done
  71. done
  72. """
  73. }
  74. pipeline {
  75. agent any
  76. options {
  77. buildDiscarder(logRotator(numToKeepStr: '15'))
  78. timestamps()
  79. timeout(time: 90, unit: 'MINUTES')
  80. }
  81. parameters {
  82. string(
  83. name: 'GIT_BRANCH',
  84. defaultValue: 'uat-20260202',
  85. trim: true,
  86. description: 'Git branch, must match remote (e.g. uat-20260202)'
  87. )
  88. booleanParam(name: 'FORCE_UPDATE', defaultValue: true, description: 'mvn -U')
  89. booleanParam(name: 'ALLOW_SNAPSHOTS', defaultValue: true, description: 'allow SNAPSHOT deps')
  90. booleanParam(
  91. name: 'PUSH_TO_HARBOR',
  92. defaultValue: true,
  93. description: 'After Maven: docker build + push to Harbor (tags uat-latest and uat-build-<N>). Uncheck for jar-only UAT deploy.'
  94. )
  95. choice(
  96. name: 'HARBOR_PUSH_SCOPE',
  97. choices: [
  98. 'all-java-services',
  99. 'gateway-only',
  100. 'store-only',
  101. 'second-only',
  102. 'store-platform-only',
  103. 'lawyer-only',
  104. 'job-only',
  105. 'dining-only',
  106. ],
  107. description: 'Only when PUSH_TO_HARBOR=true; default=all seven; *-only=one service'
  108. )
  109. string(name: 'HARBOR_REGISTRY', defaultValue: '39.105.153.68', trim: true)
  110. string(name: 'HARBOR_PROJECT', defaultValue: 'alien_cloud', trim: true)
  111. booleanParam(
  112. name: 'HARBOR_PRUNE_OLD_TAGS',
  113. defaultValue: true,
  114. description: 'After push: delete old uat-build-* tags in Harbor, keep last N per repo (never deletes uat-latest)'
  115. )
  116. string(name: 'HARBOR_KEEP_TAG_COUNT', defaultValue: '10', trim: true,
  117. description: 'How many uat-build-* tags to keep per repository')
  118. }
  119. environment {
  120. MAVEN_HOME = tool '3.6.3'
  121. PATH = "${MAVEN_HOME}/bin:${env.PATH}"
  122. GIT_URL = 'http://8.152.195.41:3000/alien/alien_cloud'
  123. GIT_CREDENTIALS = 'zhanghaomimapingzheng'
  124. HARBOR_CREDENTIALS = 'harbor-robot-alien'
  125. UAT_HARBOR_LATEST_TAG = 'uat-latest'
  126. UAT_HARBOR_BUILD_TAG = "uat-build-${env.BUILD_NUMBER}"
  127. DOCKERFILE_JAVA = 'docs/jenkins/produ/docker/Dockerfile.java-service'
  128. }
  129. stages {
  130. stage('Checkout') {
  131. steps {
  132. script {
  133. def branch = (params.GIT_BRANCH ?: 'uat-20260202').trim()
  134. if (!branch) {
  135. error('GIT_BRANCH is required')
  136. }
  137. env.GIT_BRANCH = branch
  138. echo ">>> Checkout branch: ${env.GIT_BRANCH}"
  139. git branch: "${env.GIT_BRANCH}",
  140. credentialsId: "${env.GIT_CREDENTIALS}",
  141. url: "${env.GIT_URL}"
  142. sh """
  143. set -e
  144. git fetch origin
  145. git reset --hard origin/${env.GIT_BRANCH}
  146. git log -1 --oneline
  147. """
  148. }
  149. }
  150. }
  151. stage('Prepare Maven Settings') {
  152. steps {
  153. script {
  154. writeFile file: 'settings.xml', text: """<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  155. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  156. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  157. <profiles>
  158. <profile>
  159. <id>repo-mix</id>
  160. <repositories>
  161. <repository>
  162. <id>central</id>
  163. <name>Maven Central</name>
  164. <url>https://repo.maven.apache.org/maven2</url>
  165. <releases><enabled>true</enabled><updatePolicy>always</updatePolicy></releases>
  166. <snapshots><enabled>false</enabled></snapshots>
  167. </repository>
  168. <repository>
  169. <id>spring-milestones</id>
  170. <name>Spring Milestones</name>
  171. <url>https://repo.spring.io/milestone</url>
  172. <releases><enabled>true</enabled><updatePolicy>always</updatePolicy></releases>
  173. <snapshots><enabled>false</enabled></snapshots>
  174. </repository>
  175. <repository>
  176. <id>spring-snapshots</id>
  177. <name>Spring Snapshots</name>
  178. <url>https://repo.spring.io/snapshot</url>
  179. <releases><enabled>false</enabled></releases>
  180. <snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots>
  181. </repository>
  182. </repositories>
  183. <pluginRepositories>
  184. <pluginRepository>
  185. <id>central</id>
  186. <url>https://repo.maven.apache.org/maven2</url>
  187. <releases><enabled>true</enabled></releases>
  188. <snapshots><enabled>false</enabled></snapshots>
  189. </pluginRepository>
  190. <pluginRepository>
  191. <id>spring-milestones</id>
  192. <url>https://repo.spring.io/milestone</url>
  193. <releases><enabled>true</enabled></releases>
  194. <snapshots><enabled>false</enabled></snapshots>
  195. </pluginRepository>
  196. </pluginRepositories>
  197. </profile>
  198. </profiles>
  199. <activeProfiles>
  200. <activeProfile>repo-mix</activeProfile>
  201. </activeProfiles>
  202. </settings>
  203. """
  204. }
  205. }
  206. }
  207. stage('Maven Build') {
  208. steps {
  209. script {
  210. def updateFlag = params.FORCE_UPDATE ? '-U' : ''
  211. retry(2) {
  212. sh """
  213. set -e
  214. mvn -version
  215. unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY ALL_PROXY all_proxy no_proxy NO_PROXY || true
  216. export MAVEN_OPTS="-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true"
  217. rm -rf /root/.m2/repository/org/springframework/cloud/spring-cloud-dependencies/Hoxton.SR1 || true
  218. rm -rf /root/.m2/repository/org/springframework/boot/spring-boot-dependencies/2.3.2.RELEASE || true
  219. rm -rf ${WORKSPACE}/.m2/repository/org/springframework/cloud/spring-cloud-dependencies/Hoxton.SR1 || true
  220. rm -rf ${WORKSPACE}/.m2/repository/org/springframework/boot/spring-boot-dependencies/2.3.2.RELEASE || true
  221. mvn clean package -DskipTests -s settings.xml ${updateFlag} -e -Dmaven.repo.local=${WORKSPACE}/.m2/repository
  222. """
  223. }
  224. }
  225. }
  226. }
  227. stage('Push images to Harbor') {
  228. when {
  229. expression { return params.PUSH_TO_HARBOR == true }
  230. }
  231. steps {
  232. script {
  233. def reg = params.HARBOR_REGISTRY.trim()
  234. def proj = params.HARBOR_PROJECT.trim()
  235. def latestTag = env.UAT_HARBOR_LATEST_TAG
  236. def buildTag = env.UAT_HARBOR_BUILD_TAG
  237. def baseImage = "${reg}/${proj}/base/openjdk8-ffmpeg:v1"
  238. def dockerfile = env.DOCKERFILE_JAVA
  239. def allHarborServices = [
  240. [module: 'alien-gateway', repo: 'gateway', port: '8000', withLib: false],
  241. [module: 'alien-store', repo: 'store', port: '50014', withLib: true],
  242. [module: 'alien-second', repo: 'second', port: '50015', withLib: false],
  243. [module: 'alien-store-platform', repo: 'store-platform', port: '50016', withLib: false],
  244. [module: 'alien-lawyer', repo: 'lawyer', port: '50017', withLib: true],
  245. [module: 'alien-job', repo: 'job', port: '50108', withLib: false],
  246. [module: 'alien-dining', repo: 'dining', port: '50019', withLib: false],
  247. ]
  248. def harborServices = filterHarborPushScope(allHarborServices, params.HARBOR_PUSH_SCOPE)
  249. echo ">>> HARBOR_PUSH_SCOPE=${params.HARBOR_PUSH_SCOPE} repos=${harborServices*.repo.join(',')}"
  250. withCredentials([usernamePassword(
  251. credentialsId: env.HARBOR_CREDENTIALS,
  252. usernameVariable: 'HARBOR_USER',
  253. passwordVariable: 'HARBOR_PASS',
  254. )]) {
  255. sh """
  256. set -e
  257. echo "\${HARBOR_PASS}" | docker login ${reg} -u "\${HARBOR_USER}" --password-stdin
  258. echo ">>> docker disk before Harbor push:"
  259. df -h /var/lib/docker 2>/dev/null || df -h / || true
  260. docker system prune -f --filter until=48h 2>/dev/null || true
  261. """
  262. harborServices.each { svc ->
  263. def jarName = "${svc.module}-1.0.0.jar"
  264. def imageLatest = "${reg}/${proj}/${svc.repo}:${latestTag}"
  265. def imageBuild = "${reg}/${proj}/${svc.repo}:${buildTag}"
  266. def withLibFlag = svc.withLib ? 'true' : 'false'
  267. sh """
  268. set -e
  269. test -f ${WORKSPACE}/${svc.module}/target/${jarName}
  270. cd ${WORKSPACE}/${svc.module}
  271. rm -rf .jenkins_docker_ctx && mkdir -p .jenkins_docker_ctx/lib
  272. cp -f target/${jarName} .jenkins_docker_ctx/${jarName}
  273. if [ "${withLibFlag}" = "true" ] && [ -d target/lib ]; then
  274. cp -rf target/lib/. .jenkins_docker_ctx/lib/
  275. else
  276. touch .jenkins_docker_ctx/lib/.keep
  277. fi
  278. cd .jenkins_docker_ctx
  279. if docker pull ${imageLatest} 2>/dev/null; then
  280. echo ">>> archive previous ${latestTag} -> ${buildTag}"
  281. docker tag ${imageLatest} ${imageBuild}
  282. docker push ${imageBuild}
  283. fi
  284. docker build -f ${WORKSPACE}/${dockerfile} \\
  285. --build-arg BASE_IMAGE=${baseImage} \\
  286. --build-arg JAR_FILE=${jarName} \\
  287. --build-arg SERVER_PORT=${svc.port} \\
  288. --build-arg WITH_LIB=${svc.withLib} \\
  289. -t ${imageLatest} .
  290. docker push ${imageLatest}
  291. echo ">>> pushed ${imageLatest} (archived prior latest as ${buildTag} if any)"
  292. docker rmi ${imageLatest} 2>/dev/null || true
  293. cd ${WORKSPACE}/${svc.module}
  294. rm -rf .jenkins_docker_ctx
  295. """
  296. }
  297. if (params.HARBOR_PRUNE_OLD_TAGS == true) {
  298. def keepN = (params.HARBOR_KEEP_TAG_COUNT ?: '10').trim() as int
  299. pruneHarborUatTags(
  300. this, reg, proj, harborServices*.repo,
  301. keepN, 'uat-build-', buildTag, latestTag,
  302. )
  303. }
  304. }
  305. echo ">>> Harbor latest: ${env.UAT_HARBOR_LATEST_TAG}; archived tag this run: ${env.UAT_HARBOR_BUILD_TAG}"
  306. echo ">>> Prod promote: SOURCE_TAG=${env.UAT_HARBOR_LATEST_TAG}"
  307. }
  308. }
  309. }
  310. stage('Deploy Services') {
  311. steps {
  312. script {
  313. def services = [
  314. 'alien-gateway:gateway-uat',
  315. 'alien-job:job-uat',
  316. 'alien-lawyer:lawyer-uat',
  317. 'alien-second:second-uat',
  318. 'alien-store:store-uat',
  319. 'alien-dining:dining-uat',
  320. 'alien-store-platform:store-platform-uat',
  321. ]
  322. for (item in services) {
  323. def parts = item.split(':')
  324. def moduleName = parts[0]
  325. def dirName = parts[1]
  326. def sourceJar = "${env.WORKSPACE}/${moduleName}/target/${moduleName}-1.0.0.jar"
  327. def sourceLib = "${env.WORKSPACE}/${moduleName}/target/lib"
  328. def targetDir = "/app_deploy_uat/${dirName}"
  329. sh """
  330. set -e
  331. echo ">>> Deploy module: ${moduleName}"
  332. if [ -f "${sourceJar}" ]; then
  333. mkdir -p "${targetDir}"
  334. if [ -d "${sourceLib}" ]; then
  335. rm -rf "${targetDir}/lib"
  336. cp -rf "${sourceLib}" "${targetDir}"
  337. fi
  338. cp -f "${sourceJar}" "${targetDir}/"
  339. if docker ps -a --format '{{.Names}}' | grep -wq "${dirName}"; then
  340. docker restart "${dirName}"
  341. echo ">>> [${dirName}] restarted"
  342. else
  343. echo ">>> [${dirName}] container missing, jar copied only"
  344. fi
  345. else
  346. echo ">>> [${dirName}] jar missing, skip"
  347. fi
  348. """
  349. }
  350. }
  351. }
  352. }
  353. }
  354. post {
  355. always {
  356. sh 'rm -f settings.xml || true'
  357. script {
  358. if (!params.PUSH_TO_HARBOR) {
  359. echo '>>> Harbor push SKIPPED: PUSH_TO_HARBOR is false. On "Build with Parameters" check PUSH_TO_HARBOR.'
  360. }
  361. }
  362. }
  363. success {
  364. script {
  365. if (params.PUSH_TO_HARBOR) {
  366. echo ">>> Harbor latest: ${env.UAT_HARBOR_LATEST_TAG}"
  367. echo ">>> Prod promote: SOURCE_TAG=${env.UAT_HARBOR_LATEST_TAG}"
  368. }
  369. }
  370. }
  371. }
  372. }