Skip to content

Commit

Permalink
Build backend image with dependencies in there
Browse files Browse the repository at this point in the history
  • Loading branch information
daddykotex committed Dec 16, 2023
1 parent 4125ed5 commit ffeecc7
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 14 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ jobs:
java-version: 17
cache: sbt

- run: sbt "test; backend / Docker / publishLocal"
- run: sbt "test"

- run: ./scripts/build-image.sh
env:
PUBLISH_OFFICIAL: "false"

- name: Compress target directories
run: tar cf targets.tar target project/target
Expand Down Expand Up @@ -71,6 +75,8 @@ jobs:
env:
FLY_API_TOKEN: ${{ secrets.FLY_TOKEN }}
- run: ./scripts/build-image.sh
env:
PUBLISH_OFFICIAL: "true"
- run: flyctl deploy --remote-only
env:
FLY_API_TOKEN: ${{ secrets.FLY_TOKEN }}
113 changes: 102 additions & 11 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import org.scalajs.linker.interface.ModuleSplitStyle
import com.typesafe.sbt.packager.docker._
import smithy4s_codegen._

ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "com.example"
Expand All @@ -13,6 +15,7 @@ val cirisVersion = "3.5.0"
lazy val baseUri = settingKey[String](
"""Base URI of the backend, defaults to `""` (empty string)."""
)

lazy val bundleAssets = settingKey[Boolean](
"""Whether or not assets should be bundled in the backend jar"""
)
Expand All @@ -21,8 +24,15 @@ ThisBuild / bundleAssets := sys.env
.map(_.toBoolean)
.getOrElse(false)

lazy val smithyClasspath = settingKey[Seq[ModuleID]](
"""List of artifacts to include in backend image that has dependencies"""
)
lazy val smithyClasspathDir = settingKey[String](
"""Path of the smithy classpath directory (where we mount the config and the jars)"""
)

lazy val root = (project in file("."))
.aggregate(api, frontend, backend)
.aggregate(api, frontend, backend, backendDependencies)

lazy val api = (project in file("modules/api"))

Expand Down Expand Up @@ -106,16 +116,97 @@ lazy val backend = (project in file("modules/backend"))
Docker / dockerExposedPorts := List(9000),
Docker / packageName := "smithy4s-code-generation",
Docker / dockerRepository := Some("daddykotex"),
dockerAliases ++= Seq(
dockerAlias.value.withTag(sys.env.get("GITHUB_SHA")),
dockerAlias.value
.withName("morning-bird-7081")
.withRegistryHost(Option("registry.fly.io")),
dockerAlias.value
.withTag(sys.env.get("GITHUB_SHA"))
.withName("morning-bird-7081")
.withRegistryHost(Option("registry.fly.io"))
),
dockerAliases ++= {
val sha = sys.env.get("GITHUB_SHA").map(_.take(10))
val latests = Seq(
dockerAlias.value
.withName("morning-bird-7081")
.withRegistryHost(Option("registry.fly.io"))
)
val shas = sha.toSeq.flatMap { s =>
Seq(
dockerAlias.value.withTag(Some(s)),
dockerAlias.value
.withTag(Some(s))
.withName("morning-bird-7081")
.withRegistryHost(Option("registry.fly.io"))
)
}

latests ++ shas
},
Docker / version := "latest",
dockerBaseImage := "eclipse-temurin:17.0.6_10-jre"
)

/** This is a project that's only intented to be a copy of backend but that
* builds in an image with some dependencies for the smithy-classpath.
*/
lazy val backendDependencies = project
.enablePlugins(DockerPlugin)
.settings(
smithyClasspath := Seq(
"com.disneystreaming.alloy" % "alloy-core" % "0.2.8"
),
Universal / mappings := {
val depRes = dependencyResolution.value
val artifacts = smithyClasspath.value
val smithyClasspathOutput = target.value / "smithy-classpath"
val logger = sLog.value
val resolved = artifacts.flatMap { module =>
depRes.retrieve(module, None, target.value, logger) match {
case Left(value) =>
sys.error(s"Unable to resolve smithy classpath module $module")
case Right(value) => value.headOption.map(f => module -> f)
}
}
val smithyClasspathValue = smithyClasspathDir.value

val entries: Seq[SmithyClasspathEntry] =
resolved.map { case (module, file) =>
SmithyClasspathEntry(
module,
file,
s"$smithyClasspathValue/${file.name}"
)
}
val entriesMapping =
entries.map { case SmithyClasspathEntry(_, file, pathInImage) =>
file -> pathInImage
}

val smithyClasspathFile = target.value / "smithy-classpath.json"
SmithyClasspath.toFile(
smithyClasspathFile,
entries,
(Docker / defaultLinuxInstallLocation).value
)
val configMapping = Seq(
smithyClasspathFile -> s"$smithyClasspathValue/smithy-classpath.json"
)
entriesMapping ++ configMapping
},
dockerAliases := {
val beAlias = (backend / dockerAlias).value
val sha = sys.env.get("GITHUB_SHA").map(_.take(10))
val tag =
sha.map(s => s"with-dependencies-$s").getOrElse("with-dependencies")
Seq(
dockerAlias.value.withTag(Some(tag)),
dockerAlias.value
.withTag(Some(tag))
.withName("morning-bird-7081")
.withRegistryHost(Option("registry.fly.io"))
)
},
dockerEntrypoint := (backend / dockerEntrypoint).value,
dockerBaseImage := (backend / dockerAlias).value.toString,
smithyClasspathDir := "smithy-classpath",
dockerEnvVars ++= {
val inDockerPath = (Docker / defaultLinuxInstallLocation).value
val smithyClasspathValue = smithyClasspathDir.value
Map(
"SMITHY_CLASSPATH_CONFIG" -> s"$inDockerPath/$smithyClasspathValue/smithy-classpath.json"
)
}
)
33 changes: 33 additions & 0 deletions project/SmithyClasspath.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package smithy4s_codegen

import sbt.librarymanagement.ModuleID
import sbt.librarymanagement.Disabled
import java.io.File
import sbt.io.IO

final case class SmithyClasspathEntry(
module: sbt.ModuleID,
file: File,
relativePath: String
)
object SmithyClasspath {
def toFile(
target: File,
all: Seq[SmithyClasspathEntry],
dockerPath: String
): Unit = {
val entries = all.map { sce =>
encodeModule(sce.module) -> ujson.Str(s"$dockerPath/${sce.relativePath}")
}.toMap
val content = ujson.Obj(
"entries" -> ujson.Obj.from(entries)
)
IO.write(target, content.render())
}

private def encodeModule(m: ModuleID): String = {
m.crossVersion match {
case Disabled => s"${m.organization}:${m.name}:${m.revision}"
}
}
}
1 change: 1 addition & 0 deletions project/deps.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
libraryDependencies += "com.lihaoyi" %% "upickle" % "3.1.3"
12 changes: 10 additions & 2 deletions scripts/build-image.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
#!/bin/bash

set -euo pipefail
set -eo pipefail

export BUNDLE_ASSETS="true"

if [[ "$PUBLISH_OFFICIAL" == "true" ]]; then
BACKEND_PUBLISH="publish"
else
BACKEND_PUBLISH="publishLocal"
fi

set -u

(cd modules/frontend; npm i && npm run build)
sbt "backend / Docker / publish"
sbt "backend / Docker / $BACKEND_PUBLISH; backendDependencies / Docker / $BACKEND_PUBLISH"

0 comments on commit ffeecc7

Please sign in to comment.