Jenkinsfile 8.1 KB

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