main
黄海 10 months ago
parent 0dc19b03cd
commit 466b49b6f8

@ -4,6 +4,7 @@ import java.util.List;
import Util.*;
import cn.hutool.core.io.FileUtil;
import com.jfinal.kit.Kv;
public class Publish {
@ -28,16 +29,16 @@ public class Publish {
//让用户选择要发布的项目
int choiceProject = PublishUtil.userSelectProject(listProject);
//让用户选择要发布的仓库
int choiceWarehouse = PublishUtil.userSelectWarehouse();
Kv choiceWarehouse = PublishUtil.userSelectWarehouse();
//这个项目是什么类型?
int projectAttribute = PublishUtil.getProjectAttribute(choiceProject, listProject);
String projectAttribute = PublishUtil.getProjectAttribute(choiceProject, listProject);
String workingPath = null, projectName = null, localLibPath = null;
boolean isStatic = false;
for (ProjectBean projectBean : listProject) {
if (projectBean.getId() == choiceProject) {
String basedir = new File(System.getProperty("user.dir")).getCanonicalPath();
workingPath = basedir + "\\" + projectBean.getDevProjectName()+"\\";
workingPath = basedir + "\\" + projectBean.getDevProjectName() + "\\";
projectName = projectBean.getPublishProjectName();
if (FileUtil.exist(workingPath + "\\lib")) {
localLibPath = workingPath + "\\lib\\";
@ -45,10 +46,10 @@ public class Publish {
break;
}
}
if (projectAttribute == 2){
if (projectAttribute != null && projectAttribute.equals("WEB")) {
isStatic = true;
}
//开始打包
PublishUtil.publish(projectName, isStatic, workingPath, localLibPath,choiceWarehouse);
PublishUtil.publish(projectName, isStatic, workingPath, localLibPath, choiceWarehouse);
}
}

@ -16,6 +16,12 @@ import java.util.List;
import java.util.Scanner;
public class PublishUtil {
public static void doLogin(SSHUtil ssh, String username, String password) throws Exception {
String cmd = "docker login --username=" + username + " --password=" + password + " registry.cn-hangzhou.aliyuncs.com";
ssh.exec(cmd);
}
/**
* Docker
* docker images --format "{{.Repository}}:{{.Tag}}" | xargs -r docker rmi -f
@ -32,7 +38,7 @@ public class PublishUtil {
* 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, boolean isStatic, String workingPath, String localLibPath, int choiceWarehouse) throws Exception {
public static void publish(String projectName, boolean isStatic, String workingPath, String localLibPath, Kv choiceWarehouse) throws Exception {
System.out.println("正在生成" + projectName + "的镜像...");
//配置文件
String path = PathKit.getRootClassPath() + "\\publishImage.json";
@ -96,7 +102,7 @@ public class PublishUtil {
if (isStatic) {
//倒数第二个/
int cnt = 0;
int pos = -1;
int pos;
for (int i = workingPath.length() - 1; ; i--) {
if (workingPath.charAt(i) == '/') cnt++;
if (cnt == 2) {
@ -120,53 +126,45 @@ public class PublishUtil {
ssh.exec("cd " + remotePath + " && unzip static.zip");
ssh.exec("cd " + remotePath + " && rm -rf static.zip");
}
//登录镜像仓库
//处理一下Dockerfile中的JDK21此用问题因为不同的打包操作需要登录不同的仓库这里临时用shell修改一下
String remoteDockerFile = remotePath + "Dockerfile";
doLogin(ssh, choiceWarehouse.getStr("username"), choiceWarehouse.getStr("password"));
String cmd = "sed -i '1s/^FROM .*/FROM registry.cn-hangzhou.aliyuncs.com\\/" + choiceWarehouse.getStr("name") + "\\/jdk:21/' " + remoteDockerFile;
ssh.exec(cmd);
System.out.println("仓库登录成功修改JDK21的引用镜像成功!");
//删除所有镜像
cmd = "docker rmi -f $(docker images -q)";
ssh.exec(cmd);
//打包
System.out.println("开始打包镜像,稍等....");
String cmd = "cd /usr/local/" + projectName + " && docker build -t " + projectName + ":" + newVersion + " .";
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 + " | head -n1 | awk '{print $3}'";
Kv kv = ssh.exec(cmd);
String imageId = kv.getStr("message").replace("[", "").replace("]", "");
System.out.println("镜像打包完成镜像ID=" + imageId);
//登录镜像仓库
if (choiceWarehouse == 1) {
cmd = "docker login --username=驿来特充电 --password=ylt5033. registry.cn-hangzhou.aliyuncs.com";
} else {
cmd = "docker login --username=东师黄海 --password=DsideaL4r5t6y7u registry.cn-hangzhou.aliyuncs.com";
if (StrKit.isBlank(imageId)) {
System.out.println("镜像ID生成异常请检查后再试");
System.exit(0);
}
System.out.println(cmd);
ssh.exec(cmd);
System.out.println("仓库登录成功!");
System.out.println("打标签...");
if (choiceWarehouse == 1) {
cmd = "docker tag " + imageId + " registry.cn-hangzhou.aliyuncs.com/yltcharge/" + projectName + ":" + newVersion;
} else {
cmd = "docker tag " + imageId + " registry.cn-hangzhou.aliyuncs.com/dsideal/" + projectName + ":" + newVersion;
}
cmd = "docker tag " + imageId + " registry.cn-hangzhou.aliyuncs.com/" + choiceWarehouse.getStr("name") + "/" + projectName + ":" + newVersion;
ssh.exec(cmd);
System.out.println("开始推送到远程仓库,稍等...");
if (choiceWarehouse == 1) {
cmd = "docker push registry.cn-hangzhou.aliyuncs.com/yltcharge/" + projectName + ":" + newVersion;
} else {
cmd = "docker push registry.cn-hangzhou.aliyuncs.com/dsideal/" + projectName + ":" + newVersion;
}
cmd = "docker push registry.cn-hangzhou.aliyuncs.com/" + choiceWarehouse.getStr("name") + "/" + projectName + ":" + newVersion;
System.out.println(cmd);
ssh.exec(cmd);
System.out.println("推送到远程仓库完成!");
ssh.disconnect();
@ -228,20 +226,20 @@ public class PublishUtil {
*
* @return
*/
public static int getProjectAttribute(int id, List<ProjectBean> listProject) throws IOException {
public static String getProjectAttribute(int id, List<ProjectBean> listProject) throws IOException {
for (ProjectBean projectBean : listProject) {
if (projectBean.getId() == id) {
String basedir = new File(System.getProperty("user.dir")).getCanonicalPath();
String path = basedir + "\\" + projectBean.getDevProjectName();
//这个path目录下有没有src这样的子目录如果有则判定为JAVA项目否则为WEB项目
if (new File(path + "\\src").exists()) {
return 1;
return "JAVA";
} else {
return 2;
return "WEB";
}
}
}
return 0;
return null;
}
/**
@ -271,19 +269,28 @@ public class PublishUtil {
*
* @return
*/
public static int userSelectWarehouse() {
public static Kv userSelectWarehouse() {
Scanner scanner = new Scanner(System.in); // 创建Scanner对象来读取控制台输入
System.out.println("请选择您本次要发布的仓库:");
System.out.println("1驿来特仓库 2:黄海私人仓库");
int choice = scanner.nextInt(); // 读取用户输入的整数
//输出choice对应的项目名称
Kv kv = Kv.create();
if (choice == 1) {
kv.set("id", 1);
kv.set("name", "yltcharge");
kv.set("username", "驿来特充电");
kv.set("password", "ylt5033.");
System.out.println("您选择的仓库是:驿来特仓库");
} else {
kv.set("id", 2);
kv.set("name", "dsideal");
kv.set("username", "东师黄海");
kv.set("password", "DsideaL4r5t6y7u");
System.out.println("您选择的仓库是:黄海私人仓库");
}
scanner.close(); // 关闭Scanner对象
return choice;
return kv;
}
}

Loading…
Cancel
Save