You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
7.0 KiB

11 months ago
package 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
*
10 months ago
* docker login --username= registry.cn-hangzhou.aliyuncs.com --password DsideaL4r5t6y7u
11 months ago
* #
* docker search registry.cn-hangzhou.aliyuncs.com/yltcharge/zhu-que:20240903
* #
* 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");
11 months ago
String workingPath = null, remotePath, localLibPath = null, localStatic = null, localNginxConf = null;
11 months ago
for (Object o : ja) {
JSONObject project = (JSONObject) o;
String p = project.getString("projectName");
if (p.equals(projectName)) {
workingPath = project.getString("workingPath");
localLibPath = project.getString("localLibPath");
11 months ago
if (project.getString("localStatic") != null) {
10 months ago
localStatic = project.getString("localStatic");
11 months ago
}
if (project.getString("localNginxConf") != null) {
10 months ago
localNginxConf = project.getString("localNginxConf");
11 months ago
}
11 months ago
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(":", "");
//准备工作
10 months ago
remotePath = "/usr/local/" + projectName+"/";
11 months ago
ssh.exec("rm -rf " + remotePath);
11 months ago
ssh.mkdir(remotePath);
11 months ago
System.out.println("正在上传Dockerfile...");
11 months ago
ssh.upload(workingPath + "/Dockerfile", remotePath + "/Dockerfile");
11 months ago
if (!StrKit.isBlank(localLibPath)) {
System.out.println("正在创建lib目录...");
11 months ago
ssh.mkdir(remotePath + "/lib");
11 months ago
//遍历lib目录下的文件
for (File file : FileUtil.loopFiles(localLibPath)) {
System.out.println("正在上传jar包" + file.getName());
ssh.upload(file.getAbsolutePath(), remotePath + "/lib/" + file.getName());
}
}
11 months ago
if (!StrKit.isBlank(localStatic)) {
10 months ago
System.out.println("正在创建" + localStatic + "目录...");
ssh.mkdir(remotePath + "/" + localStatic);
//1、需要把目录压缩成ZIP文件
// 将目录打包成ZIP文件不包含目录本身只包含目录下的文件和子目录
String localFile = workingPath + localStatic + ".zip";
System.out.println("正在进行静态文件的打包ZIP操作请稍等...");
ZipUtil.zip(workingPath + localStatic, localFile, true);
System.out.println("静态文件打包ZIP已完成正在上传...");
//2、需要把ZIP文件上传
ssh.upload(localFile, remotePath + "/" + localStatic + ".zip");
//3、把ZIP文件在远端解压缩
ssh.exec("cd " + remotePath + " && unzip " + localStatic + ".zip");
ssh.exec("cd " + remotePath + " && rm -rf " + localStatic + ".zip");
//4、删除target.zip
FileUtil.del(localFile);
11 months ago
}
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");
}
11 months ago
//打包target为zip
10 months ago
if (FileUtil.isDirectory(workingPath + "target")) {
String localFile = workingPath + "target.zip";
11 months ago
if (FileUtil.exist(localFile)) {
FileUtil.del(localFile);
}
System.out.println("正在上传target.zip...");
// 将目录打包成ZIP文件不包含目录本身只包含目录下的文件和子目录
10 months ago
ZipUtil.zip(workingPath + "target", localFile, true);
11 months ago
//上传
10 months ago
ssh.upload(localFile, remotePath + "target.zip");
11 months ago
//删除target.zip
11 months ago
FileUtil.del(localFile);
11 months ago
//解压缩
System.out.println("正在解压缩target.zip...");
ssh.exec("cd " + remotePath + " && unzip target.zip");
ssh.exec("cd " + remotePath + " && rm -rf target.zip");
11 months ago
}
//打包
System.out.println("开始打包镜像,稍等....");
String cmd = "cd /usr/local/" + projectName + " && docker build -t " + projectName + ":" + newVersion + " .";
System.out.println(cmd);
11 months ago
ssh.exec(cmd);
10 months ago
11 months ago
//获取最新打包后的镜像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);
10 months ago
//登录镜像仓库
10 months ago
cmd = "docker login --username=东师黄海 --password=DsideaL4r5t6y7u registry.cn-hangzhou.aliyuncs.com";
10 months ago
ssh.exec(cmd);
10 months ago
System.out.println("仓库登录成功!");
System.out.println("打标签...");
10 months ago
cmd = "docker tag " + imageId + " registry.cn-hangzhou.aliyuncs.com/dsideal/" + projectName + ":" + newVersion;
10 months ago
ssh.exec(cmd);
11 months ago
10 months ago
System.out.println("开始推送到远程仓库,稍等...");
cmd = "docker push registry.cn-hangzhou.aliyuncs.com/dsideal/" + projectName + ":" + newVersion;
ssh.exec(cmd);
System.out.println("推送到远程仓库完成!");
11 months ago
ssh.disconnect();
System.out.println("恭喜,镜像打包成功!");
}
}