Jenkinsfile 3.8 KB

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