Skip to content

Commit

Permalink
Use docker image hash to determine if build should rerun (#3124)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
nrktkt and lefou authored Apr 23, 2024
1 parent 471238a commit 6c814c7
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions contrib/docker/src/mill/contrib/docker/DockerModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ trait DockerModule { outer: JavaModule =>
*/
def executable: T[String] = "docker"

private def baseImageCacheBuster: T[(Boolean, Double)] = T.input {
val pull = pullBaseImage()
if (pull) (pull, Math.random()) else (pull, 0d)
}

def dockerfile: T[String] = T {
val jarName = assembly().path.last
val labelRhs = labels()
Expand Down Expand Up @@ -133,6 +128,18 @@ trait DockerModule { outer: JavaModule =>
|ENTRYPOINT [$quotedEntryPointArgs]""".stripMargin
}

private def pullAndHash = T.input {
def imageHash() =
os.proc(executable(), "images", "--no-trunc", "--quiet", baseImage())
.call(stderr = os.Inherit).out.text().trim

if (pullBaseImage() || imageHash().isEmpty)
os.proc(executable(), "image", "pull", baseImage())
.call(stdout = os.Inherit, stderr = os.Inherit)

(pullBaseImage(), imageHash())
}

final def build = T {
val dest = T.dest

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

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

val (pull, _) = baseImageCacheBuster()
val (pull, _) = pullAndHash()
val pullLatestBase = IterableShellable(if (pull) Some("--pull") else None)

val result = os
Expand Down

0 comments on commit 6c814c7

Please sign in to comment.