Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pandaforme/e378dc3f1f32aa252b14e40937491e9c to your computer and use it in GitHub Desktop.
Save pandaforme/e378dc3f1f32aa252b14e40937491e9c to your computer and use it in GitHub Desktop.
sbt-release integrates with sbt-docker
import sbt.Keys._
import sbt._
import sbtdocker.DockerPlugin.autoImport._
import sbtrelease.ReleasePlugin.autoImport.ReleaseTransformations._
lazy val resolversSettings = ...
lazy val versions = ...
lazy val dependenciesSettings = ...
// Create a default Scala style task to run with compiles
lazy val scalastyleCompile = taskKey[Unit]("Run scalastyle before compile")
lazy val commonSettings = Seq(
scalaVersion := "2.11.8",
scalacOptions ++= Seq(
"-deprecation",
"-unchecked",
"-encoding",
"UTF-8",
"-Xlint",
"-Yclosure-elim",
"-Yinline",
"-Xverify",
"-feature",
"-language:postfixOps",
"-target:jvm-1.8"
),
resolvers ++= resolversSettings,
libraryDependencies ++= dependenciesSettings,
scalastyleCompile := scalastyle.in(Compile).toTask("").value,
(scalastyleConfig in Compile) := file(
s"${(resourceDirectory in Compile).value}/scalastyle-config.xml"),
(compile in Compile) <<= (compile in Compile) dependsOn scalastyleCompile
) ++ scalafmtSettings
lazy val dockerSettings = ...
lazy val loginAwsEcr = TaskKey[Unit]("loginAwsEcr", "Login AWS ECR")
loginAwsEcr := {
import sys.process._
Seq("bash", "(aws ecr get-login --region us-west-2)") !
}
lazy val publishDocker = ReleaseStep(action = st => {
val extracted = Project.extract(st)
val ref: ProjectRef = extracted.get(thisProjectRef)
extracted.runAggregated(loginAwsEcr in ref, st)
extracted.runAggregated(
sbtdocker.DockerKeys.dockerBuildAndPush in sbtdocker.DockerPlugin.autoImport.docker in ref,
st)
st
})
lazy val releaseSettings = Seq(
releaseProcess := Seq[ReleaseStep](
checkSnapshotDependencies,
inquireVersions,
runTest,
setReleaseVersion,
commitReleaseVersion,
publishDocker,
setNextVersion,
commitNextVersion
))
lazy val root = Project(
id = "root",
base = file("."),
settings = commonSettings ++ dockerSettings ++ releaseSettings
).enablePlugins(DockerPlugin)
@deontaljaard
Copy link

deontaljaard commented Jan 23, 2018

Very helpful, thank you @pandaforme.

I had to make two changes:

  1. Prepend the ECR namespace to the repo name. For example, that it looks as follows: xxxxxxxxxxxx.dkr.ecr.eu-central-1.amazonaws.com/[organization]/[service]. This is the imageName definition in dockerSettings:
    imageNames in docker := Seq( ImageName( namespace = Some(s"$ecrNamespace/${organization.value}"), repository = name.value, tag = Some(version.value) ) )
  2. Execute the ouput from the 'aws ecr get-login' command. The loginAwsEcr looks as follows:
    lazy val loginAwsEcr = TaskKey[Unit]("loginAwsEcr", "Login AWS ECR") loginAwsEcr := { import sys.process._ val dockerLogin = Seq("aws", "ecr", "get-login", "--no-include-email", "--region", "eu-central-1").!! dockerLogin.replaceAll("\n", "").split(" ").toSeq.! }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment