Jenkinsfile 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. pipeline {
  2. agent any
  3. parameters {
  4. choice(name: 'ENV', choices: ['prod'], description: '部署环境')
  5. choice(name: 'SERVICE', choices: ['weichi-crm-gateway', 'weichi-crm-work', 'weichi-crm-oa', 'weichi-crm-authorization', 'weichi-crm-admin', 'weichi-crm-bi', 'weichi-crm-crm', 'weichi-crm-job', 'weichi-crm-examine', 'weichi-crm-hrm'], description: '选择要部署的微服务')
  6. }
  7. environment {
  8. DOCKER_REGISTRY = "registry.cn-shanghai.aliyuncs.com"
  9. DOCKER_NAMESPACE = "advich-group"
  10. DOCKER_REPO = "${params.SERVICE}"
  11. DOCKER_IMAGE = "${DOCKER_REGISTRY}/${DOCKER_NAMESPACE}/${DOCKER_REPO}:latest"
  12. }
  13. stages {
  14. stage('拉取Git代码') {
  15. steps {
  16. checkout scmGit(branches: [[name: '${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: 'fb3553e7-6ea6-4f34-92a0-6ba34a71c5b5', url: 'https://git.dev.advichcloud.com/wangfan/weichi-crm-server.git']])
  17. }
  18. }
  19. stage('Build') {
  20. steps {
  21. dir("${params.SERVICE}") {
  22. sh '''
  23. set -e
  24. mvn clean package -DskipTests
  25. '''
  26. }
  27. }
  28. }
  29. stage('Build and Push Docker Image') {
  30. steps {
  31. script {
  32. withCredentials([usernamePassword(
  33. credentialsId: '41a5dc65-0283-4d56-ace5-36bc08350b08',
  34. usernameVariable: 'REGISTRY_USERNAME',
  35. passwordVariable: 'REGISTRY_PASSWORD'
  36. )]) {
  37. sh """
  38. docker build -t ${DOCKER_IMAGE} .
  39. echo "\${REGISTRY_PASSWORD}" | docker login ${DOCKER_REGISTRY} -u \${REGISTRY_USERNAME} --password-stdin
  40. docker push ${DOCKER_IMAGE}
  41. """
  42. }
  43. }
  44. }
  45. }
  46. stage('Deploy to Server') {
  47. when {
  48. expression { return params.ENV == 'prod' }
  49. }
  50. steps {
  51. sshPublisher(publishers: [sshPublisherDesc(configName:'CRM(阿里云)', transfers: [sshTransfer(cleanRemote: false, excludes: '',
  52. execCommand: """
  53. docker login -u wangfan@1934160439855919 -p Initial0 ${DOCKER_REGISTRY}
  54. docker pull ${DOCKER_IMAGE}
  55. docker rm -f ${DOCKER_REPO} || true
  56. docker run -d --name ${DOCKER_REPO} ${DOCKER_IMAGE}
  57. """,
  58. execTimeout: 120000, flatten: false, makeEmptyDirs: false,
  59. noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '',
  60. remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')],
  61. usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose:
  62. false)])
  63. }
  64. }
  65. }
  66. post {
  67. always {
  68. cleanWs()
  69. }
  70. success {
  71. echo 'Deployment succeeded!'
  72. }
  73. failure {
  74. echo 'Deployment failed.'
  75. }
  76. }
  77. }