Jelajahi Sumber

调整 OssStaticPack 和 ZipPack 实现

reghao 1 hari lalu
induk
melakukan
edb0b979bd

+ 22 - 27
mgr/src/main/java/cn/reghao/devops/mgr/ops/builder/tool/packer/StaticPack.java → mgr/src/main/java/cn/reghao/devops/mgr/ops/builder/tool/packer/OssStaticPack.java

@@ -19,14 +19,7 @@ import java.io.IOException;
  */
 @Slf4j
 @Component
-public class StaticPack implements CodePacker {
-    // TODO 必须指定构建存在的目录
-    private String artifactPath;
-
-    /*public StaticPack(String artifactPath) {
-        this.artifactPath = artifactPath;
-    }*/
-
+public class OssStaticPack implements CodePacker {
     private void createTargetDir(String targetPath) throws IOException {
         File dir = new File(targetPath);
         if (!dir.exists()) {
@@ -34,30 +27,32 @@ public class StaticPack implements CodePacker {
         }
     }
 
-    //@Override
-    public String pack(String appId, String commitId, String appRootPath, String dockerfile) throws Exception {
-        if (artifactPath == null) {
-            throw new Exception("存放编译产物的路径 artifactPath 不能是 null");
-        }
+    @Override
+    public void pack(PipelineContext ctx, PackerConfig packerConfig) {
+        String appId = ctx.getAppId();
+        String commitId = ctx.getCommitInfo().getCommitId();
+        String artifactPath = packerConfig.getArtifactPath();
 
-        String sourceDirPath = appRootPath + File.separator + artifactPath;
+        String sourceCodeDir = ctx.getCompileDir();
+        String sourceDirPath = sourceCodeDir + File.separator + artifactPath;
         File srcDir = new File(sourceDirPath);
         String targetDirPath = LocalBuildDir.packDir + File.separator + appId;
-        createTargetDir(targetDirPath);
-
-        String dirname = String.format("%s_%s", appId, commitId);
-        String destDirPath = targetDirPath + File.separator + dirname;
-        File destDir = new File(destDirPath);
-        if (destDir.exists()) {
-            FileUtils.deleteDirectory(destDir);
-            log.info("delete exist dir {}", destDirPath);
+        try {
+            createTargetDir(targetDirPath);
+
+            String dirname = String.format("%s_%s", appId, commitId);
+            String destDirPath = targetDirPath + File.separator + dirname;
+            File destDir = new File(destDirPath);
+            if (destDir.exists()) {
+                FileUtils.deleteDirectory(destDir);
+                log.info("delete exist dir {}", destDirPath);
+            }
+            FileUtils.copyDirectory(srcDir, destDir);
+            ctx.setBuildTargetPath(destDirPath);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
         }
-        FileUtils.copyDirectory(srcDir, destDir);
-        return "";
-    }
 
-    @Override
-    public void pack(PipelineContext ctx, PackerConfig packerConfig) {
     }
 
     @Override

+ 18 - 24
mgr/src/main/java/cn/reghao/devops/mgr/ops/builder/tool/packer/ZipPack.java

@@ -18,13 +18,6 @@ import java.io.IOException;
 @Slf4j
 @Component
 public class ZipPack implements CodePacker {
-    // TODO 必须指定构建存在的目录
-    private String artifactPath;
-
-    /*public ZipPack(String artifactPath) {
-        this.artifactPath = artifactPath;
-    }*/
-
     private void createTargetDir(String targetPath) throws IOException {
         File dir = new File(targetPath);
         if (!dir.exists()) {
@@ -32,25 +25,26 @@ public class ZipPack implements CodePacker {
         }
     }
 
-    //@Override
-    public String pack(String appId, String commitId, String appRootPath, String dockerfile) throws Exception {
-        if (artifactPath == null) {
-            throw new Exception("构建产物的路径 artifactPath 不能是 null");
-        }
-
-        String sourceDirPath = appRootPath + File.separator + artifactPath;
-        String targetDirPath = LocalBuildDir.packDir + File.separator + appId;
-        createTargetDir(targetDirPath);
-
-        String filename = String.format("%s_%s.zip", appId, commitId);
-        String dest = targetDirPath + File.separator + filename;
-        String url = String.format("/api/app/bd/dl/%s", filename);
-        ZipFiles.zip(new File(sourceDirPath), dest);
-        return url;
-    }
-
     @Override
     public void pack(PipelineContext ctx, PackerConfig packerConfig) {
+        String appId = ctx.getAppId();
+        String commitId = ctx.getCommitInfo().getCommitId();
+        String artifactPath = packerConfig.getArtifactPath();
+
+        String sourceCodeDir = ctx.getCompileDir();
+        String sourceDirPath = sourceCodeDir + File.separator + artifactPath;
+        String targetDirPath = LocalBuildDir.packDir + File.separator + appId;
+        try {
+            createTargetDir(targetDirPath);
+
+            String filename = String.format("%s_%s.zip", appId, commitId);
+            String dest = targetDirPath + File.separator + filename;
+            String url = String.format("/api/app/bd/dl/%s", filename);
+            ZipFiles.zip(new File(sourceDirPath), dest);
+            ctx.setBuildTargetPath(url);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
     }
 
     @Override