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

Support cross building #1527

Merged
merged 1 commit into from
Jan 16, 2023
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
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 @@ -83,6 +83,7 @@ object DockerPlugin extends AutoPlugin {
dockerExposedPorts := Seq(),
dockerExposedUdpPorts := Seq(),
dockerExposedVolumes := Seq(),
dockerBuildxPlatforms := Seq(),
dockerLabels := Map(),
dockerEnvVars := Map(),
dockerRepository := None,
Expand Down Expand Up @@ -273,11 +274,21 @@ object DockerPlugin extends AutoPlugin {
def publishTask =
Def.task {
val _ = publishLocal.value
val alias = dockerAliases.value
val log = streams.value.log
val execCommand = dockerExecCommand.value
val alias = dockerAliases.value
val context = stage.value
val multiplatform = dockerBuildxPlatforms.value.nonEmpty
val execCommand =
if (multiplatform)
dockerExecCommand.value ++ Seq(
"buildx",
"build",
s"--platform=${dockerBuildxPlatforms.value.mkString(",")}",
"--push"
) ++ dockerBuildOptions.value :+ "."
else dockerBuildCommand.value
Comment on lines -278 to +289
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@muuki88 @dswiecki This line introduced a bug. Here's a fix: #1533

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it should be dockerExecCommand, thank for quick response 👍

alias.foreach { aliasValue =>
publishDocker(execCommand, aliasValue.toString, log)
publishDocker(context, execCommand, aliasValue.toString, log, multiplatform)
}
} tag (Tags.Network, Tags.Publish)

Expand Down Expand Up @@ -377,7 +388,7 @@ object DockerPlugin extends AutoPlugin {
}

/**
* @param dockerBaseDirectory, the installation directory
* @param dockerBaseDirectory , the installation directory
*/
private final def makeWorkdir(dockerBaseDirectory: String): CmdLike =
Cmd("WORKDIR", dockerBaseDirectory)
Expand Down Expand Up @@ -705,7 +716,7 @@ object DockerPlugin extends AutoPlugin {
log.info(s"Removed image ${tag}")
}

def publishDocker(execCommand: Seq[String], tag: String, log: Logger): Unit = {
def publishDocker(context: File, execCommand: Seq[String], tag: String, log: Logger, multiplatform: Boolean): Unit = {
val loginRequired = new AtomicBoolean(false)

def publishLogger(log: Logger) =
Expand All @@ -728,11 +739,11 @@ object DockerPlugin extends AutoPlugin {
override def buffer[T](f: => T): T = f
}

val cmd = execCommand ++ Seq("push", tag)
val cmd = if (multiplatform) execCommand else execCommand ++ Seq("push", tag)

log.debug("Executing " + cmd.mkString(" "))

val ret = sys.process.Process(cmd) ! publishLogger(log)
val ret = sys.process.Process(cmd, context) ! publishLogger(log)

if (loginRequired.get)
sys.error("""No credentials for repository, please run "docker login"""")
Expand Down
2 changes: 2 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 @@ -74,4 +74,6 @@ 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 dockerBuildxPlatforms =
SettingKey[Seq[String]]("dockerBuildxPlatforms", "The docker image platforms for buildx multi-platform build")
}
2 changes: 2 additions & 0 deletions test-project-docker/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ version := "0.1.0"
maintainer := "Gary Coady <gary@lyranthe.org>"
dockerBaseImage := "openjdk:8-jre-alpine"
dockerUpdateLatest := true
dockerBuildxPlatforms := Seq("linux/arm64/v8", "linux/amd64")
dockerUsername := Some("dswiecki")
2 changes: 1 addition & 1 deletion test-project-docker/project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.2.6
sbt.version=1.5.4