Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dockerEnv target to customize environment passed to docker command #3257

Merged
merged 3 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading