Upgrade to Pro — share decks privately, control downloads, hide ads and more …

もう一つのビルドツール mill で作る Docker イメージ / Build docker image with mill the yet another build tool

AGAWA Koji
September 16, 2019

もう一つのビルドツール mill で作る Docker イメージ / Build docker image with mill the yet another build tool

AGAWA Koji

September 16, 2019
Tweet

More Decks by AGAWA Koji

Other Decks in Programming

Transcript

  1. ΋͏ҰͭͷϏϧυπʔϧ mill Ͱ࡞Δ Docker Πϝʔδ Ѩ઒ ߞ࢘ @atty303 AI tech

    studio / CyberAgent, Inc. © 2019 CyberAgent, Inc. 1/33
  2. Ѩ઒ ߞ࢘ ιϑτ΢ΣΞΤϯδχΞ @CyberAgent, Inc. • ۀ຿ྺ 14 ೥ /

    Scala ྺ 4 ೥ • ήʔϜ͕޷͖ (Steam ϝΠϯ) ීஈ͸ઃܭͨ͠ΓόοΫΤϯυ Λ΍Γͭͭɺͨ·ʹ Web ϑϩϯ τΤϯυ΍ Android Λ৮ͬͨΓ ΋͠·͢ɻ © 2019 CyberAgent, Inc. 2/33
  3. 2018 Scala Developer Survey 1 Which build tool are you

    currently using? 2 2 https://typesafe.co1.qualtrics.com/results/public/ dHlwZXNhZmUtVVJfNlB4cWNSMXdub0liVExmLTVhZjMwZDc4MjAzMGVkMDAxNDhkOTc4 OA== 1 https://www.scala-lang.org/news/survey-2018.html © 2019 CyberAgent, Inc. 4/33
  4. Your shiny new Java/Scala build tool! • https://github.com/lihaoyi/mill • Made

    by Li Hayoi • Ammonite / fastparse / utest / upickle © 2019 CyberAgent, Inc. 6/33
  5. యܕతͳMillϏϧυઃఆ // build.sc import mill._, scalalib._ object foo extends ScalaModule

    { def scalaVersion = T("2.12.10") def scalacOptions = T(Seq( "-Ypartial-unification", )) def ivyDeps = T(Agg( ivy"com.lihaoyi::upickle:0.5.1", )) } © 2019 CyberAgent, Inc. 7/33
  6. IntelliJ IDEA ΁ͷΠϯϙʔτ ૢ࡞ sbt mill ॳճΠϯϙʔτ 04:07 01:45 ࠶Πϯϙʔτ

    02:25 00:23 • ಉ͡ MacBook • ϓϩδΣΫτ਺͸ 28 © 2019 CyberAgent, Inc. 8/33
  7. mill ͷ֦ு import mill._, scalalib._ trait ExampleModule extends Module {

    def hello = T { "Hello" } def firstName = T { "Koji" } def lastName = T { "AGAWA" } def fullName = T { s"${firstName()} ${lastName()}" } def message = T { s"${hello()}, ${fullName()}" } } object example extends ScalaModule with ExampleModule { def scalaVersion = "2.13.0" } © 2019 CyberAgent, Inc. 9/33
  8. λεΫͷґଘؔ܎ $ ./mill visualize example._ 3 $ open out/visualize/out.png 3

    ExampleModule ʹؔ܎͢Δ෦෼ͷΈൈਮ © 2019 CyberAgent, Inc. 12/33
  9. ίʔυྔগͳ͍ package mill package contrib.docker import mill.scalalib.JavaModule import os.Shellable.IterableShellable import

    scala.collection.immutable._ trait DockerModule { outer: JavaModule => trait DockerConfig extends mill.Module { /** * Tags that should be applied to the built image * In the standard registry/repository:tag format */ def tags: T[Seq[String]] = T(List(outer.artifactName())) def labels: T[Map[String, String]] = Map.empty[String, String] def baseImage: T[String] = "gcr.io/distroless/java:latest" def pullBaseImage: T[Boolean] = T(baseImage().endsWith(":latest")) private def baseImageCacheBuster: T[(Boolean, Double)] = T.input { val pull = pullBaseImage() if(pull) (pull, Math.random()) else (pull, 0d) } def dockerfile: T[String] = T { val jarName = assembly().path.last val labelRhs = labels() .map { case (k, v) => val lineBrokenValue = v .replace("\r\n", "\\\r\n") .replace("\n", "\\\n") .replace("\r", "\\\r") s""""$k"="$lineBrokenValue"""" } .mkString(" ") val labelLine = if(labels().isEmpty) "" else s"LABEL $labelRhs" s""" |FROM ${baseImage()} |$labelLine |COPY $jarName /$jarName |ENTRYPOINT ["java", "-jar", "/$jarName"] """.stripMargin } final def build = T { val dest = T.ctx().dest val asmPath = outer.assembly().path os.copy(asmPath, dest / asmPath.last) os.write(dest / "Dockerfile", dockerfile()) val log = T.ctx().log val tagArgs = tags().flatMap(t => List("-t", t)) val (pull, _) = baseImageCacheBuster() val pullLatestBase = IterableShellable(if(pull) Some("--pull") else None) val result = os .proc("docker", "build", tagArgs, pullLatestBase, dest) .call(stdout = os.Inherit, stderr = os.Inherit) log.info(s"Docker build completed ${if(result.exitCode == 0) "successfully" else "unsuccessfully"} with ${result.exitCode}") tags() } final def push() = T.command { val tags = build() tags.foreach(t => os.proc("docker", "push", t).call(stdout = os.Inherit, stderr = os.Inherit)) } } } © 2019 CyberAgent, Inc. 17/33
  10. ͔͠͠… final def build = T { val dest =

    T.ctx().dest val asmPath = outer.assembly().path os.copy(asmPath, dest / asmPath.last) os.write(dest / "Dockerfile", dockerfile()) © 2019 CyberAgent, Inc. 18/33
  11. Jib Core 5 Java library for building containers by Google

    5 https://github.com/GoogleContainerTools/jib/tree/master/jib-core © 2019 CyberAgent, Inc. 20/33
  12. ੒Ռ෺ • Jib Core ͷ͓ӄͰ mill ͷϞδϡʔϧԽ͸؆୯ • ۀ຿ϓϩδΣΫτʹಋೖ •

    ScalaPB ϞδϡʔϧΛ sbt ൛ʹ͚ۙͮͨΓ • GenIdea Ϟδϡʔϧ͕ ScalaPB ͷੜ੒ιʔεΛ ೝࣝ͢ΔΑ͏ʹ • ޙ೔ Pull Request ͠·͢ © 2019 CyberAgent, Inc. 32/33