|
|
@@ -8,7 +8,9 @@ import com.github.dockerjava.api.DockerClient;
|
|
|
import com.github.dockerjava.api.async.ResultCallback;
|
|
|
import com.github.dockerjava.api.command.BuildImageCmd;
|
|
|
import com.github.dockerjava.api.command.CreateContainerResponse;
|
|
|
+import com.github.dockerjava.api.command.PullImageResultCallback;
|
|
|
import com.github.dockerjava.api.command.WaitContainerResultCallback;
|
|
|
+import com.github.dockerjava.api.exception.NotFoundException;
|
|
|
import com.github.dockerjava.api.model.*;
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
@@ -71,7 +73,7 @@ public class DockerBuild {
|
|
|
.withMemory(1024 * 1024 * 1024L)
|
|
|
.withBaseDirectory(new File(sourceCodeDir)) // 上下文路径
|
|
|
.withDockerfile(new File(sourceCodeDir, "Dockerfile"))
|
|
|
- .withPull(true)
|
|
|
+ .withPull(true) // 镜像不存在则拉取
|
|
|
.withTags(Collections.singleton(repoTag));
|
|
|
|
|
|
for (KeyValue keyValue : keyValueList) {
|
|
|
@@ -105,6 +107,22 @@ public class DockerBuild {
|
|
|
|
|
|
public void build(PipelineContext ctx, String repoTag, String sourceCodeDir,
|
|
|
String image, String compileCmd, List<CompilerBind> compilerBinds, List<String> envs) {
|
|
|
+ try {
|
|
|
+ // 1. 尝试查看镜像信息
|
|
|
+ dockerClient.inspectImageCmd(image).exec();
|
|
|
+ } catch (NotFoundException e) {
|
|
|
+ // 2. 如果镜像不存在,执行拉取
|
|
|
+ ctx.log("镜像 {} 不存在,正在从仓库拉取..." + image);
|
|
|
+ try {
|
|
|
+ dockerClient.pullImageCmd(image)
|
|
|
+ .exec(new PullImageResultCallback())
|
|
|
+ .awaitCompletion(); // 这是一个阻塞操作,确保拉取完成后再继续
|
|
|
+ ctx.log("镜像 {} 拉取完成" + image);
|
|
|
+ } catch (InterruptedException ex) {
|
|
|
+ throw new RuntimeException(ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 这里的 workDir 会在 docker build 使用的 Dockerfile 中被引用
|
|
|
String workDir = "/app";
|
|
|
List<Bind> bindList = new ArrayList<>();
|