Jenkinsfile 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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-web-admin-frontend_${_git_branch}_${_buildTime}.tar.gz" // 产物文件名
  23. _productPath = '/app/430-platform-/back-end-ui' // 产物启动目录
  24. _productBackupPath = '/app/backup/430-platform-/back-end-ui' // 产物备份目录
  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. cleanWs() // 清理工作空间插件[Workspace Cleanup Plugin](https://plugins.jenkins.io/ws-cleanup)
  65. }
  66. success { // 成功后执行
  67. updateGitlabCommitStatus name: 'build', state: 'success'
  68. script {
  69. qyWechatRobot.endBuild('构建成功😀', _qyWechatRobotToken)
  70. }
  71. }
  72. failure { // 失败后执行
  73. updateGitlabCommitStatus name: 'build', state: 'failed'
  74. script {
  75. qyWechatRobot.endBuild('构建失败🤬', _qyWechatRobotToken)
  76. }
  77. }
  78. aborted { // 取消后执行
  79. script {
  80. qyWechatRobot.endBuild('构建取消🤯', _qyWechatRobotToken)
  81. }
  82. }
  83. }
  84. stages {
  85. stage('Start Notification') {
  86. steps {
  87. script {
  88. qyWechatRobot.startBuild(_qyWechatRobotToken)
  89. }
  90. }
  91. }
  92. stage('Env & Param') {
  93. parallel {
  94. stage('Env') {
  95. steps {
  96. sh 'printenv'
  97. echo "系统当前用户 [${env.USER}]"
  98. echo "JENKINS_URL [${env.JENKINS_URL}]"
  99. echo "WORKSPACE [${env.WORKSPACE}]"
  100. }
  101. }
  102. stage('Job') {
  103. steps {
  104. echo "-------------------- --------------------"
  105. echo "JOB_NAME [${env.JOB_NAME}]"
  106. echo "BUILD_NUMBER [${env.BUILD_NUMBER}]"
  107. echo "BUILD_URL [${env.BUILD_URL}]"
  108. echo "BUILD_NUMBER [${env.BUILD_NUMBER}]"
  109. echo "-------------------- --------------------"
  110. echo "BRANCH_NAME [${env.BRANCH_NAME}]"
  111. echo "GIT_BRANCH [${env.GIT_BRANCH}]"
  112. echo "GIT_COMMIT [${env.GIT_COMMIT}]"
  113. echo "GIT_COMMIT [${env.commit}]"
  114. echo "_git_tag [${_git_tag}]"
  115. echo "_git_branch [${_git_branch}]"
  116. echo "-------------------- --------------------"
  117. echo "产物名称 [${_productFileName}]"
  118. sh "node -v"
  119. sh "npm -v"
  120. }
  121. }
  122. }
  123. }
  124. stage('app') {
  125. parallel {
  126. stage('admin') {
  127. steps {
  128. dir("${env.WORKSPACE}") {
  129. sh 'npm install'
  130. sh 'npm run build'
  131. }
  132. dir("${env.WORKSPACE}/dist") {
  133. sh "tar -zcvf ${env.WORKSPACE}/430-platform-web-admin-frontend_${_git_branch}_${_buildTime}.tar.gz ./"
  134. }
  135. }
  136. }
  137. }
  138. }
  139. stage('Product') {
  140. steps {
  141. archiveArtifacts(
  142. artifacts: '*.tar.gz', // 字符串类型,需要归档的文件路径,使用的是Ant风格路径表达式。
  143. // excludes: '', // 字符串类型,需要排除的文件路径,使用的也是Ant风格路径表达式。
  144. caseSensitive: true, // 布尔类型,对路径大小写是否敏感。
  145. fingerprint: true, // 布尔类型,是否对归档的文件进行签名。
  146. onlyIfSuccessful: true // 布尔类型,只在构建成功时进行归档。
  147. )
  148. }
  149. }
  150. stage('Pre-Release') {
  151. steps {
  152. script {
  153. if ("${_git_branch}" == 'master') {
  154. echo "当前分支为:[${_git_branch}], 准备发布ing..."
  155. } else {
  156. error("当前分支为:[${_git_branch}], 终止当前发布操作...")
  157. }
  158. }
  159. }
  160. }
  161. stage('Release') {
  162. parallel {
  163. stage('master分支') {
  164. steps {
  165. script {
  166. echo "当前分支为:[${_git_branch}], 准备发布ing..."
  167. sh """
  168. ssh ${_remote} "
  169. source /etc/profile
  170. mkdir -pv ${_productBackupPath}
  171. mkdir -pv ${_productPath}
  172. "
  173. """
  174. sh "scp -r ${WORKSPACE}/${_productFileName} ${_remote}:${_productBackupPath}"
  175. sh """
  176. ssh ${_remote} "
  177. source /etc/profile
  178. rm -rf ${_productPath}/*
  179. touch ${_productPath}/${_git_branch}_${_buildTime}.log
  180. tar -zxvf ${_productBackupPath}/${_productFileName} -C ${_productPath}
  181. "
  182. """
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }