Skip to content

Commit

Permalink
Add dockerEnv target to customize environment passed to docker c…
Browse files Browse the repository at this point in the history
…ommand (#3257)

Fixes #3132

Pull Request: #3257

---------

Co-authored-by: Li Haoyi <haoyi.sg@gmail.com>
  • Loading branch information
lolgab and lihaoyi authored Jan 20, 2025
1 parent 75e286c commit f4fbabf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
23 changes: 18 additions & 5 deletions contrib/docker/src/mill/contrib/docker/DockerModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ trait DockerModule { outer: JavaModule =>
*/
def envVars: T[Map[String, String]] = Map.empty[String, String]

/**
* Environment to pass to the docker commands.
* Example: DOCKER_DEFAULT_PLATFORM=linux/amd64
*
* See also the Docker docs on
* [[https://docs.docker.com/engine/reference/commandline/cli/#environment-variables Environment variables]]
* for more information.
*/
def dockerEnv: T[Map[String, String]] = T.env

/**
* Commands to add as RUN instructions.
*
Expand Down Expand Up @@ -138,19 +148,21 @@ trait DockerModule { outer: JavaModule =>
}

private def pullAndHash = Task.Input {
val env = dockerEnv()
def imageHash() =
os.proc(executable(), "images", "--no-trunc", "--quiet", baseImage())
.call(stderr = os.Inherit).out.text().trim
.call(stderr = os.Inherit, env = env).out.text().trim

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

(pullBaseImage(), imageHash())
}

final def build = Task {
val dest = T.dest
val env = dockerEnv()

val asmPath = outer.assembly().path
os.copy(asmPath, dest / asmPath.last)
Expand All @@ -168,7 +180,7 @@ trait DockerModule { outer: JavaModule =>
if (platform().nonEmpty)
log.info("Platform parameter is ignored when using non-docker executable")
os.proc(executable(), "build", tagArgs, pullLatestBase, dest)
.call(stdout = os.Inherit, stderr = os.Inherit)
.call(stdout = os.Inherit, stderr = os.Inherit, env = env)
} else {
os.proc(
executable(),
Expand All @@ -180,7 +192,7 @@ trait DockerModule { outer: JavaModule =>
platform(),
dest
)
.call(stdout = os.Inherit, stderr = os.Inherit)
.call(stdout = os.Inherit, stderr = os.Inherit, env = env)
}
log.info(s"Docker build completed ${if (result.exitCode == 0) "successfully"
else "unsuccessfully"} with ${result.exitCode}")
Expand All @@ -189,8 +201,9 @@ trait DockerModule { outer: JavaModule =>

final def push() = Task.Command {
val tags = build()
val env = dockerEnv()
tags.foreach(t =>
os.proc(executable(), "push", t).call(stdout = os.Inherit, stderr = os.Inherit)
os.proc(executable(), "push", t).call(stdout = os.Inherit, stderr = os.Inherit, env = env)
)
}
}
Expand Down
15 changes: 15 additions & 0 deletions contrib/docker/test/src/mill/contrib/docker/DockerModuleTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package mill
package contrib.docker

import mill.scalalib.JavaModule
import mill.api.Result
import mill.util.TestUtil
import mill.testkit.UnitTester
import mill.testkit.TestBaseModule
import os.Path
Expand Down Expand Up @@ -41,6 +43,10 @@ object DockerModuleTest extends TestSuite {
override def executable = testExecutable
override def jvmOptions = Seq("-Xmx1024M")
}

object dockerEnv extends DockerConfig {
override def dockerEnv = Map("DOCKER_HOST" -> "wrong_host")
}
}

val testArtifactName = "mill-docker-contrib-test"
Expand Down Expand Up @@ -86,6 +92,15 @@ object DockerModuleTest extends TestSuite {
val Right(result) = eval(Docker.dockerAll.build)
assert(result.value == List(testArtifactName))
}

"dockerEnv" - workspaceTest(Docker) { eval =>
// since stdout and stderr are inherited we can only test
// that docker fails with wrong DOCKER_HOST
val Left(Result.Exception(error: os.SubprocessException, _)) =
eval(Docker.dockerEnv.build)
val message = error.getMessage
assert(message == "Result of docker…: 1\n")
}
}

test("dockerfile contents") {
Expand Down

0 comments on commit f4fbabf

Please sign in to comment.