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

[Docker] add dockerEnvVars support #1137

Merged
merged 7 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 12 additions & 2 deletions src/main/scala/com/typesafe/sbt/packager/docker/DockerPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.typesafe.sbt.packager.linux.LinuxPlugin.autoImport.{daemonUser, defau
import com.typesafe.sbt.packager.universal.UniversalPlugin
import com.typesafe.sbt.packager.universal.UniversalPlugin.autoImport.stage
import com.typesafe.sbt.SbtNativePackager.Universal
import com.typesafe.sbt.packager.Compat._
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if this compiles with sbt 0.13.x

import com.typesafe.sbt.packager.{MappingsHelper, Stager}

import scala.sys.process.Process
Expand Down Expand Up @@ -61,7 +60,7 @@ object DockerPlugin extends AutoPlugin {
*/
val UnixSeparatorChar = '/'

override def requires = UniversalPlugin
override def requires: UniversalPlugin.type = UniversalPlugin
Copy link
Contributor

Choose a reason for hiding this comment

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

The more generic type would be Plugins


override def projectConfigurations: Seq[Configuration] = Seq(Docker)

Expand All @@ -71,6 +70,7 @@ object DockerPlugin extends AutoPlugin {
dockerExposedUdpPorts := Seq(),
dockerExposedVolumes := Seq(),
dockerLabels := Map(),
dockerEnvVars := Map(),
dockerRepository := None,
dockerUsername := None,
dockerAlias := DockerAlias(
Expand Down Expand Up @@ -104,6 +104,7 @@ object DockerPlugin extends AutoPlugin {
generalCommands ++
Seq(makeWorkdir(dockerBaseDirectory)) ++ makeAdd(dockerVersion.value, dockerBaseDirectory, user, group) ++
dockerLabels.value.map(makeLabel) ++
dockerEnvVars.value.map(makeEnvVar) ++
makeExposePorts(dockerExposedPorts.value, dockerExposedUdpPorts.value) ++
makeVolumes(dockerExposedVolumes.value, user, group) ++
Seq(makeUser(user), makeEntrypoint(dockerEntrypoint.value), makeCmd(dockerCmd.value))
Expand Down Expand Up @@ -175,6 +176,15 @@ object DockerPlugin extends AutoPlugin {
Cmd("LABEL", variable + "=\"" + value.toString + "\"")
}

/**
* @param envVar
* @return ENV command
*/
private final def makeEnvVar(envVar: (String, String)): CmdLike = {
val (variable, value) = envVar
Cmd("ENV", variable + "=\"" + value.toString + "\"")
}

/**
* @param dockerBaseDirectory, the installation directory
*/
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 @@ -33,6 +33,8 @@ trait DockerKeys {
val dockerBuildOptions = SettingKey[Seq[String]]("dockerBuildOptions", "Options 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 =
SettingKey[Map[String, String]]("dockerEnvVars", "Environment Variables applied to the Docker image")
val dockerRmiCommand =
SettingKey[Seq[String]]("dockerRmiCommand", "Command for removing the Docker image from the local registry")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ trait CmdLike {
* }}}
*/
case class ExecCmd(cmd: String, args: String*) extends CmdLike {
def makeContent =
def makeContent: String =
"%s [%s]\n" format (cmd, args.map('"' + _ + '"').mkString(", "))
}

Expand All @@ -59,7 +59,7 @@ case class ExecCmd(cmd: String, args: String*) extends CmdLike {
* }}}
*/
case class Cmd(cmd: String, args: String*) extends CmdLike {
def makeContent = "%s %s\n" format (cmd, args.mkString(" "))
def makeContent: String = "%s %s\n" format (cmd, args.mkString(" "))
}

/**
Expand All @@ -76,7 +76,7 @@ case class Cmd(cmd: String, args: String*) extends CmdLike {
* }}}
*/
case class CombinedCmd(cmd: String, arg: CmdLike) extends CmdLike {
def makeContent = "%s %s\n" format (cmd, arg.makeContent)
def makeContent: String = "%s %s\n" format (cmd, arg.makeContent)
}

/** Represents dockerfile used by docker when constructing packages. */
Expand Down
19 changes: 19 additions & 0 deletions src/sbt-test/docker/envVars/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
enablePlugins(DockerPlugin)

name := "simple-test"

version := "0.1.0"

dockerEnvVars := Map("FOO" -> "bar", "FOO_BAR" -> "foo bar", "NUMBER" -> "123")


TaskKey[Unit]("checkDockerfile") := {
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")

assert(dockerfile contains """ENV FOO="bar"""", s"does not contain foo=bar\n$dockerfile")
assert(dockerfile contains """ENV FOO_BAR="foo bar"""", s"does not contain foo=bar\n$dockerfile")
assert(dockerfile contains """ENV NUMBER="123"""", s"does not contain foo=bar\n$dockerfile")

streams.value.log.success("Successfully tested Dockerfile")
()
}
1 change: 1 addition & 0 deletions src/sbt-test/docker/envVars/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version"))
3 changes: 3 additions & 0 deletions src/sbt-test/docker/envVars/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Stage the distribution and ensure files show up.
> docker:stage
> checkDockerfile