1
1
import sbt .*
2
2
import sbt .Keys .*
3
3
4
+ import com .typesafe .sbt .packager .docker .{Cmd , LayeredMapping }
5
+
4
6
ThisBuild / credentials += Credentials (
5
7
" GitHub Package Registry" ,
6
8
" maven.pkg.github.com" ,
@@ -29,7 +31,8 @@ lazy val compileSettings = Seq(
29
31
// Ensure Java-based DAS SDK code is compiled first, so it is accessible from Scala.
30
32
compileOrder := CompileOrder .JavaThenScala ,
31
33
// Ensure we fork new JVM for run, so we can set JVM flags.
32
- Compile / run / fork := true )
34
+ Compile / run / fork := true ,
35
+ Compile / mainClass := Some (" com.rawlabs.das.server.DASServer" ))
33
36
34
37
lazy val testSettings = Seq (
35
38
// Ensure we fork new JVM for run, so we can set JVM flags.
@@ -50,8 +53,72 @@ lazy val publishSettings = Seq(
50
53
lazy val strictBuildSettings =
51
54
commonSettings ++ compileSettings ++ buildSettings ++ testSettings ++ Seq (scalacOptions ++= Seq (" -Xfatal-warnings" ))
52
55
56
+ lazy val dockerSettings = Seq (
57
+ Docker / packageName := " das-mock-server" ,
58
+ dockerBaseImage := " eclipse-temurin:21-jre" ,
59
+ dockerLabels ++= Map (
60
+ " vendor" -> " RAW Labs SA" ,
61
+ " product" -> " das-mock-server" ,
62
+ " image-type" -> " final" ,
63
+ " org.opencontainers.image.source" -> " https://github.com/raw-labs/das-server-scala" ),
64
+ Docker / daemonUser := " raw" ,
65
+ Docker / daemonUserUid := Some (" 1001" ),
66
+ Docker / daemonGroup := " raw" ,
67
+ Docker / daemonGroupGid := Some (" 1001" ),
68
+ dockerExposedVolumes := Seq (" /var/log/raw" ),
69
+ dockerExposedPorts := Seq (50051 ),
70
+ dockerEnvVars := Map (" PATH" -> s " ${(Docker / defaultLinuxInstallLocation).value}/bin: $$ PATH " ),
71
+ dockerEnvVars += " LANG" -> " C.UTF-8" ,
72
+ updateOptions := updateOptions.value.withLatestSnapshots(true ),
73
+ Linux / linuxPackageMappings += packageTemplateMapping(s " /var/lib/ ${packageName.value}" )(),
74
+ bashScriptDefines := {
75
+ val ClasspathPattern = " declare -r app_classpath=\" (.*)\"\n " .r
76
+ bashScriptDefines.value.map {
77
+ case ClasspathPattern (classpath) => s """
78
+ |declare -r app_classpath=" $$ {app_home}/../conf: $classpath"
79
+ | """ .stripMargin
80
+ case _ @ entry => entry
81
+ }
82
+ },
83
+ Docker / dockerLayerMappings := (Docker / dockerLayerMappings).value.map {
84
+ case lm @ LayeredMapping (Some (1 ), file, path) => {
85
+ val fileName = java.nio.file.Paths .get(path).getFileName.toString
86
+ if (! fileName.endsWith(" .jar" )) {
87
+ // If it is not a jar, put it on the top layer. Configuration files and other small files.
88
+ LayeredMapping (Some (2 ), file, path)
89
+ } else if (fileName.startsWith(" com.raw-labs" ) && fileName.endsWith(" .jar" )) {
90
+ // If it is one of our jars, also top layer. These will change often.
91
+ LayeredMapping (Some (2 ), file, path)
92
+ } else {
93
+ // Otherwise it is a 3rd party library, which only changes when we change dependencies, so leave it in layer 1
94
+ lm
95
+ }
96
+ }
97
+ case lm @ _ => lm
98
+ },
99
+ Docker / version := {
100
+ val ver = version.value
101
+ // Docker tags have their own restrictions - only allow [a-zA-Z0-9_.-]
102
+ // Replace + with - and ensure no invalid characters
103
+ ver.replaceAll(" [+]" , " -" ).replaceAll(" [^\\ w.-]" , " -" )
104
+ },
105
+ dockerAlias := {
106
+ val devRegistry = sys.env.getOrElse(" DEV_REGISTRY" , " ghcr.io/raw-labs/das-server-scala" )
107
+ dockerAlias.value.withRegistryHost(Some (devRegistry))
108
+ },
109
+ dockerAliases := {
110
+ val devRegistry = sys.env.getOrElse(" DEV_REGISTRY" , " ghcr.io/raw-labs/das-server-scala" )
111
+ val releaseRegistry = sys.env.get(" RELEASE_DOCKER_REGISTRY" )
112
+ val baseAlias = dockerAlias.value.withRegistryHost(Some (devRegistry))
113
+
114
+ releaseRegistry match {
115
+ case Some (releaseReg) => Seq (baseAlias, dockerAlias.value.withRegistryHost(Some (releaseReg)))
116
+ case None => Seq (baseAlias)
117
+ }
118
+ })
119
+
53
120
lazy val root = (project in file(" ." ))
54
- .enablePlugins(BuildInfoPlugin )
121
+ .enablePlugins(BuildInfoPlugin , JavaAppPackaging , DockerPlugin )
55
122
.settings(
56
123
name := " das-server-scala" ,
57
124
buildInfoKeys := Seq [BuildInfoKey ](name, version, scalaVersion),
@@ -71,19 +138,29 @@ lazy val root = (project in file("."))
71
138
// Configuration
72
139
" com.typesafe" % " config" % " 1.4.3" ,
73
140
// Protocol DAS
74
- " com.raw-labs" %% " protocol-das" % " 1.0.0 " ,
141
+ " com.raw-labs" %% " protocol-das" % " 1.0.2 " ,
75
142
// Akka Streams
76
143
" org.apache.pekko" %% " pekko-actor-typed" % " 1.1.3" ,
77
144
" org.apache.pekko" %% " pekko-stream" % " 1.1.3" ,
78
145
" org.apache.pekko" %% " pekko-http" % " 1.1.0" ,
79
- // Jackson databind
80
- " com.fasterxml.jackson.core" % " jackson-databind" % " 2.18.2" % Test ,
81
- // gRPC Testing
82
- " io.grpc" % " grpc-inprocess" % " 1.62.2" ,
83
146
// Web UI
84
147
" com.typesafe.akka" %% " akka-http" % " 10.5.3" ,
85
148
" com.lihaoyi" %% " scalatags" % " 0.13.1" ,
149
+ // Jackson databind
150
+ " com.fasterxml.jackson.core" % " jackson-databind" % " 2.18.2" % Test ,
151
+ // gRPC Testing
152
+ " io.grpc" % " grpc-inprocess" % " 1.62.2" % Test ,
86
153
// Postgres
87
154
" org.postgresql" % " postgresql" % " 42.7.4" % Test ,
88
155
// Testing
89
- " org.scalatest" %% " scalatest" % " 3.2.19" % Test ))
156
+ " org.scalatest" %% " scalatest" % " 3.2.19" % Test ),
157
+ dockerSettings)
158
+
159
+ lazy val printDockerImageName = taskKey[Unit ](" Prints the full Docker image name that will be produced" )
160
+
161
+ printDockerImageName := {
162
+ // Get the main Docker alias (the first one in the sequence)
163
+ val alias = (Docker / dockerAliases).value.head
164
+ // The toString method already returns the full image name with registry and tag
165
+ println(s " DOCKER_IMAGE= ${alias}" )
166
+ }
0 commit comments