Skip to content

Commit

Permalink
Add dockerBuildEnvVars setting, fix docker test failure in CI (#1594)
Browse files Browse the repository at this point in the history
* add dockerBuildEnvVars option

* fix docker test failure in CI

* add mima excludes
  • Loading branch information
dwickern authored Apr 10, 2024
1 parent 9e009b0 commit 37fcec3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/main/mima-filters/1.3.15.backward.excludes
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,6 @@ ProblemFilters.exclude[DirectMissingMethodProblem]("com.typesafe.sbt.packager.do
ProblemFilters.exclude[ReversedMissingMethodProblem]("com.typesafe.sbt.packager.linux.LinuxKeys.com$typesafe$sbt$packager$linux$LinuxKeys$_setter_$daemonHome_=")
ProblemFilters.exclude[ReversedMissingMethodProblem]("com.typesafe.sbt.packager.linux.LinuxKeys.daemonHome")
ProblemFilters.exclude[DirectMissingMethodProblem]("com.typesafe.sbt.packager.linux.LinuxPlugin.makeReplacements")

ProblemFilters.exclude[ReversedMissingMethodProblem]("com.typesafe.sbt.packager.docker.DockerKeys.dockerBuildEnvVars")
ProblemFilters.exclude[ReversedMissingMethodProblem]("com.typesafe.sbt.packager.docker.DockerKeys.com$typesafe$sbt$packager$docker$DockerKeys$_setter_$dockerBuildEnvVars_=")
25 changes: 18 additions & 7 deletions src/main/scala/com/typesafe/sbt/packager/docker/DockerPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ object DockerPlugin extends AutoPlugin {
dockerBuildOptions := Seq("--force-rm") ++ dockerAliases.value.flatMap { alias =>
Seq("-t", alias.toString)
} ++ { if (dockerBuildInit.value) List("--init") else Nil },
dockerBuildEnvVars := Map.empty,
dockerBuildkitEnabled := {
dockerBuildEnvVars.value
.get("DOCKER_BUILDKIT")
.orElse(sys.env.get("DOCKER_BUILDKIT"))
.map(_ == "1")
.getOrElse {
// BuildKit is the default since v23.0
dockerVersion.value.fold(false)(_.major >= 23)
}
},
dockerRmiCommand := dockerExecCommand.value ++ Seq("rmi"),
dockerBuildCommand := dockerExecCommand.value ++ Seq("build") ++ dockerBuildOptions.value ++ Seq("."),
dockerAdditionalPermissions := {
Expand Down Expand Up @@ -262,8 +273,10 @@ object DockerPlugin extends AutoPlugin {
stage.value,
dockerBuildCommand.value,
dockerExecCommand.value,
dockerBuildEnvVars.value,
dockerPermissionStrategy.value,
dockerAutoremoveMultiStageIntermediateImages.value,
dockerBuildkitEnabled.value,
log
)
log.info(
Expand Down Expand Up @@ -645,21 +658,19 @@ object DockerPlugin extends AutoPlugin {
context: File,
buildCommand: Seq[String],
execCommand: Seq[String],
envVars: Map[String, String],
strategy: DockerPermissionStrategy,
removeIntermediateImages: Boolean,
buildkitEnabled: Boolean,
log: Logger
): Unit = {
log.debug("Executing Native " + buildCommand.mkString(" "))
log.debug("Working directory " + context.toString)

val logger = sys.env
.get("DOCKER_BUILDKIT")
.filter(_ == "1")
.map(_ => publishLocalBuildkitLogger(log))
.getOrElse(publishLocalLogger(log))
val ret = sys.process.Process(buildCommand, context) ! logger
val logger = if (buildkitEnabled) publishLocalBuildkitLogger(log) else publishLocalLogger(log)
val ret = sys.process.Process(buildCommand, context, envVars.toSeq: _*) ! logger

if (removeIntermediateImages) {
if (!buildkitEnabled && removeIntermediateImages) {
val labelKey = "snp-multi-stage-id"
val labelCmd = s"LABEL ${labelKey}="
strategy match {
Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/com/typesafe/sbt/packager/docker/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ trait DockerKeys {
val dockerExecCommand = SettingKey[Seq[String]]("dockerExecCommand", "The shell command used to exec Docker")
val dockerVersion = TaskKey[Option[DockerVersion]]("dockerVersion", "The docker server version")
val dockerBuildOptions = SettingKey[Seq[String]]("dockerBuildOptions", "Options used for the Docker build")
val dockerBuildEnvVars =
SettingKey[Map[String, String]]("dockerBuildEnvVars", "Environment variables used for the Docker build")
val dockerBuildCommand = SettingKey[Seq[String]]("dockerBuildCommand", "Command for building the Docker image")
val dockerLabels = SettingKey[Map[String, String]]("dockerLabels", "Labels applied to the Docker image")
val dockerEnvVars =
Expand Down Expand Up @@ -74,6 +76,7 @@ private[packager] trait DockerKeysEx extends DockerKeys {
"Setting to true will cause Docker to bundle a tini in the container, to run as the init process, which is recommended for JVM apps. " +
"Requires Docker API version 1.25+"
)
val dockerBuildkitEnabled = TaskKey[Boolean]("dockerBuildkitEnabled", "Detects whether buildkit is enabled")
val dockerBuildxPlatforms =
SettingKey[Seq[String]]("dockerBuildxPlatforms", "The docker image platforms for buildx multi-platform build")
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# When using BuildKit, intermediate build steps are not exposed as images
> 'set dockerBuildEnvVars += "DOCKER_BUILDKIT" -> "0"'
# First make sure we start clean
$ exec bash -c 'docker image prune -f --filter label=snp-multi-stage=intermediate'
# Generate the Docker image locally
Expand Down
6 changes: 6 additions & 0 deletions src/sphinx/formats/docker.rst
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ Publishing Settings
Overrides the default Docker build options.
Defaults to ``Seq("--force-rm", "-t", "[dockerAlias]")``. This default is expanded if either ``dockerUpdateLatest`` or ``dockerBuildInit`` is set to true.

``dockerBuildEnvVars``
The environment variables passed to the Docker build.
Defaults to empty.
For example, to disable Docker BuildKit:
``dockerBuildEnvVars += "DOCKER_BUILDKIT" -> "0"``

``dockerExecCommand``
Overrides the default Docker exec command.
Defaults to ``Seq("docker")``
Expand Down

0 comments on commit 37fcec3

Please sign in to comment.