Jenkinsfile 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env groovy
  2. pipeline {
  3. agent any
  4. options {
  5. timestamps()
  6. }
  7. parameters {
  8. choice(
  9. name: 'profile',
  10. choices: ['prod', 'test'],
  11. description: '选择部署环境'
  12. )
  13. }
  14. environment {
  15. ARTIFACTID = "storleadwebsite"
  16. DOCKER_REGISTRY = "reg-aliyun"
  17. DOCKER_HOST = "registry.cn-shenzhen.aliyuncs.com"
  18. DOCKER_LOGIN_REGISTRY = "https://${DOCKER_HOST}"
  19. BUILD_PREFIX = "storlead-huawei"
  20. DOCKER_IMG_NAME = "${DOCKER_HOST}/${BUILD_PREFIX}/${ARTIFACTID}"
  21. SWARM_INIT_REPLICAS_NUM = 1
  22. SERVER_HOST_NAME = "test1"
  23. API_PORT = 3001
  24. }
  25. stages {
  26. stage('处理环境') {
  27. steps {
  28. script {
  29. if (params.profile == "test") {
  30. SERVER_HOST = "test1.storlead.com"
  31. SERVER_PORT = 53023
  32. SERVER_HOST_NAME = "test1"
  33. }
  34. if (params.profile == "prod") {
  35. SERVER_HOST = "110.41.82.21"
  36. SERVER_PORT = 22
  37. SERVER_HOST_NAME = "node2"
  38. }
  39. echo "部署服务器: ${SERVER_HOST}"
  40. }
  41. }
  42. }
  43. stage('部署服务') {
  44. steps {
  45. withCredentials([
  46. usernamePassword(credentialsId: 'storleadHW', usernameVariable: 'usernameHW', passwordVariable: 'passwordHW'),
  47. usernamePassword(credentialsId: 'storlead', usernameVariable: 'username', passwordVariable: 'password'),
  48. usernamePassword(credentialsId: 'reg-aliyun', usernameVariable: 'aliUsername', passwordVariable: 'aliPassword')
  49. ]) {
  50. script {
  51. def remote = [:]
  52. remote.name = "${SERVER_HOST}"
  53. remote.host = "${SERVER_HOST}"
  54. remote.port = SERVER_PORT
  55. remote.allowAnyHosts = true
  56. if (params.profile == "prod") {
  57. remote.user = usernameHW
  58. remote.password = passwordHW
  59. } else {
  60. remote.user = username
  61. remote.password = password
  62. }
  63. echo "登录阿里云镜像仓库"
  64. sshCommand remote: remote, command: """
  65. docker login -u ${aliUsername} -p ${aliPassword} ${DOCKER_LOGIN_REGISTRY}
  66. """
  67. echo "拉取镜像"
  68. sshCommand remote: remote, command: """
  69. docker pull ${DOCKER_IMG_NAME}:latest
  70. """
  71. sh """
  72. echo '
  73. version: "3.7"
  74. services:
  75. ${ARTIFACTID}-${params.profile}:
  76. image: ${DOCKER_IMG_NAME}:latest
  77. restart: always
  78. deploy:
  79. replicas: ${SWARM_INIT_REPLICAS_NUM}
  80. placement:
  81. constraints:
  82. - 'node.hostname == ${SERVER_HOST_NAME}'
  83. restart_policy:
  84. condition: on-failure
  85. environment:
  86. - "TZ=Asia/Shanghai"
  87. ports:
  88. - "${API_PORT}:3000"
  89. networks:
  90. - ${ARTIFACTID}
  91. networks:
  92. ${ARTIFACTID}:
  93. driver: overlay
  94. attachable: true
  95. ' > stack.yml
  96. """
  97. sshPut remote: remote, from: "stack.yml", into: "/docker/${ARTIFACTID}-stack.yml"
  98. echo "部署 docker swarm"
  99. sshCommand remote: remote, command: """
  100. docker stack deploy \
  101. --resolve-image=always \
  102. --with-registry-auth \
  103. -c /docker/${ARTIFACTID}-stack.yml \
  104. ${ARTIFACTID}-${params.profile}
  105. """
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }