From d9cc9d5dc1b420c8250ac4244537b52105cb4514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Sat, 7 Sep 2024 19:41:24 +0800 Subject: [PATCH] 'commit' --- ds-build/src/main/java/Util/PublishUtil.java | 70 +++++++++++++------ .../{buildImage.java => build_ds_gw.java} | 2 +- ds-build/src/main/resources/publishImage.json | 7 +- ds-build/target/classes/publishImage.json | 15 ++++ 4 files changed, 67 insertions(+), 27 deletions(-) rename ds-build/src/main/java/{buildImage.java => build_ds_gw.java} (87%) create mode 100644 ds-build/target/classes/publishImage.json diff --git a/ds-build/src/main/java/Util/PublishUtil.java b/ds-build/src/main/java/Util/PublishUtil.java index a1c3d5dc..1d7ea361 100644 --- a/ds-build/src/main/java/Util/PublishUtil.java +++ b/ds-build/src/main/java/Util/PublishUtil.java @@ -1,6 +1,7 @@ package Util; import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.file.PathUtil; import cn.hutool.core.util.ZipUtil; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; @@ -41,15 +42,20 @@ public class PublishUtil { int port = jo.getIntValue("port"); JSONArray ja = jo.getJSONArray("project"); - String workingPath = null, remotePath = null, localLibPath = null; + String workingPath = null, remotePath, localLibPath = null, localStatic = null, localNginxConf = null; for (Object o : ja) { JSONObject project = (JSONObject) o; String p = project.getString("projectName"); if (p.equals(projectName)) { workingPath = project.getString("workingPath"); - remotePath = project.getString("remotePath"); localLibPath = project.getString("localLibPath"); + if (project.getString("localStatic") != null) { + localStatic = workingPath + project.getString("localStatic"); + } + if (project.getString("localNginxConf") != null) { + localNginxConf = workingPath + project.getString("localNginxConf"); + } break; } } @@ -65,54 +71,72 @@ public class PublishUtil { newVersion = newVersion.replace(" ", "").replace("-", "").replace(":", ""); //准备工作 + remotePath = "/usr/local/" + projectName; ssh.exec("rm -rf " + remotePath); + ssh.mkdir(remotePath); System.out.println("正在上传Dockerfile..."); - ssh.upload(workingPath + "/Dockerfile", "/usr/local/" + projectName + "/Dockerfile"); + ssh.upload(workingPath + "/Dockerfile", remotePath + "/Dockerfile"); if (!StrKit.isBlank(localLibPath)) { System.out.println("正在创建lib目录..."); - ssh.mkdir("/usr/local/" + projectName + "/lib"); - ssh.mkdir(remotePath); + ssh.mkdir(remotePath + "/lib"); //遍历lib目录下的文件 for (File file : FileUtil.loopFiles(localLibPath)) { System.out.println("正在上传jar包:" + file.getName()); ssh.upload(file.getAbsolutePath(), remotePath + "/lib/" + file.getName()); } } + if (!StrKit.isBlank(localStatic)) { + System.out.println("正在创建static目录..."); + ssh.mkdir(remotePath + "/static"); + //遍历static目录下的文件 + for (File file : FileUtil.loopFiles(localStatic)) { + System.out.println("正在上传文件:" + file.getName()); + ssh.upload(file.getAbsolutePath(), remotePath + "/static/" + file.getName()); + } + } + if (!StrKit.isBlank(localNginxConf)) { + System.out.println("正在上传nginx.conf..."); + File file = new File(localNginxConf); + ssh.upload(file.getAbsolutePath(), remotePath + "/" + file.getName()); + System.out.println("成功完成上传文件nginx.conf"); + } //打包target为zip - String localFile = workingPath + "/target.zip"; - if (FileUtil.exist(localFile)) { + if (FileUtil.isDirectory(workingPath + "/target")) { + String localFile = workingPath + "/target.zip"; + if (FileUtil.exist(localFile)) { + FileUtil.del(localFile); + } + System.out.println("正在上传target.zip..."); + // 将目录打包成ZIP文件,不包含目录本身,只包含目录下的文件和子目录 + ZipUtil.zip(workingPath + "/target", localFile, true); + //上传 + ssh.upload(localFile, remotePath + "/target.zip"); + //删除target.zip FileUtil.del(localFile); + //解压缩 + System.out.println("正在解压缩target.zip..."); + ssh.exec("cd " + remotePath + " && unzip target.zip"); + ssh.exec("cd " + remotePath + " && rm -rf target.zip"); } - System.out.println("正在上传target.zip..."); - // 将目录打包成ZIP文件,不包含目录本身,只包含目录下的文件和子目录 - ZipUtil.zip(workingPath + "/target", localFile, true); - //上传 - ssh.upload(localFile, remotePath + "/target.zip"); - //删除target.zip - FileUtil.del(localFile); - //解压缩 - System.out.println("正在解压缩target.zip..."); - ssh.exec("cd " + remotePath + " && unzip target.zip"); - ssh.exec("cd " + remotePath + " && rm -rf target.zip"); - //打包 System.out.println("开始打包镜像,稍等...."); String cmd = "cd /usr/local/" + projectName + " && docker build -t " + projectName + ":" + newVersion + " ."; - ssh.exec(cmd); System.out.println(cmd); + ssh.exec(cmd); //获取最新打包后的镜像ID cmd = "docker images --format \"{{.Repository}} {{.Tag}} {{.ID}} {{.CreatedAt}}\" | grep " + projectName + " | sort -k4,5 -r | head -n1 | awk '{print $3}'"; Kv kv = ssh.exec(cmd); String imageId = kv.getStr("message").replace("[", "").replace("]", ""); System.out.println("镜像打包完成,镜像ID=" + imageId); - System.out.println("打标签..."); - cmd = "docker tag " + imageId + " registry.cn-hangzhou.aliyuncs.com/yltcharge/" + projectName + ":" + newVersion; - ssh.exec(cmd); + System.out.println("测试命令:docker run -p 80:80 -d " + projectName + ":" + newVersion); +// System.out.println("打标签..."); +// cmd = "docker tag " + imageId + " registry.cn-hangzhou.aliyuncs.com/yltcharge/" + projectName + ":" + newVersion; +// ssh.exec(cmd); // System.out.println("开始推送到远程仓库,稍等..."); // cmd = "docker push registry.cn-hangzhou.aliyuncs.com/yltcharge/" + projectName + ":" + newVersion; diff --git a/ds-build/src/main/java/buildImage.java b/ds-build/src/main/java/build_ds_gw.java similarity index 87% rename from ds-build/src/main/java/buildImage.java rename to ds-build/src/main/java/build_ds_gw.java index ae330c53..3b6b4565 100644 --- a/ds-build/src/main/java/buildImage.java +++ b/ds-build/src/main/java/build_ds_gw.java @@ -1,6 +1,6 @@ import Util.PublishUtil; -public class buildImage { +public class build_ds_gw { public static void main(String[] args) throws Exception { //项目名称 String projectName = "ds-gw"; diff --git a/ds-build/src/main/resources/publishImage.json b/ds-build/src/main/resources/publishImage.json index 06c03729..ec6aad1b 100644 --- a/ds-build/src/main/resources/publishImage.json +++ b/ds-build/src/main/resources/publishImage.json @@ -6,9 +6,10 @@ "project": [ { "projectName": "ds-gw", - "workingPath": "D:/dsWork/dsExam/ds-gw", - "remotePath": "/usr/local/ds-gw", - "localLibPath": "" + "workingPath": "D:/dsWork/dsExam/ds-gw/", + "localLibPath": "", + "localStatic": "static", + "localNginxConf": "nginx.conf" } ] } diff --git a/ds-build/target/classes/publishImage.json b/ds-build/target/classes/publishImage.json new file mode 100644 index 00000000..ec6aad1b --- /dev/null +++ b/ds-build/target/classes/publishImage.json @@ -0,0 +1,15 @@ +{ + "host": "10.10.14.212", + "user": "root", + "pwd": "dsideal", + "port": 22, + "project": [ + { + "projectName": "ds-gw", + "workingPath": "D:/dsWork/dsExam/ds-gw/", + "localLibPath": "", + "localStatic": "static", + "localNginxConf": "nginx.conf" + } + ] +}