Jenkinsfile 8.4 KB

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