Jenkinsfile-test 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env groovy
  2. pipeline {
  3. options {
  4. //禁止并发构建
  5. disableConcurrentBuilds()
  6. //持久化工件和控制台输出,用于保存Pipeline最近几次运行的数据
  7. buildDiscarder(logRotator(numToKeepStr: '20'))
  8. preserveStashes(buildCount: 5)
  9. //设置Pipeline运行的超时时间,之后Jenkins应该中止Pipeline
  10. timeout(time: 1, unit: 'HOURS')
  11. }
  12. agent {
  13. kubernetes {
  14. label "slave-${UUID.randomUUID().toString()}"
  15. cloud "Kubernetes"
  16. }
  17. }
  18. environment {
  19. PROJECT_NAME = 'hk-boa'
  20. IMAGE_NAME = 'services'
  21. CONTAINER_PORT = '8081'
  22. ENV = 'test'
  23. }
  24. parameters {
  25. string(name: 'REPLICAS', defaultValue: '1', description: '部署副本数')
  26. string(name: 'JVM_OPTS', defaultValue: '-Xmx500m -Xms500m -Xmn200m ', description: 'JVM信息')
  27. string(name: 'NODE_PORT', defaultValue: '30710', description: '发布端口')
  28. string(name: 'CPU', defaultValue: '500', description: '限制最大能使用的CPU,1000为1核')
  29. string(name: 'MEMORY', defaultValue: '600', description: '限制最大能使用的内存数量单位M')
  30. string(name: 'EMAILS', defaultValue: 'fengsw@bjrrtx.com', description: '企业微信通知人员,多个使用逗号隔开')
  31. booleanParam(name: 'FIRST_BUILD', defaultValue: false, description: '是否是首次构建')
  32. }
  33. stages {
  34. stage('拉取代码') {
  35. steps {
  36. checkout scm
  37. }
  38. }
  39. stage('构建应用') {
  40. steps {
  41. echo "构建应用"
  42. sh 'mvn clean package -U -Dmaven.test.skip=true --settings /data/nfs/maven/settings.xml'
  43. }
  44. }
  45. // stage("测试应用") {
  46. // steps {
  47. // withSonarQubeEnv('sonar') {
  48. // sh 'mvn clean sonar:sonar --settings /data/nfs/maven/settings.xml'
  49. // }
  50. // }
  51. // }
  52. // stage("代码质量检查") {
  53. // steps {
  54. // timeout(time: 1, unit: 'HOURS') {
  55. // waitForQualityGate abortPipeline: true
  56. // }
  57. // }
  58. // }
  59. stage('构建镜像') {
  60. steps {
  61. echo "构建镜像"
  62. sh "docker build -t reg.bjrrtx.com:5000/${env.PROJECT_NAME}/${env.IMAGE_NAME}-${ENV}:v${env.BUILD_NUMBER} -f deploy/Dockerfile ."
  63. }
  64. }
  65. stage('推送镜像') {
  66. steps {
  67. echo "推送镜像"
  68. sh '/opt/accessAuth.sh'
  69. sh "docker push reg.bjrrtx.com:5000/${env.PROJECT_NAME}/${env.IMAGE_NAME}-${ENV}:v${env.BUILD_NUMBER}"
  70. }
  71. }
  72. stage('部署应用') {
  73. steps {
  74. sh "sed -i \"s/{PROJECT_NAME}/${PROJECT_NAME}/g\" deploy/deploy.yaml"
  75. sh "sed -i \"s/{IMAGE_NAME}/${IMAGE_NAME}/g\" deploy/deploy.yaml"
  76. sh "sed -i \"s/{CONTAINER_PORT}/${CONTAINER_PORT}/g\" deploy/deploy.yaml"
  77. sh "sed -i \"s/{REPLICAS}/${params.REPLICAS}/g\" deploy/deploy.yaml"
  78. sh "sed -i \"s/{JVM_OPTS}/${params.JVM_OPTS}/g\" deploy/deploy.yaml"
  79. sh "sed -i \"s/{ENV}/${ENV}/g\" deploy/deploy.yaml"
  80. sh "sed -i \"s/{NODE_PORT}/${params.NODE_PORT}/g\" deploy/deploy.yaml"
  81. sh "sed -i \"s/{CPU}/${params.CPU}/g\" deploy/deploy.yaml"
  82. sh "sed -i \"s/{MEMORY}/${params.MEMORY}/g\" deploy/deploy.yaml"
  83. sh "sed -i \"s/{BUILD_NUMBER}/${env.BUILD_NUMBER}/g\" deploy/deploy.yaml"
  84. script {
  85. if (FIRST_BUILD == 'true') {
  86. sh 'kubectl create -f deploy/deploy.yaml'
  87. } else {
  88. sh 'kubectl delete -f deploy/deploy.yaml'
  89. sh 'kubectl create -f deploy/deploy.yaml'
  90. }
  91. }
  92. }
  93. }
  94. stage('发送企业微信通知') {
  95. steps {
  96. script {
  97. def info = "发版工程:${env.JOB_NAME} \\n发版结果:<font color=\\\"info\\\">成功!</font>\\n发布版本:V${env.BUILD_NUMBER} \\n更新记录:\\n" + getChanges()
  98. sh "curl -X GET http://mb.test.bjrrtx.com/magpie-bridge/jenkinsNotify -H \'Content-Type: application/json\' --data-binary \'{\"emails\":[\"${params.EMAILS}\"], \"content\":\"${info} \" }\' "
  99. }
  100. }
  101. }
  102. }
  103. post {
  104. always {
  105. echo 'This will always run'
  106. }
  107. success {
  108. echo "This will run if success"
  109. }
  110. failure {
  111. script {
  112. def info = "发版工程:${env.JOB_NAME} \\n发版结果:<font color=\\\"warning\\\">失败!</font>\\n发布版本:V${env.BUILD_NUMBER} \\n更新记录:\\n" + getChanges()
  113. sh "curl -X GET http://mb.test.bjrrtx.com/magpie-bridge/jenkinsNotify -H \'Content-Type: application/json\' --data-binary \'{\"emails\":[\"${params.EMAILS}\"], \"content\":\"${info} \" }\' "
  114. }
  115. }
  116. unstable {
  117. echo 'This will run only if the run was marked as unstable'
  118. }
  119. changed {
  120. echo 'This will run only if the state of the Pipeline has changed'
  121. echo 'For example, if the Pipeline was previously failing but is now successful'
  122. }
  123. }
  124. }
  125. @NonCPS
  126. def getChanges() {
  127. MAX_MSG_LEN = 100
  128. def changeString = ""
  129. def changeLogSets = currentBuild.changeSets
  130. for (int i = 0; i < changeLogSets.size(); i++) {
  131. def entries = changeLogSets[i].items
  132. for (int j = 0; j < entries.length; j++) {
  133. def entry = entries[j]
  134. change_msg = entry.msg.take(MAX_MSG_LEN)
  135. change_author = entry.author.fullName
  136. changeString += " ${change_msg}" + "---" + "${change_author}\\n"
  137. }
  138. }
  139. if (!changeString) {
  140. changeString = " No changes "
  141. }
  142. return changeString
  143. }