Jenkinsfile 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // 所有的脚本命令都放在pipeline中
  2. pipeline{
  3. // 指定任务在哪个集群节点中执行
  4. agent any
  5. // 声明全局变量,方便后面使用
  6. environment {
  7. harborUser = 'admin'
  8. harborPasswd = 'Harbor12345'
  9. harborAddress = '47.103.25.235:8899'
  10. harborRepo = 'repo'
  11. }
  12. stages{
  13. stage('拉取git仓库代码'){
  14. steps{
  15. checkout scmGit(branches: [[name: '${tag}']], extensions: [], userRemoteConfigs: [[credentialsId: '184a2c7a-1d7b-448f-90c4-3474c395c7b3', url: 'http://110.42.248.201:30000/chl/mytest.git']])
  16. }
  17. }
  18. stage('通过maven构建项目'){
  19. steps{
  20. sh '/var/jenkins_home/maven/bin/mvn clean package -DskipTests'
  21. }
  22. }
  23. stage('通过SonarQube做代码质量检测'){
  24. steps{
  25. sh '/var/jenkins_home/sonar-scanner/bin/sonar-scanner -Dsonar.source=./ -Dsonar.projectname=${JOB_NAME} -Dsonar.projectKey=${JOB_NAME} -Dsonar.java.binaries=./target/ -Dsonar.login=b5071fbe332d4863b0c96b0d648593b23ec8d6c8'
  26. }
  27. }
  28. stage('通过Docker制作自定义镜像'){
  29. steps{
  30. sh '''mv ./target/*.jar ./docker
  31. docker build -t ${JOB_NAME}:${tag} ./docker/'''
  32. }
  33. }
  34. stage('将自定义镜像推送到Harbor'){
  35. steps{
  36. sh '''docker login -u ${harborUser} -p ${harborPasswd} ${harborAddress}
  37. docker tag ${JOB_NAME}:${tag} ${harborAddress}/${harborRepo}/${JOB_NAME}:${tag}
  38. docker push ${harborAddress}/${harborRepo}/${JOB_NAME}:${tag}'''
  39. }
  40. }
  41. stage('通过Publish Over SSH通知目标服务器'){
  42. steps{
  43. sshPublisher(publishers: [sshPublisherDesc(configName: 'test', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "deploy.sh $harborAddress $harborRepo $JOB_NAME $tag $container_port $host_port", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
  44. echo '通过Publish Over SSH通知目标服务器 - SUCCESS'
  45. }
  46. }
  47. }
  48. post {
  49. success {
  50. dingtalk (
  51. robot: "test",
  52. type:'ACTION_CARD',
  53. atAll: false,
  54. title: "构建成功:${env.JOB_NAME}",
  55. //messageUrl: 'xxxx',
  56. text: [
  57. "### [${env.JOB_NAME}](${env.JOB_URL}) ",
  58. '---',
  59. "- 任务:[${currentBuild.displayName}](${env.BUILD_URL})",
  60. '- 状态:<font color=#00CD00 >成功</font>',
  61. "- 持续时间:${currentBuild.durationString}".split("and counting")[0],
  62. "- 执行人:${BUILD_USER}",
  63. ]
  64. )
  65. }
  66. failure{
  67. dingtalk (
  68. robot: "test",
  69. type:'ACTION_CARD',
  70. atAll: false,
  71. title: "构建失败:${env.JOB_NAME}",
  72. //messageUrl: 'xxxx',
  73. text: [
  74. "### [${env.JOB_NAME}](${env.JOB_URL}) ",
  75. '---',
  76. "- 任务:[${currentBuild.displayName}](${env.BUILD_URL})",
  77. '- 状态:<font color=#EE0000 >失败</font>',
  78. "- 持续时间:${currentBuild.durationString}".split("and counting")[0],
  79. "- 执行人:${BUILD_USER}",
  80. ]
  81. )
  82. }
  83. }
  84. }