kgdxpr 10 months ago
commit d7407ea60a

@ -1,4 +1,4 @@
FROM registry.cn-hangzhou.aliyuncs.com/dsideal/jdk:21
FROM registry.cn-hangzhou.aliyuncs.com/yltcharge/jdk:21
# WORKDIR指令用于设置容器内部的工作目录即后续指令执行时的当前目录。当Docker容器启动并执行命令时这些命令将在WORKDIR指定的目录中执行。
WORKDIR /root

@ -1,4 +1,4 @@
FROM nginx:alpine
FROM swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:alpine
RUN rm /etc/nginx/conf.d/default.conf
RUN rm /etc/nginx/nginx.conf
COPY nginx.conf /etc/nginx/

@ -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("推送到远程仓库完成!");

@ -0,0 +1,9 @@
import Util.PublishUtil;
public class build_ds_base_web {
public static void main(String[] args) throws Exception {
//项目名称
String projectName = "ds-base-web";
PublishUtil.publish(projectName);
}
}

@ -7,12 +7,20 @@
{
"projectName": "ds-gw",
"workingPath": "D:/dsWork/dsProject/dsGw/",
"localLibPath": "D:/dsWork/dsProject/dsGw/lib/"
"localLibPath": "D:/dsWork/dsProject/dsGw/lib/",
"isStatic": false
},
{
"projectName": "ds-base",
"workingPath": "D:/dsWork/dsProject/dsBase/",
"localLibPath": "D:/dsWork/dsProject/dsBase/lib/"
"localLibPath": "D:/dsWork/dsProject/dsBase/lib/",
"isStatic": false
},
{
"projectName": "ds-base-web",
"workingPath": "D:/dsWork/dsProject/dsBaseWeb/",
"localLibPath": "",
"isStatic": true
}
]
}

@ -7,12 +7,20 @@
{
"projectName": "ds-gw",
"workingPath": "D:/dsWork/dsProject/dsGw/",
"localLibPath": "D:/dsWork/dsProject/dsGw/lib/"
"localLibPath": "D:/dsWork/dsProject/dsGw/lib/",
"isStatic": false
},
{
"projectName": "ds-base",
"workingPath": "D:/dsWork/dsProject/dsBase/",
"localLibPath": "D:/dsWork/dsProject/dsBase/lib/"
"localLibPath": "D:/dsWork/dsProject/dsBase/lib/",
"isStatic": false
},
{
"projectName": "ds-base-web",
"workingPath": "D:/dsWork/dsProject/dsBaseWeb/",
"localLibPath": "",
"isStatic": true
}
]
}

Loading…
Cancel
Save