main
黄海 11 months ago
parent 1a030b3cf8
commit d9cc9d5dc1

@ -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;

@ -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";

@ -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"
}
]
}

@ -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"
}
]
}
Loading…
Cancel
Save