Quellcode durchsuchen

feat: 前端部署模板,待测试

370774330@qq.com vor 5 Jahren
Ursprung
Commit
3a6f2039c0

+ 1 - 0
core/src/main/java/seecoder/devcloud/core/pipeline/config/HandlerConfigTable.java

@@ -26,6 +26,7 @@ public class HandlerConfigTable {
      */
     static {
         handlerConfigMap = new HashMap<>();
+        handlerConfigMap.put("node-image-build", NodeImageBuildHandler.class);
         handlerConfigMap.put("java-image-build", JavaImageBuildHandler.class);
         handlerConfigMap.put("k8s-deployment", K8sDeploymentHandler.class);
         handlerConfigMap.put("k8s-service", K8sServiceHandler.class);

+ 2 - 2
core/src/main/java/seecoder/devcloud/core/pipeline/handler/K8sDeploymentHandler.java

@@ -54,8 +54,8 @@ public class K8sDeploymentHandler extends AbstractHandler {
         //0. 配置初始化
         String imageName = context.getConfigs().get("imageName");
         String imageTag = context.getConfigs().get("imageTag");
-        int port = Integer.parseInt(context.getConfigs().getOrDefault("port", "8000"));
-        int replicas = Integer.parseInt(context.getConfigs().getOrDefault("replicas", "1"));
+        int port = Integer.parseInt(context.getConfigs().get("port"));
+        int replicas = 1;
         String namespace = context.getNamespace();
         String projectName = context.getProjectName();
 

+ 92 - 0
core/src/main/java/seecoder/devcloud/core/pipeline/handler/NodeImageBuildHandler.java

@@ -0,0 +1,92 @@
+package seecoder.devcloud.core.pipeline.handler;
+
+import com.spotify.docker.client.ProgressHandler;
+import com.spotify.docker.client.exceptions.DockerException;
+import com.spotify.docker.client.messages.ProgressMessage;
+import org.apache.commons.lang.StringUtils;
+import org.apache.http.HttpStatus;
+import org.eclipse.jgit.api.Git;
+import seecoder.devcloud.api.docker.DockerApi;
+import seecoder.devcloud.api.git.GitApi;
+import seecoder.devcloud.common.util.SpringUtil;
+import seecoder.devcloud.core.pipeline.Context;
+import seecoder.devcloud.core.pipeline.PipelineException;
+import seecoder.devcloud.core.pipeline.template.PipelineTemplateTable;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Date;
+
+/**
+ * @author PuHong Weng
+ * @date 2021/3/24
+ * @description:
+ */
+public class NodeImageBuildHandler extends AbstractHandler{
+    public final DockerApi dockerApi;
+
+    public final GitApi gitApi;
+
+    public NodeImageBuildHandler() {
+        dockerApi = SpringUtil.getBean(DockerApi.class);
+        gitApi = SpringUtil.getBean(GitApi.class);
+    }
+
+    @Override
+    public void process(Context context) throws PipelineException {
+        //0. 变量准备
+        String repoUrl = context.getConfigs().get("repoUrl");
+        String branchName = context.getConfigs().getOrDefault("branchName","master");
+        String templateName = context.getTemplateName();
+        String imageName = context.getNamespace() + "-" + context.getProjectName();
+
+        // 1. 有git从git拿到地址仓库
+        Path directory;
+        try {
+            //注:temp文件/目录系统会定时删,实际文件名喂 prefix + 它生成的suffix随机数,所以不会命名冲突
+            directory = Files.createTempDirectory("seecoder-devcloud-");
+            Git git = gitApi.clone(repoUrl, directory.toString());
+            git.checkout().setName(branchName).call();
+        } catch (Exception e) {
+            context.appendErrorResult(PipelineException.GIT_CLONE_ERROR,e);
+            throw new PipelineException(HttpStatus.SC_SERVICE_UNAVAILABLE, PipelineException.GIT_CLONE_ERROR,e);
+        }
+        //2. 帮助学生创建dockerfile
+        Path dockerfile = Paths.get(directory.toString(), "Dockerfile");
+        try {
+            PipelineTemplateTable.copyTemplateDockerfile(templateName,dockerfile);
+        } catch (IOException e) {
+            context.appendErrorResult(PipelineException.DOCKERFILE_CREATE_ERROR,e);
+            throw new PipelineException(HttpStatus.SC_SERVICE_UNAVAILABLE,PipelineException.DOCKERFILE_CREATE_ERROR,e);
+        }
+        //3. 镜像构建
+        String imageTag = String.valueOf(new Date().getTime());
+        StringBuilder sb = new StringBuilder();
+        try {
+            dockerApi.buildAndPush(directory.toString(), "seecoder-devcloud-" + imageName, imageTag, new ProgressHandler() {
+                @Override
+                public void progress(ProgressMessage message) throws DockerException {
+                    String value = message.stream();
+                    if (value == null) {
+                        value = "";
+                    }
+                    if (!StringUtils.isEmpty(message.error())) {
+                        value += "\n" + message.error();
+                        sb.append(value);
+                        System.out.println(sb.toString());
+                    }
+                    context.setResult(context.getResult() + sb.toString());
+                }
+            });
+        } catch (Exception e) {
+            context.appendErrorResult(PipelineException.IMAGE_BUILD_ERROR,e);
+            throw new PipelineException(HttpStatus.SC_SERVICE_UNAVAILABLE, PipelineException.IMAGE_BUILD_ERROR ,e);
+        }
+        //6. 将镜像名字加入context
+        context.getConfigs().put("imageName", imageName);
+        context.getConfigs().put("imageTag", imageTag);
+        context.appendSuccessResult("镜像构建成功,已推送进仓库");
+    }
+}

+ 1 - 0
core/src/main/java/seecoder/devcloud/core/pipeline/template/PipelineTemplateTable.java

@@ -29,6 +29,7 @@ public class PipelineTemplateTable {
      */
     static {
         templateNames = new HashSet<String>();
+        templateNames.add("NODE-10");
         templateNames.add("SPRINGBOOT-JAVA8");
 
     }

+ 30 - 0
core/src/main/resources/template/config/NODE-10.json

@@ -0,0 +1,30 @@
+[
+  {
+    "name": "node-image-build",
+    "configs": {
+      "repoUrl": "#todo",
+      "branchName": "#todo"
+    }
+  },
+  {
+    "name": "k8s-namespace",
+    "configs": {
+    }
+  },
+  {
+    "name": "k8s-deployment",
+    "configs": {
+      "port": "#todo"
+    }
+  },
+  {
+    "name": "k8s-service",
+    "configs": {
+    }
+  },
+  {
+    "name": "k8s-ingress",
+    "configs": {
+    }
+  }
+]

+ 1 - 1
core/src/main/resources/template/config/SPRINGBOOT-JAVA8.json

@@ -1,6 +1,6 @@
 [
   {
-    "name": "image-build",
+    "name": "java-image-build",
     "configs": {
       "repoUrl": "#todo",
       "branchName": "#todo"

+ 10 - 0
core/src/main/resources/template/dockerfile/NODE-10

@@ -0,0 +1,10 @@
+# 前端打包dockerfile
+FROM node:10-alpine as builder
+WORKDIR /code
+RUN yarn config set registry https://registry.npm.taobao.org/
+ADD package.json /code
+RUN yarn install
+ADD . /code
+RUN yarn build
+FROM nginx:1.19-alpine
+COPY --from=builder /code/dist/ /var/www/html/dist/

+ 1 - 1
web/src/main/resources/application-wph.yml

@@ -34,7 +34,7 @@ seecoder:
     ingressHostSuffix: .devcloud.seec.seecoder.cn
   gitlab:
     host: http://gitlab.192.168.99.105.nip.io
-    webhookProxy: url
+    webhookProxy: url/hook
     token: wXDeETv2Kmt-JhiApJ9H
   mail:
     from: 370774330@qq.com