|
|
@ -1,126 +0,0 @@
|
|
|
|
package com.dsideal.base.Util;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
|
|
|
|
|
import cn.hutool.core.util.ZipUtil;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONArray;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
|
|
|
|
|
import com.jfinal.kit.Kv;
|
|
|
|
|
|
|
|
import com.jfinal.kit.PathKit;
|
|
|
|
|
|
|
|
import com.jfinal.kit.StrKit;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class PublishUtil {
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 删除本机所有Docker镜像
|
|
|
|
|
|
|
|
* docker images --format "{{.Repository}}:{{.Tag}}" | xargs -r docker rmi -f
|
|
|
|
|
|
|
|
* <p>
|
|
|
|
|
|
|
|
* 登录远程仓库
|
|
|
|
|
|
|
|
* 方法1:
|
|
|
|
|
|
|
|
* docker login -u 驿来特充电 -p ylt5033. registry.cn-hangzhou.aliyuncs.com
|
|
|
|
|
|
|
|
* 方法2:
|
|
|
|
|
|
|
|
* docker login --username=驿来特充电 --password=ylt5033. registry.cn-hangzhou.aliyuncs.com
|
|
|
|
|
|
|
|
* <p>
|
|
|
|
|
|
|
|
* # 搜索
|
|
|
|
|
|
|
|
* docker search registry.cn-hangzhou.aliyuncs.com/yltcharge/zhu-que:20240903
|
|
|
|
|
|
|
|
* <p>
|
|
|
|
|
|
|
|
* #拉取
|
|
|
|
|
|
|
|
* docker pull registry.cn-hangzhou.aliyuncs.com/yltcharge/zhu-que:20240903
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static void publish(String projectName) throws Exception {
|
|
|
|
|
|
|
|
System.out.println("正在生成" + projectName + "的镜像...");
|
|
|
|
|
|
|
|
//配置文件
|
|
|
|
|
|
|
|
String path = PathKit.getRootClassPath() + "\\publishImage.json";
|
|
|
|
|
|
|
|
JSONObject jo = JSONObject.parseObject(FileUtil.readUtf8String(path));
|
|
|
|
|
|
|
|
//主机
|
|
|
|
|
|
|
|
String host = jo.getString("host");
|
|
|
|
|
|
|
|
String user = jo.getString("user");
|
|
|
|
|
|
|
|
String pwd = jo.getString("pwd");
|
|
|
|
|
|
|
|
int port = jo.getIntValue("port");
|
|
|
|
|
|
|
|
JSONArray ja = jo.getJSONArray("project");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String workingPath = null, remotePath = null, localLibPath = 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");
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//声明SSH对象
|
|
|
|
|
|
|
|
SSHUtil ssh = new SSHUtil(user, pwd, host, port);
|
|
|
|
|
|
|
|
ssh.connect();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 格式化日期
|
|
|
|
|
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
|
|
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
|
|
String newVersion = now.format(formatter);
|
|
|
|
|
|
|
|
newVersion = newVersion.replace(" ", "").replace("-", "").replace(":", "");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//准备工作
|
|
|
|
|
|
|
|
ssh.exec("rm -rf " + remotePath);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("正在上传Dockerfile...");
|
|
|
|
|
|
|
|
ssh.upload(workingPath + "/Dockerfile", "/usr/local/" + projectName + "/Dockerfile");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!StrKit.isBlank(localLibPath)) {
|
|
|
|
|
|
|
|
System.out.println("正在创建lib目录...");
|
|
|
|
|
|
|
|
ssh.mkdir("/usr/local/" + projectName + "/lib");
|
|
|
|
|
|
|
|
ssh.mkdir(remotePath);
|
|
|
|
|
|
|
|
//遍历lib目录下的文件
|
|
|
|
|
|
|
|
for (File file : FileUtil.loopFiles(localLibPath)) {
|
|
|
|
|
|
|
|
System.out.println("正在上传jar包:" + file.getName());
|
|
|
|
|
|
|
|
ssh.upload(file.getAbsolutePath(), remotePath + "/lib/" + file.getName());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//打包target为zip
|
|
|
|
|
|
|
|
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("开始打包镜像,稍等....");
|
|
|
|
|
|
|
|
String cmd = "cd /usr/local/" + projectName + " && docker build -t " + projectName + ":" + newVersion + " .";
|
|
|
|
|
|
|
|
ssh.exec(cmd);
|
|
|
|
|
|
|
|
System.out.println(cmd);
|
|
|
|
|
|
|
|
//获取最新打包后的镜像ID
|
|
|
|
|
|
|
|
cmd = "docker images | grep " + projectName + " | awk '{print $3}' | sort -r | head -n 1";
|
|
|
|
|
|
|
|
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("开始推送到远程仓库,稍等...");
|
|
|
|
|
|
|
|
cmd = "docker push registry.cn-hangzhou.aliyuncs.com/yltcharge/" + projectName + ":" + newVersion;
|
|
|
|
|
|
|
|
ssh.exec(cmd);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("推送到远程仓库完成!");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ssh.disconnect();
|
|
|
|
|
|
|
|
System.out.println("恭喜,镜像打包成功并完成上传到仓库操作!");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|