Skip to content

Commit 6547b73

Browse files
committed
Automate Docker container build and fix Java version detection
Previously, the Docker release process was manual. Now, the Docker container is built by sbt on CI release and pushed to Docker Hub. Also, the Docker container previously used the default Java version to launch `lsif-java`. This caused problems in cases where the sources of the project would be compiled with a different Java version from the version that's specified in `dump.lsif`. Now, the LSIF version should always match the version that was used to compile the sources.
1 parent 6ce148c commit 6547b73

File tree

8 files changed

+118
-66
lines changed

8 files changed

+118
-66
lines changed

.devcontainer/Dockerfile

Lines changed: 0 additions & 11 deletions
This file was deleted.

.devcontainer/devcontainer.json

Lines changed: 0 additions & 27 deletions
This file was deleted.

.github/workflows/docker.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Docker
2+
on:
3+
push:
4+
tags: ["*"]
5+
jobs:
6+
publish:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
with:
11+
fetch-depth: 0
12+
- uses: olafurpg/setup-scala@v13
13+
- uses: docker/setup-buildx-action@v1
14+
- name: Login to DockerHub
15+
uses: docker/login-action@v1
16+
with:
17+
username: ${{ secrets.DOCKER_USERNAME }}
18+
password: ${{ secrets.DOCKER_PASSWORD }}
19+
- run: sbt cli/dockerBuildAndPush

auto-indexing/Dockerfile

Lines changed: 0 additions & 7 deletions
This file was deleted.

bin/lsif-java-docker-script.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
# Wrapper script for `lsif-java`, which automatically picks up the correct JVM
3+
# version. It assumes that `coursier` is available on the `$PATH` and that the
4+
# `lsif-java` binary is already installed at `/app/lsif-java/bin/lsif-java`.
5+
set -eux
6+
JVM_VERSION="8"
7+
FILE="$PWD/lsif-java.json"
8+
if test -f "$FILE"; then
9+
FROM_CONFIG=$(jq -r '.jvm' "$FILE")
10+
if [ "$FROM_CONFIG" != "null" ]; then
11+
JVM_VERSION="$FROM_CONFIG"
12+
fi
13+
fi
14+
if [ "$JVM_VERSION" = "17" ]; then
15+
JVM_VERSION="temurin:17"
16+
fi
17+
18+
echo "Using JVM version '$JVM_VERSION'"
19+
eval "$(coursier java --jvm "$JVM_VERSION" --env --jvm-index https://github.com/coursier/jvm-index/blob/master/index.json)"
20+
21+
if [ "$JVM_VERSION" = "17" ]; then
22+
export JAVA_OPTS="--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED"
23+
fi
24+
25+
/app/lsif-java/bin/lsif-java "$@"

build.sbt

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ lazy val cli = project
149149
version,
150150
sbtVersion,
151151
scalaVersion,
152+
"javacModuleOptions" -> javacModuleOptions,
152153
"semanticdbScalacVersions" ->
153154
com
154155
.sourcegraph
@@ -210,14 +211,50 @@ lazy val cli = project
210211
propsFile :: copiedJars.toList
211212
}
212213
.taskValue,
213-
nativeImageOptions ++=
214+
docker / imageNames :=
214215
List(
215-
"-H:IncludeResources=^semanticdb-.*jar$",
216-
s"-H:ReflectionConfigurationFiles=${target.value / "native-image-configs" / "reflect-config.json"}"
216+
ImageName("sourcegraph/lsif-java:latest"),
217+
ImageName(s"sourcegraph/lsif-java:${version.value}")
217218
),
218-
nativeImageOutput := (NativeImage / target).value / "lsif-java"
219+
docker / dockerfile := {
220+
val binaryDistribution = pack.value
221+
val script = (ThisBuild / baseDirectory).value / "bin" /
222+
"lsif-java-docker-script.sh"
223+
new Dockerfile {
224+
from("gradle:7.2.0-jdk8")
225+
226+
// Setup system dependencies.
227+
run("apt-get", "update")
228+
run("apt-get", "install", "--yes", "maven", "jq")
229+
230+
// Install Coursier.
231+
run(
232+
"curl",
233+
"-fLo",
234+
"/usr/local/bin/coursier",
235+
"https://git.io/coursier-cli"
236+
)
237+
run("chmod", "+x", "/usr/local/bin/coursier")
238+
239+
// Pre-download Java 8, 11 and 17.
240+
run("coursier", "java-home", "--jvm", "8")
241+
run("coursier", "java-home", "--jvm", "11")
242+
run(
243+
"coursier",
244+
"java-home",
245+
"--jvm",
246+
"temurin:17",
247+
"--jvm-index",
248+
"https://github.com/coursier/jvm-index/blob/master/index.json"
249+
)
250+
251+
// Install `lsif-java` binary.
252+
add(script, "/usr/local/bin/lsif-java")
253+
add(binaryDistribution, "/app/lsif-java")
254+
}
255+
}
219256
)
220-
.enablePlugins(NativeImagePlugin, BuildInfoPlugin)
257+
.enablePlugins(PackPlugin, DockerPlugin, BuildInfoPlugin)
221258
.dependsOn(lsif)
222259

