Skip to content

Commit 6c814c7

Browse files
nrktktlefou
andauthored
Use docker image hash to determine if build should rerun (#3124)
Previously, the docker build would re-run if the user had `pullBaseImage` set to true. Now it always checks the remote repository for a new version of the base image, but only re-runs the build if the base image hash has changed. Pull request: #3124 --------- Co-authored-by: Tobias Roeser <[email protected]>
1 parent 471238a commit 6c814c7

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

contrib/docker/src/mill/contrib/docker/DockerModule.scala

+13-6
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ trait DockerModule { outer: JavaModule =>
8989
*/
9090
def executable: T[String] = "docker"
9191

92-
private def baseImageCacheBuster: T[(Boolean, Double)] = T.input {
93-
val pull = pullBaseImage()
94-
if (pull) (pull, Math.random()) else (pull, 0d)
95-
}
96-
9792
def dockerfile: T[String] = T {
9893
val jarName = assembly().path.last
9994
val labelRhs = labels()
@@ -133,6 +128,18 @@ trait DockerModule { outer: JavaModule =>
133128
|ENTRYPOINT [$quotedEntryPointArgs]""".stripMargin
134129
}
135130

131+
private def pullAndHash = T.input {
132+
def imageHash() =
133+
os.proc(executable(), "images", "--no-trunc", "--quiet", baseImage())
134+
.call(stderr = os.Inherit).out.text().trim
135+
136+
if (pullBaseImage() || imageHash().isEmpty)
137+
os.proc(executable(), "image", "pull", baseImage())
138+
.call(stdout = os.Inherit, stderr = os.Inherit)
139+
140+
(pullBaseImage(), imageHash())
141+
}
142+
136143
final def build = T {
137144
val dest = T.dest
138145

@@ -145,7 +152,7 @@ trait DockerModule { outer: JavaModule =>
145152

146153
val tagArgs = tags().flatMap(t => List("-t", t))
147154

148-
val (pull, _) = baseImageCacheBuster()
155+
val (pull, _) = pullAndHash()
149156
val pullLatestBase = IterableShellable(if (pull) Some("--pull") else None)
150157

151158
val result = os

0 commit comments

Comments
 (0)