Jenkinsfile 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. pipeline {
  2. agent any
  3. // tools { maven '' }
  4. // parameters {}
  5. environment {
  6. _productFileName = buildProductFileName() // 产物文件名
  7. _productBackupPath = '/app/csair/backup' // 产物备份目录
  8. }
  9. triggers {
  10. // cron('0 0 * * *') // 周期任务
  11. // pollSCM('H/1 * * * *') // 轮询代码仓库(每分钟判断一次代码是否有变化)
  12. gitlab(
  13. triggerOnPush: true,
  14. triggerOnMergeRequest: true,
  15. triggerOnNoteRequest: true,
  16. branchFilterType: 'All',
  17. secretToken: 'asdfghjkl'
  18. )
  19. }
  20. options {
  21. buildDiscarder(logRotator(numToKeepStr: '10')) // 保存最近历史构建记录的数量
  22. disableConcurrentBuilds() // 同一个pipeline,Jenkins默认是可以同时执行多次的,此选项为了禁止pipeline同时执行
  23. // checkoutToSubdirectory('sub') // Jenkins默认拉取源码至工作空间的根目录中,此选项可以指定检出到工作空间的子目录中
  24. retry(2) // 当发生失败时进行重试(包括第1次失败)
  25. timestamps() // 添加日志打印时间
  26. timeout(time: 15, unit: 'MINUTES') // 如果pipeline执行时间过长,超出了设置的timeout时间,Jenkins将中止pipeline(SECONDS、MINUTES、HOURS)
  27. gitLabConnection('gitlab') // 连接gitlab服务(需要在Jenkins中设置Jenkins -> Configure System)
  28. }
  29. post {
  30. always { // 不论当前完成状态是什么,都执行
  31. // pom.xml中增加plugin -> maven-pmd-plugin
  32. // Jenkins PMD插件[PMD](https://plugins.jenkins.io/pmd)
  33. pmd(canRunOnFailed: true, pattern: '**/target/pmd.xml')
  34. cleanWs() // 清理工作空间插件[Workspace Cleanup Plugin](https://plugins.jenkins.io/ws-cleanup)
  35. }
  36. failure {
  37. updateGitlabCommitStatus name: 'build', state: 'failed'
  38. }
  39. success {
  40. updateGitlabCommitStatus name: 'build', state: 'success'
  41. }
  42. }
  43. stages {
  44. stage('Env & Param') {
  45. parallel {
  46. stage('Env') {
  47. steps {
  48. sh 'printenv'
  49. echo "系统当前用户 [${env.USER}]"
  50. echo "WORKSPACE [${env.WORKSPACE}]"
  51. echo "JENKINS_URL [${env.JENKINS_URL}]"
  52. echo "${_productFileName}"
  53. }
  54. }
  55. stage('Job') {
  56. steps {
  57. echo "Running [${env.BUILD_NUMBER}] on [${env.BUILD_URL}]"
  58. echo "BRANCH_NAME [${env.BRANCH_NAME}] GIT_BRANCH [${env.GIT_BRANCH}]"
  59. }
  60. }
  61. }
  62. }
  63. stage('Build') {
  64. steps {
  65. sh 'mvn clean package -Dautoconfig.skip=true -Dmaven.test.skip=true'
  66. }
  67. }
  68. stage('Static check') {
  69. parallel {
  70. stage('pmd') {
  71. steps {
  72. sh 'mvn pmd:pmd'
  73. }
  74. }
  75. }
  76. }
  77. stage('Product') {
  78. steps {
  79. dir("${env.WORKSPACE}/target") {
  80. sh "mv ${env.WORKSPACE}/target/*.jar ${env.WORKSPACE}/target/${_productFileName}"
  81. }
  82. archiveArtifacts(artifacts: '**/target/*.jar', caseSensitive: true, fingerprint: true)
  83. }
  84. }
  85. stage('Release') {
  86. parallel {
  87. stage('Test') {
  88. when {
  89. branch 'test'
  90. }
  91. steps {
  92. echo 'Test环境'
  93. }
  94. }
  95. stage('Master') {
  96. when {
  97. branch 'master'
  98. }
  99. steps {
  100. script {
  101. // 判断产物备份目录是否存在,否则创建文件夹
  102. if(!fileExists("${_productBackupPath}")) {
  103. sh "mkdir ${_productBackupPath}"
  104. }
  105. }
  106. sh "scp -r ${WORKSPACE}/target/${_productFileName} ${_productBackupPath}"
  107. }
  108. }
  109. stage('Tag') {
  110. when {
  111. tag "v*.*.*"
  112. }
  113. steps {
  114. script {
  115. // 判断产物备份目录是否存在,否则创建文件夹
  116. if(!fileExists("${_productBackupPath}")) {
  117. sh "mkdir ${_productBackupPath}"
  118. }
  119. }
  120. sh "scp -r ${WORKSPACE}/target/${_productFileName} ${_productBackupPath}"
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }
  127. // 生成产物的文件名
  128. def buildProductFileName() {
  129. String projectName = "${env.JOB_NAME}".tokenize('//')[0]
  130. String branchName = "${env.JOB_NAME}".tokenize('//')[1]
  131. String date = new Date().format('yyyyMMddHHmmss')
  132. return "${projectName}-${branchName}-${date}.jar"
  133. }