Jenkinsfile 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 rmi ${DOCKER_IMG_NAME}:latest || true
  70. // """
  71. echo "拉取镜像"
  72. sshCommand remote: remote, command: """
  73. docker pull ${DOCKER_IMG_NAME}:latest
  74. """
  75. sh """
  76. echo '
  77. version: "3.7"
  78. services:
  79. ${ARTIFACTID}-${params.profile}:
  80. image: ${DOCKER_IMG_NAME}:latest
  81. restart: always
  82. deploy:
  83. replicas: ${SWARM_INIT_REPLICAS_NUM}
  84. placement:
  85. constraints:
  86. - 'node.hostname == ${SERVER_HOST_NAME}'
  87. restart_policy:
  88. condition: on-failure
  89. environment:
  90. - "TZ=Asia/Shanghai"
  91. env_file:
  92. - /docker/sp/storleadwebsite.env
  93. ports:
  94. - "${API_PORT}:3000"
  95. networks:
  96. - ${ARTIFACTID}
  97. networks:
  98. ${ARTIFACTID}:
  99. driver: overlay
  100. attachable: true
  101. ' > stack.yml
  102. """
  103. sshPut remote: remote, from: "stack.yml", into: "/docker/sp/${ARTIFACTID}-stack.yml"
  104. echo "部署 docker swarm"
  105. sshCommand remote: remote, command: """
  106. docker stack deploy \
  107. --resolve-image=always \
  108. --with-registry-auth \
  109. -c /docker/sp/${ARTIFACTID}-stack.yml \
  110. ${ARTIFACTID}-${params.profile}
  111. """
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }