|
|
|
@ -17,12 +17,17 @@ public class PublishUtil {
|
|
|
|
|
* 删除本机所有Docker镜像
|
|
|
|
|
* docker images --format "{{.Repository}}:{{.Tag}}" | xargs -r docker rmi -f
|
|
|
|
|
* 登录远程仓库
|
|
|
|
|
* docker login --username=东师黄海 registry.cn-hangzhou.aliyuncs.com --password DsideaL4r5t6y7u
|
|
|
|
|
|
|
|
|
|
docker login --username=东师黄海 registry.cn-hangzhou.aliyuncs.com --password DsideaL4r5t6y7u
|
|
|
|
|
docker login --username=驿来特充电 registry.cn-hangzhou.aliyuncs.com --password=ylt5033.
|
|
|
|
|
* <p>
|
|
|
|
|
* # 搜索
|
|
|
|
|
* docker search registry.cn-hangzhou.aliyuncs.com/yltcharge/zhu-que:20240903
|
|
|
|
|
* #拉取
|
|
|
|
|
* docker pull registry.cn-hangzhou.aliyuncs.com/yltcharge/zhu-que:20240903
|
|
|
|
|
*
|
|
|
|
|
docker pull registry.cn-hangzhou.aliyuncs.com/dsideal/jdk:21
|
|
|
|
|
docker tag 19a54d2204aa registry.cn-hangzhou.aliyuncs.com/yltcharge/jdk:21
|
|
|
|
|
docker push registry.cn-hangzhou.aliyuncs.com/yltcharge/jdk:21
|
|
|
|
|
*/
|
|
|
|
|
public static void publish(String projectName) throws Exception {
|
|
|
|
|
System.out.println("正在生成" + projectName + "的镜像...");
|
|
|
|
@ -37,6 +42,7 @@ public class PublishUtil {
|
|
|
|
|
JSONArray ja = jo.getJSONArray("project");
|
|
|
|
|
|
|
|
|
|
String workingPath = null, remotePath, localLibPath = null;
|
|
|
|
|
boolean isStatic = false;
|
|
|
|
|
|
|
|
|
|
for (Object o : ja) {
|
|
|
|
|
JSONObject project = (JSONObject) o;
|
|
|
|
@ -44,6 +50,9 @@ public class PublishUtil {
|
|
|
|
|
if (p.equals(projectName)) {
|
|
|
|
|
workingPath = project.getString("workingPath");
|
|
|
|
|
localLibPath = project.getString("localLibPath");
|
|
|
|
|
if (project.getBoolean("isStatic")) {
|
|
|
|
|
isStatic = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -59,13 +68,14 @@ public class PublishUtil {
|
|
|
|
|
newVersion = newVersion.replace(" ", "").replace("-", "").replace(":", "");
|
|
|
|
|
|
|
|
|
|
//准备工作
|
|
|
|
|
remotePath = "/usr/local/" + projectName+"/";
|
|
|
|
|
remotePath = "/usr/local/" + projectName + "/";
|
|
|
|
|
ssh.exec("rm -rf " + remotePath);
|
|
|
|
|
ssh.mkdir(remotePath);
|
|
|
|
|
|
|
|
|
|
System.out.println("正在上传Dockerfile...");
|
|
|
|
|
ssh.upload(workingPath + "Dockerfile", remotePath + "Dockerfile");
|
|
|
|
|
|
|
|
|
|
if (!isStatic) {
|
|
|
|
|
System.out.println("正在上传Dockerfile...");
|
|
|
|
|
ssh.upload(workingPath + "Dockerfile", remotePath + "Dockerfile");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!StrKit.isBlank(localLibPath)) {
|
|
|
|
|
System.out.println("正在创建lib目录...");
|
|
|
|
@ -96,29 +106,65 @@ public class PublishUtil {
|
|
|
|
|
ssh.exec("cd " + remotePath + " && rm -rf target.zip");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//处理静态文件
|
|
|
|
|
if (isStatic) {
|
|
|
|
|
//倒数第二个/
|
|
|
|
|
int cnt = 0;
|
|
|
|
|
int pos = -1;
|
|
|
|
|
for (int i = workingPath.length() - 1; ; i--) {
|
|
|
|
|
if (workingPath.charAt(i) == '/') cnt++;
|
|
|
|
|
if (cnt == 2) {
|
|
|
|
|
pos = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
String localFile = workingPath.substring(0, pos) + "/static.zip";
|
|
|
|
|
if (FileUtil.exist(localFile)) {
|
|
|
|
|
FileUtil.del(localFile);
|
|
|
|
|
}
|
|
|
|
|
System.out.println("正在上传static.zip...");
|
|
|
|
|
// 将目录打包成ZIP文件,不包含目录本身,只包含目录下的文件和子目录
|
|
|
|
|
ZipUtil.zip(workingPath, localFile, false);
|
|
|
|
|
//上传
|
|
|
|
|
ssh.upload(localFile, remotePath + "static.zip");
|
|
|
|
|
//删除target.zip
|
|
|
|
|
FileUtil.del(localFile);
|
|
|
|
|
//解压缩
|
|
|
|
|
System.out.println("正在解压缩static.zip...");
|
|
|
|
|
ssh.exec("cd " + remotePath + " && unzip static.zip");
|
|
|
|
|
ssh.exec("cd " + remotePath + " && rm -rf static.zip");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//打包
|
|
|
|
|
System.out.println("开始打包镜像,稍等....");
|
|
|
|
|
String cmd = "cd /usr/local/" + projectName + " && docker build -t " + projectName + ":" + newVersion + " .";
|
|
|
|
|
System.out.println(cmd);
|
|
|
|
|
ssh.exec(cmd);
|
|
|
|
|
|
|
|
|
|
//删除所有镜像
|
|
|
|
|
cmd="docker rmi $(docker images -q)";
|
|
|
|
|
ssh.exec(cmd);
|
|
|
|
|
|
|
|
|
|
//获取最新打包后的镜像ID
|
|
|
|
|
cmd = "docker images --format \"{{.Repository}} {{.Tag}} {{.ID}} {{.CreatedAt}}\" | grep " + projectName + " | sort -k4,5 -r | head -n1 | awk '{print $3}'";
|
|
|
|
|
cmd = "docker images --format \"{{.Repository}} {{.Tag}} {{.ID}} {{.CreatedAt}}\" | grep "+projectName+" | head -n1 | awk '{print $3}'";
|
|
|
|
|
Kv kv = ssh.exec(cmd);
|
|
|
|
|
String imageId = kv.getStr("message").replace("[", "").replace("]", "");
|
|
|
|
|
System.out.println("镜像打包完成,镜像ID=" + imageId);
|
|
|
|
|
|
|
|
|
|
//登录镜像仓库
|
|
|
|
|
cmd = "docker login --username=东师黄海 --password=DsideaL4r5t6y7u registry.cn-hangzhou.aliyuncs.com";
|
|
|
|
|
cmd = "docker login --username=驿来特充电 --password=ylt5033. registry.cn-hangzhou.aliyuncs.com";
|
|
|
|
|
System.out.println(cmd);
|
|
|
|
|
ssh.exec(cmd);
|
|
|
|
|
System.out.println("仓库登录成功!");
|
|
|
|
|
|
|
|
|
|
System.out.println("打标签...");
|
|
|
|
|
cmd = "docker tag " + imageId + " registry.cn-hangzhou.aliyuncs.com/dsideal/" + projectName + ":" + newVersion;
|
|
|
|
|
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/dsideal/" + projectName + ":" + newVersion;
|
|
|
|
|
cmd = "docker push registry.cn-hangzhou.aliyuncs.com/yltcharge/" + projectName + ":" + newVersion;
|
|
|
|
|
System.out.println(cmd);
|
|
|
|
|
ssh.exec(cmd);
|
|
|
|
|
|
|
|
|
|
System.out.println("推送到远程仓库完成!");
|
|
|
|
|