223260
def commitAll(): Unit = {
@@ -276,26 +313,28 @@ lazy val minimized8 = project
276313
.dependsOn(agent, plugin)
277314
.disablePlugins(JavaFormatterPlugin)
278315

316+
def javacModuleOptions =
317+
List(
318+
"-J--add-exports",
319+
"-Jjdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
320+
"-J--add-exports",
321+
"-Jjdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
322+
"-J--add-exports",
323+
"-Jjdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED",
324+
"-J--add-exports",
325+
"-Jjdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
326+
"-J--add-exports",
327+
"-Jjdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED"
328+
)
329+
279330
lazy val minimized17 = project
280331
.in(file("tests/minimized/.j17"))
281332
.settings(
282333
minimizedSettings,
283334
javaToolchainJvmIndex :=
284335
Some("https://github.com/coursier/jvm-index/blob/master/index.json"),
285336
javaToolchainVersion := "temurin:17",
286-
javacOptions ++=
287-
List(
288-
"-J--add-exports",
289-
"-Jjdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
290-
"-J--add-exports",
291-
"-Jjdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
292-
"-J--add-exports",
293-
"-Jjdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED",
294-
"-J--add-exports",
295-
"-Jjdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
296-
"-J--add-exports",
297-
"-Jjdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED"
298-
)
337+
javacOptions ++= javacModuleOptions
299338
)
300339
.dependsOn(agent, plugin)
301340
.disablePlugins(JavaFormatterPlugin)
@@ -373,6 +412,7 @@ lazy val bench = project
373412
lazy val docs = project
374413
.in(file("lsif-java-docs"))
375414
.settings(
415+
moduleName := "lsif-java-docs",
376416
mdocOut :=
377417
(ThisBuild / baseDirectory).value / "website" / "target" / "docs",
378418
fork := false,

lsif-java/src/main/scala/com/sourcegraph/lsif_java/buildtools/LsifBuildTool.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,16 +494,28 @@ class LsifBuildTool(index: IndexCommand) extends BuildTool("LSIF", index) {
494494
index.app.reporter.info(line)
495495
})
496496
val javac = Paths.get(
497-
os.proc(coursier.toString, "java-home", "--jvm", config.jvm)
497+
os.proc(
498+
coursier.toString,
499+
"java-home",
500+
"--jvm",
501+
config.jvm,
502+
"--jvm-index",
503+
"https://github.com/coursier/jvm-index/blob/master/index.json"
504+
)
498505
.call()
499506
.out
500507
.trim(),
501508
"bin",
502509
"javac"
503510
)
504511
index.app.reporter.info(s"$$ $javac @$argsfile")
512+
val javacModuleOptions: Seq[String] =
513+
if (config.jvm != "8")
514+
BuildInfo.javacModuleOptions
515+
else
516+
Nil
505517
val result = os
506-
.proc(javac.toString, s"@$argsfile")
518+
.proc(javac.toString, s"@$argsfile", javacModuleOptions)
507519
.call(
508520
stdout = pipe,
509521
stderr = pipe,

project/plugins.sbt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
addSbtPlugin("org.scalameta" % "sbt-native-image" % "0.3.2")
1+
addSbtPlugin("org.xerial.sbt" % "sbt-pack" % "0.14")
2+
addSbtPlugin("se.marcuslonnberg" % "sbt-docker" % "1.8.2")
23
addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.7")
34
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.10.0")
45
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.3")

0 commit comments

Comments
 (0)