Pārlūkot izejas kodu

启动脚本问题

chenkq 2 nedēļas atpakaļ
vecāks
revīzija
a9e2efdf9f
1 mainītis faili ar 61 papildinājumiem un 66 dzēšanām
  1. 61 66
      Jenkinsfile

+ 61 - 66
Jenkinsfile

@@ -283,74 +283,69 @@ pipeline {
                             //如果选择推送才使用远程的版本 如果不选择推送到远程则直接运行本机刚打包的镜像版本即可
                             sshCommand remote: remote, command: "docker pull ${DOCKER_IMG_NAME}"
                         }
-                        sh """
-                          echo '
-  version: "3.7"
-  services:
-    ${params.module}-${params.profile}:
-      image: ${DOCKER_IMG_NAME}
-      deploy:
-        replicas: ${SWARM_INIT_REPLICAS_NUM}
-        placement:
-          constraints:  
-            - 'node.hostname == ${SERVER_HOST_NAME}'
-
-        resources:
-          limits:
-            memory: 2G
-          reservations:
-            memory: 512M
 
-        update_config:
-          parallelism: 1
-          delay: 1m
-
-        restart_policy:
-          condition: on-failure
-          # 最多连续重试3次如果仍然失败则放弃重试
-          max_attempts: 3
-      healthcheck:
-        test: ["CMD", "curl", "-f", "http://localhost:${API_PORT}/${API_NAMESPACE_RESTFUL}/actuator"]
-        interval: 1m
-        timeout: 5s
-        retries: 3
-        start_period: 30s
-      environment:
-        - "TZ=Asia/Shanghai"
-        - "SPRING_PROFILES_ACTIVE=${params.profile}"
-        - "JAVA_OPTS=-Xms512m -Xmx1024m"
-      volumes:
-        - /app:/app
-        - /mnt/vdb/storlead:/mnt/vdb/storlead
-      ports:
-        - "${API_PORT}:${API_PORT}"
-        - "${API_REMOTE_DEBUG_PORT}:${API_REMOTE_DEBUG_PORT}"
-      networks:
-        - ${ARTIFACTID}
-      extra_hosts:
-        - "www.storlead.com:172.18.194.161"
-        - "node1:110.41.82.21"
-        - "node2:110.41.174.46"
-        - "node3:121.37.226.174"
-        - "test1.storlead.com:172.18.194.168"
-        - "test2.storlead.com:172.18.194.163"
-  networks:
-    ${ARTIFACTID}:
-       driver: overlay
-       external: true
-                          ' > ${params.module}-${params.profile}-stack.yml
-                         """
-                        //                        if (params.profile =="dev") {
-                        //                            sh "docker stack deploy -c ${ARTIFACTID}-${params.profile}-stack.yml ${ARTIFACTID}-${params.profile}"
+                        // 勿用 echo '...' 写 yml:约束行里的单引号会截断 shell 字符串,导致 stack yml 损坏
+                        def stackFile = "${params.module}-${params.profile}-stack.yml"
+                        def stackYaml = """\
+version: "3.7"
+services:
+  ${params.module}-${params.profile}:
+    image: ${DOCKER_IMG_NAME}
+    deploy:
+      replicas: ${SWARM_INIT_REPLICAS_NUM}
+      placement:
+        constraints:
+          - node.hostname == ${SERVER_HOST_NAME}
+      resources:
+        limits:
+          memory: 2G
+        reservations:
+          memory: 512M
+      update_config:
+        parallelism: 1
+        delay: 1m
+      restart_policy:
+        condition: on-failure
+        max_attempts: 3
+    healthcheck:
+      test: ["CMD-SHELL", "wget -q -O /dev/null http://localhost:${API_PORT}/${API_NAMESPACE_RESTFUL}/actuator || exit 1"]
+      interval: 1m
+      timeout: 5s
+      retries: 3
+      start_period: 30s
+    environment:
+      - TZ=Asia/Shanghai
+      - SPRING_PROFILES_ACTIVE=${params.profile}
+      - JAVA_OPTS=-Xms512m -Xmx1024m
+    volumes:
+      - /app:/app
+      - /mnt/vdb/storlead:/mnt/vdb/storlead
+    ports:
+      - "${API_PORT}:${API_PORT}"
+      - "${API_REMOTE_DEBUG_PORT}:${API_REMOTE_DEBUG_PORT}"
+    networks:
+      - ${ARTIFACTID}
+    extra_hosts:
+      - "www.storlead.com:172.18.194.161"
+      - "node1:110.41.82.21"
+      - "node2:110.41.174.46"
+      - "node3:121.37.226.174"
+      - "test1.storlead.com:172.18.194.168"
+      - "test2.storlead.com:172.18.194.163"
+networks:
+  ${ARTIFACTID}:
+    driver: overlay
+    external: true
+"""
+                        writeFile file: stackFile, text: stackYaml
+                        echo "----- stack yml -----"
+                        sh "cat ${stackFile}"
 
-                        //                            sh "docker ps -f name=${ARTIFACTID} -q | xargs --no-run-if-empty docker container stop"
-                        //                            sh "docker container ls -a -fname=${ARTIFACTID} -q | xargs -r docker container rm"
-                        //                            sh "docker run -d --restart=always -p ${API_PORT}:${API_PORT} -p ${API_REMOTE_DEBUG_PORT}:${API_REMOTE_DEBUG_PORT} -v /app:/app --name ${ARTIFACTID} ${DOCKER_IMG_NAME}"
-                        //                        } else {
-                        // 全部都以swarm 模式启动
-                        sshPut remote: remote, from: "${params.module}-${params.profile}-stack.yml", into: "/docker/sp/${params.module}-${params.profile}-stack.yml"
-                        sshCommand remote: remote, command: "docker stack deploy --resolve-image=always --with-registry-auth -c /docker/sp/${params.module}-${params.profile}-stack.yml ${params.module}-${params.profile}"
-                        //                        }
+                        // 全部都以swarm 模式启动;external 网络不存在时先创建
+                        sshCommand remote: remote, command: "mkdir -p /docker/sp"
+                        sshCommand remote: remote, command: "docker network inspect ${ARTIFACTID} >/dev/null 2>&1 || docker network create -d overlay --attachable ${ARTIFACTID}"
+                        sshPut remote: remote, from: stackFile, into: "/docker/sp/${stackFile}"
+                        sshCommand remote: remote, command: "docker stack deploy --resolve-image=always --with-registry-auth -c /docker/sp/${stackFile} ${params.module}-${params.profile}"
                     }
                 }
             }