Created
August 13, 2017 14:51
-
-
Save charithe/7f36debdb3b43598a78b8afcc08ed357 to your computer and use it in GitHub Desktop.
SBT Build Environment Dependent Docker Image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lazy val deploymentSettings = settingKey[DeploymentSettings]("My environment") | |
lazy val commonSettings = Seq( | |
name := "test-docker", | |
version := "0.1", | |
scalaVersion := "2.12.3", | |
imageNames in docker := Seq( | |
ImageName(s"${deploymentSettings.value.dockerRegistry}/test-image")), | |
dockerfile in docker := { | |
new Dockerfile { | |
from("anapsix/alpine-java") | |
entryPoint("java", "-version") | |
} | |
} | |
) | |
lazy val root = (project in file(".")) | |
.enablePlugins(BuildEnvPlugin, DockerPlugin) | |
.settings(commonSettings) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import BuildEnvPlugin.autoImport.BuildEnv | |
import com.sun.xml.internal.ws.api.config.management.policy.ManagementAssertion.Setting | |
import sbt.PluginTrigger.AllRequirements | |
import sbt.{AutoPlugin, Def, PluginTrigger, Plugins} | |
import sbtdocker.DockerPlugin | |
import sbt._ | |
import sbt.Keys._ | |
object BuildEnvPlugin extends AutoPlugin { | |
override def trigger: PluginTrigger = AllRequirements | |
override def requires: Plugins = DockerPlugin | |
object autoImport { | |
object BuildEnv extends Enumeration { | |
val Production, Stage, Test, Developement = Value | |
} | |
val buildEnv: SettingKey[BuildEnv.Value] = | |
settingKey[BuildEnv.Value]("the current build environment") | |
val deploymentSettings: SettingKey[DeploymentSettings] = | |
settingKey[DeploymentSettings]("Deployment settings") | |
} | |
override def projectSettings: Seq[Def.Setting[_]] = { | |
import autoImport._ | |
Seq( | |
buildEnv := sys.props | |
.get("env") | |
.orElse(sys.env.get("BUILD_ENV")) | |
.flatMap { | |
case "prod" => Some(BuildEnv.Production) | |
case "stage" => Some(BuildEnv.Stage) | |
case "test" => Some(BuildEnv.Test) | |
case "dev" => Some(BuildEnv.Developement) | |
case _ => None | |
} | |
.getOrElse(BuildEnv.Developement), | |
deploymentSettings := { | |
buildEnv.value match { | |
case BuildEnv.Production => ProductionDeploymentSettings | |
case _ => LocalDeploymentSettings | |
} | |
}, | |
onLoadMessage := { | |
val defaultMessage = onLoadMessage.value | |
val env = buildEnv.value | |
s"""|$defaultMessage | |
|Running in build environment: $env""".stripMargin | |
} | |
) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait DeploymentSettings { | |
var dockerRegistry: String | |
var imageName: String = "hello" | |
var dockerImages: Seq[ImageName] = Seq(ImageName(s"$dockerRegistry/$imageName")) | |
} | |
object LocalDeploymentSettings extends DeploymentSettings { | |
override var dockerRegistry = "eu.gcr.io/staging" | |
} | |
object ProductionDeploymentSettings extends DeploymentSettings { | |
override var dockerRegistry = "eu.gcr.io/production" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment