Jenkinsfile 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. @Library('jenkins-share-library@master')
  2. def qyWechatRobot = new org.devops.notification.QYWechatRobot()
  3. pipeline {
  4. agent {
  5. node {
  6. label 'master'
  7. }
  8. }
  9. // tools { node '' }
  10. // parameters {}
  11. environment {
  12. _fullDisplayName = "幼享乐-服务端"
  13. _qyWechatRobotToken = "a47cb467-8e94-4877-b275-832cc1f0d9af"
  14. _remote = "root@47.97.230.53"
  15. _remoteB = "root@8.134.14.62"
  16. _buildTime = new Date().format('yyyyMMddHHmmss') // 生成当前时间戳
  17. _git_tag = sh(returnStdout: true, script: 'git describe --tags --always').trim()
  18. _git_branch = sh(returnStdout: true, script: 'echo ${GIT_BRANCH#*/}').trim()
  19. _git_commit_email = sh(returnStdout: true, script: 'git --no-pager show -s --format=\'%ae\'').trim()
  20. _git_commit_message = sh (script: 'git log -1 --pretty=%B ${GIT_COMMIT}', returnStdout: true).trim()
  21. _productFileName = "yxl-backend_${_git_branch}_${_buildTime}"
  22. _productPath = '/app/yxl/yxl-backend' // 产物启动目录
  23. _productBackupPath = '/app/backup/yxl/yxl-backend' // 产物备份目录
  24. }
  25. triggers {
  26. // cron('0 0 * * *') // cron计划任务定期执行构建
  27. // pollSCM('H/1 * * * *') // pollSCM与cron定义类似,但由Jenkins定期检测源码变化
  28. // upstream( // upstream接受逗号分割的工作字符串和阀值。当字符串中的任何作业以最小阀值结束时,流水线被重新触发。
  29. // upstreamProjects: 'job1, job2',
  30. // threshold: hudson.model.Result.SUCCESS
  31. // )
  32. gitlab(
  33. triggerOnPush: true,
  34. triggerOnMergeRequest: true,
  35. triggerOnNoteRequest: true,
  36. branchFilterType: 'All',
  37. secretToken: 'asdfghjkl'
  38. )
  39. }
  40. options {
  41. timestamps() // 添加日志打印时间
  42. // checkoutToSubdirectory('sub') // Jenkins默认拉取源码至工作空间的根目录中,此选项可以指定检出到工作空间的子目录中
  43. disableConcurrentBuilds() // 同一个pipeline,Jenkins默认是可以同时执行多次的,此选项为了禁止pipeline同时执行
  44. // skipDefaultCheckout() // 删除隐式checkuot scm语句
  45. retry(1) // 当发生失败时进行重试(包括第1次失败)
  46. buildDiscarder(
  47. logRotator(
  48. daysToKeepStr: '30', // 构建记录将保存的天数
  49. numToKeepStr: '10', // 最多此数目的构建记录将被保存
  50. artifactDaysToKeepStr: '30', // 比此早的发布包将被删除,但构建的日志、操作历史、报告等将被保留
  51. artifactNumToKeepStr: '10' // 最多此数目的构建将保留他们的发布包
  52. )
  53. )
  54. timeout( // 流水线超时设置
  55. time: 15, // 整型
  56. unit: 'MINUTES', // 时间单位,默认分钟。支持NANOSECONDS,MICROSECONDS,MILLISECONDS,SECONDS,MINUTES(默认),HOURS,DAYS
  57. activity: true // 布尔类型,true时 只有当日志没活动才算真正的超时
  58. )
  59. gitLabConnection('gitlab') // 连接gitlab服务(需要在Jenkins中设置Jenkins -> Configure System)
  60. }
  61. post {
  62. always { // 不论当前完成状态是什么,都执行
  63. // pom.xml中增加plugin -> maven-pmd-plugin
  64. // Jenkins PMD插件[PMD](https://plugins.jenkins.io/pmd)
  65. // pmd(canRunOnFailed: true, pattern: '**/target/pmd.xml')
  66. cleanWs() // 清理工作空间插件[Workspace Cleanup Plugin](https://plugins.jenkins.io/ws-cleanup)
  67. }
  68. success { // 成功后执行
  69. updateGitlabCommitStatus name: 'build', state: 'success'
  70. script {
  71. qyWechatRobot.endBuild('构建成功😀', _qyWechatRobotToken)
  72. }
  73. }
  74. failure { // 失败后执行
  75. updateGitlabCommitStatus name: 'build', state: 'failed'
  76. script {
  77. qyWechatRobot.endBuild('构建失败🤬', _qyWechatRobotToken)
  78. }
  79. }
  80. aborted { // 取消后执行
  81. script {
  82. qyWechatRobot.endBuild('构建取消🤯', _qyWechatRobotToken)
  83. }
  84. }
  85. }
  86. stages {
  87. stage('Start Notification') {
  88. steps {
  89. script {
  90. qyWechatRobot.startBuild(_qyWechatRobotToken)
  91. }
  92. }
  93. }
  94. stage('Env & Param') {
  95. parallel {
  96. stage('Env') {
  97. steps {
  98. sh 'printenv'
  99. echo "系统当前用户 [${env.USER}]"
  100. echo "JENKINS_URL [${env.JENKINS_URL}]"
  101. echo "WORKSPACE [${env.WORKSPACE}]"
  102. }
  103. }
  104. stage('Job') {
  105. steps {
  106. echo "-------------------- --------------------"
  107. echo "JOB_NAME [${env.JOB_NAME}]"
  108. echo "BUILD_NUMBER [${env.BUILD_NUMBER}]"
  109. echo "BUILD_URL [${env.BUILD_URL}]"
  110. echo "BUILD_NUMBER [${env.BUILD_NUMBER}]"
  111. echo "-------------------- --------------------"
  112. echo "BRANCH_NAME [${env.BRANCH_NAME}]"
  113. echo "GIT_BRANCH [${env.GIT_BRANCH}]"
  114. echo "GIT_COMMIT [${env.GIT_COMMIT}]"
  115. echo "GIT_COMMIT [${env.commit}]"
  116. echo "_git_tag [${_git_tag}]"
  117. echo "_git_branch [${_git_branch}]"
  118. echo "-------------------- --------------------"
  119. echo "产物名称 [${_productFileName}]"
  120. sh "java -version"
  121. sh "mvn -v"
  122. }
  123. }
  124. }
  125. }
  126. stage('clone code') {
  127. steps {
  128. timeout(time: 1, unit: 'MINUTES') {
  129. script {
  130. println('clone code')
  131. }
  132. }
  133. }
  134. }
  135. stage('Build nx-framework-parent') {
  136. steps {
  137. dir("./nx-framework-parent") {
  138. sh 'mvn clean install -Dautoconfig.skip=true -Dmaven.test.skip=true'
  139. }
  140. }
  141. }
  142. stage('Build nx-service-parent') {
  143. steps {
  144. dir("./nx-service-parent") {
  145. sh 'mvn clean install -Dautoconfig.skip=true -Dmaven.test.skip=true'
  146. }
  147. }
  148. }
  149. stage('Build services') {
  150. failFast true // 当其中一个进程失败时,强制所以当parallel阶段都被终止
  151. parallel {
  152. stage('service') {
  153. steps {
  154. dir("./yxl") {
  155. sh 'mvn clean package -Dautoconfig.skip=true -Dmaven.test.skip=true'
  156. }
  157. }
  158. }
  159. }
  160. }
  161. stage('Static check') {
  162. parallel {
  163. stage('service') {
  164. steps {
  165. echo "service"
  166. // sh 'mvn pmd:pmd'
  167. // sh "./mvnw clean test jacoco:report"
  168. // withSonarQubeEnv( installationName: 'sonar_server') {
  169. // sh './mvnw sonar:sonar'
  170. // }
  171. // timeout(time: 1, unit: 'HOURS') {
  172. // waitForQualityGate abortPipeline: true
  173. // }
  174. }
  175. }
  176. }
  177. }
  178. stage('Product') {
  179. parallel {
  180. stage('service') {
  181. steps {
  182. dir("./yxl/target") {
  183. sh "cp ${env.WORKSPACE}/yxl/target/*.jar ${env.WORKSPACE}/${_productFileName}.jar"
  184. sh "cp -rv ${env.WORKSPACE}/yxl/target/lib ${env.WORKSPACE}/lib"
  185. sh "cp -rv ${env.WORKSPACE}/yxl/target/resources ${env.WORKSPACE}/resources"
  186. }
  187. }
  188. }
  189. }
  190. }
  191. stage('archiveArtifacts') {
  192. steps {
  193. archiveArtifacts(
  194. artifacts: '*.jar', // 字符串类型,需要归档的文件路径,使用的是Ant风格路径表达式。
  195. // excludes: '', // 字符串类型,需要排除的文件路径,使用的也是Ant风格路径表达式。
  196. caseSensitive: true, // 布尔类型,对路径大小写是否敏感。
  197. fingerprint: true, // 布尔类型,是否对归档的文件进行签名。
  198. onlyIfSuccessful: true // 布尔类型,只在构建成功时进行归档。
  199. )
  200. }
  201. }
  202. stage('Pre-Release') {
  203. steps {
  204. script {
  205. echo "当前分支为:[${_git_branch}], 准备发布ing..."
  206. // if ("${_git_branch}" == 'master') {
  207. // echo "当前分支为:[${_git_branch}], 准备发布ing..."
  208. // } else {
  209. // error("当前分支为:[${_git_branch}], 终止当前发布操作...")
  210. // }
  211. }
  212. }
  213. }
  214. stage('开发环境(47.97.230.53)') {
  215. steps {
  216. script {
  217. echo "当前分支为:[${_git_branch}], 准备发布ing..."
  218. sh """
  219. ssh ${_remote} "
  220. source /etc/profile
  221. mkdir -pv ${_productBackupPath}
  222. mkdir -pv ${_productPath}
  223. rm -rf ${_productPath}/resources
  224. "
  225. """
  226. // rm -rf ${_productPath}/lib
  227. sh "scp -rv ${env.WORKSPACE}/${_productFileName}.jar ${_remote}:${_productBackupPath}/"
  228. // sh "scp -rv ${env.WORKSPACE}/lib ${_remote}:${_productPath}/lib"
  229. sh "scp -rv ${env.WORKSPACE}/resources ${_remote}:${_productPath}/resources"
  230. sh """
  231. ssh ${_remote} "
  232. source /etc/profile
  233. rm -rf ${_productPath}/*.jar
  234. cp ${_productBackupPath}/${_productFileName}.jar ${_productPath}/${_productFileName}.jar
  235. cd ${_productPath}
  236. sudo bash ./bin/start.sh ${_productFileName}.jar
  237. "
  238. """
  239. }
  240. }
  241. }
  242. stage('生产环境(8.134.14.62)') {
  243. when {
  244. branch 'master' // 仅在多分支Pipeline有效
  245. }
  246. steps {
  247. script {
  248. echo "当前分支为:[${_git_branch}], 准备发布ing..."
  249. sh """
  250. ssh ${_remoteB} "
  251. source /etc/profile
  252. mkdir -pv ${_productBackupPath}
  253. mkdir -pv ${_productPath}
  254. rm -rf ${_productPath}/resources
  255. "
  256. """
  257. // rm -rf ${_productPath}/lib
  258. sh "scp -rv ${env.WORKSPACE}/${_productFileName}.jar ${_remoteB}:${_productBackupPath}/"
  259. // sh "scp -rv ${env.WORKSPACE}/lib ${_remoteB}:${_productPath}/lib"
  260. sh "scp -rv ${env.WORKSPACE}/resources ${_remoteB}:${_productPath}/resources"
  261. sh """
  262. ssh ${_remoteB} "
  263. source /etc/profile
  264. rm -rf ${_productPath}/*.jar
  265. rm -rf ${_productPath}/*.log
  266. cp ${_productBackupPath}/${_productFileName}.jar ${_productPath}/yxl-backend-1.jar
  267. cp ${_productBackupPath}/${_productFileName}.jar ${_productPath}/yxl-backend-2.jar
  268. cd ${_productPath}
  269. touch ${_buildTime}.log
  270. sudo bash ./bin-1/start.sh yxl-backend-1.jar
  271. sudo bash ./bin-2/start.sh yxl-backend-2.jar
  272. "
  273. """
  274. }
  275. }
  276. }
  277. }
  278. }