Skip to content

Commit

Permalink
Fix graalvm download url (#1041)
Browse files Browse the repository at this point in the history
* Fix GraalVM download URL

This is the Gradle's counterpart to micronaut-projects/micronaut-maven-plugin#1219

The fix is slightly different since we already had a way to define the base path, but not the full path.
Now the plugin provides the option to completely override the GraalVM download URL, in case the derivation
from the JDK and arch properties is not good enough.

* Update Amazon Linux base image
  • Loading branch information
melix authored Oct 22, 2024
1 parent eb9d2b3 commit fe4b58e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
*/
public abstract class NativeImageDockerfile extends Dockerfile implements DockerBuildOptions {

public static final String AMAZON_LINUX_BASE_IMAGE = "public.ecr.aws/amazonlinux/amazonlinux:" + DefaultVersions.AMAZONLINUX;

private static final List<Integer> SUPPORTED_JAVA_VERSIONS = List.of(
// keep those in descending order
21,
Expand All @@ -65,6 +67,11 @@ public abstract class NativeImageDockerfile extends Dockerfile implements Docker
private static final String ARM_ARCH = "aarch64";
private static final String X86_64_ARCH = "x64";

private static final String GRAALVM_DOWNLOAD_BASE_URL = "https://download.oracle.com/graalvm";
private static final String GRAALVM_DISTRIBUTION_PATH = "/%s/%s/graalvm-jdk-%s_linux-%s_bin.tar.gz";
//Latest version of GraalVM for JDK 17 available under the GraalVM Free Terms and Conditions (GFTC) licence
private static final String GRAALVM_FOR_JDK17 = "17.0.12";

/**
* @return The JDK version to use with native image. Defaults to the toolchain version, or the current Java version.
*/
Expand Down Expand Up @@ -132,6 +139,17 @@ public abstract class NativeImageDockerfile extends Dockerfile implements Docker
@Optional
public abstract Property<String> getGraalReleasesUrl();

/**
* Return the full URL of the GraalVM distribution to use. By default, it
* uses the base URL from {@link #getGraalReleasesUrl()} and searches in a
* location known to exist when the plugin was built.
*
* @return the URL of the GraalVM distribution to use
*/
@Input
@Optional
public abstract Property<String> getGraalVMDistributionUrl();

@Input
@Override
public abstract Property<String> getTargetWorkingDirectory();
Expand Down Expand Up @@ -188,6 +206,17 @@ public NativeImageDockerfile() {
getTargetWorkingDirectory().convention(DEFAULT_WORKING_DIR);
getExposedPorts().convention(Collections.singletonList(8080));
getGraalImage().convention(getJdkVersion().map(NativeImageDockerfile::toGraalVMBaseImageName));
getGraalReleasesUrl().convention(GRAALVM_DOWNLOAD_BASE_URL);
var distributionPath = getJdkVersion().zip(getGraalArch(), (jdk, arch) -> {
if ("17".equals(jdk)) {
getLogger().warn("You are using the latest release of GraalVM available under the GraalVM Free Terms and Conditions (GFTC) licence (" + GRAALVM_FOR_JDK17 + "). Consider upgrading to Java 21.");
return GRAALVM_DISTRIBUTION_PATH.formatted(jdk, "archive", GRAALVM_FOR_JDK17, arch);
}
return GRAALVM_DISTRIBUTION_PATH.formatted(jdk, "latest", jdk, arch);
});
getGraalVMDistributionUrl().convention(
getGraalReleasesUrl().zip(distributionPath, (base, path) -> base + path)
);
getNativeImageOptions().convention(project
.getTasks()
.named(NativeImagePlugin.NATIVE_COMPILE_TASK_NAME, BuildNativeImageTask.class)
Expand Down Expand Up @@ -431,13 +460,13 @@ private void setupInstructions(List<Instruction> additionalInstructions) {
if (buildStrategy == DockerBuildStrategy.LAMBDA) {
from(new From(imageResolver.resolve()).withStage("graalvm"));
environmentVariable("LANG", "en_US.UTF-8");
runCommand("yum install -y gcc gcc-c++ glibc-devel glibc-langpack-en curl-minimal bash zlib zlib-devel zlib-static zip tar gzip");
runCommand("dnf update -y && dnf install -y gcc glibc-devel zlib-devel libstdc++-static tar && dnf clean all && rm -rf /var/cache/dnf");
String jdkVersion = getJdkVersion().get();
String graalArch = getGraalArch().get();
// https://download.oracle.com/graalvm/17/latest/graalvm-jdk-17_linux-aarch64_bin.tar.gz
String fileName = "graalvm-jdk-" + jdkVersion + "_linux-" + graalArch + "_bin.tar.gz";
String releasesUrl = getGraalReleasesUrl().getOrElse("https://download.oracle.com/graalvm");
runCommand("curl -4 -L " + releasesUrl + "/" + jdkVersion + "/latest/" + fileName + " -o /tmp/" + fileName);
String graalvmDistributionUrl = getGraalVMDistributionUrl().get();
runCommand("curl -4 -L " + graalvmDistributionUrl + " -o /tmp/" + fileName);
runCommand("tar -zxf /tmp/" + fileName + " -C /tmp && ls -d /tmp/graalvm-jdk-"+ jdkVersion + "* | grep -v \"tar.gz\" | xargs -I_ mv _ /usr/lib/graalvm");
runCommand("rm -rf /tmp/*");
if (toMajorVersion(jdkVersion) < 21) {
Expand All @@ -448,6 +477,7 @@ private void setupInstructions(List<Instruction> additionalInstructions) {
defaultCommand("/usr/lib/graalvm/bin/native-image");
environmentVariable("PATH", "/usr/lib/graalvm/bin:${PATH}");
from(new From("graalvm").withStage("builder"));
runCommand("dnf update -y && dnf install -y zip && dnf clean all");
} else {
from(new From(getGraalImage().get()).withStage("graalvm"));
}
Expand Down Expand Up @@ -504,7 +534,7 @@ private void setupInstructions(List<Instruction> additionalInstructions) {
case LAMBDA:
from(baseImageProvider);
workingDir("/function");
runCommand("yum install -y zip");
runCommand("dnf install -y zip");
copyFile(new CopyFile(workDir + "/application", "/function/func").withStage("builder"));
String funcCmd = String.join(" ", getArgs().map(strings -> {
List<String> newList = new ArrayList<>(strings.size() + 1);
Expand Down Expand Up @@ -744,7 +774,7 @@ private String resolve() {
String baseImage = getBaseImage().getOrNull();

if (strategy == DockerBuildStrategy.LAMBDA && baseImage == null) {
baseImage = "amazonlinux:2023";
baseImage = AMAZON_LINUX_BASE_IMAGE;
} else if (baseImage == null) {
baseImage = "cgr.dev/chainguard/wolfi-base:latest";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DockerNativeFunctionalTest extends AbstractEagerConfiguringFunctionalTest
@Lazy
String defaultDockerFrom = "FROM $defaultBaseImage"

def "test build docker native image for runtime #runtime"() {
def "test build docker native image for runtime #runtime (JDK #jdk)"() {
given:
settingsFile << "rootProject.name = 'hello-world'"
println settingsFile.text
Expand Down Expand Up @@ -45,6 +45,7 @@ class DockerNativeFunctionalTest extends AbstractEagerConfiguringFunctionalTest
dockerfileNative {
args('-Xmx64m')
instruction \"\"\"HEALTHCHECK CMD curl -s localhost:8090/health | grep '"status":"UP"'\"\"\"
jdkVersion = "$jdk"
}
graalvmNative.binaries.all {
Expand Down Expand Up @@ -101,10 +102,11 @@ micronaut:
task.outcome == TaskOutcome.SUCCESS

where:
runtime | nativeImage
"netty" | "FROM ghcr.io/graalvm/native-image-community:17-ol${DefaultVersions.ORACLELINUX}"
"lambda_provided" | 'FROM amazonlinux:2023 AS graalvm'
"jetty" | "FROM ghcr.io/graalvm/native-image-community:17-ol${DefaultVersions.ORACLELINUX}"
runtime | jdk | nativeImage
"netty" | 17 | "FROM ghcr.io/graalvm/native-image-community:17-ol${DefaultVersions.ORACLELINUX}"
"lambda_provided" | 17 | "FROM public.ecr.aws/amazonlinux/amazonlinux:${DefaultVersions.AMAZONLINUX} AS graalvm"
"lambda_provided" | 21 | "FROM public.ecr.aws/amazonlinux/amazonlinux:${DefaultVersions.AMAZONLINUX} AS graalvm"
"jetty" | 17 | "FROM ghcr.io/graalvm/native-image-community:17-ol${DefaultVersions.ORACLELINUX}"
}

void 'build mostly static native images when using distroless docker image for runtime=#runtime'() {
Expand Down Expand Up @@ -649,7 +651,7 @@ micronaut:
build "dockerfileNative"
def dockerFile = normalizeLineEndings(file("build/docker/native-main/DockerfileNative").text)
dockerFile = dockerFile.replaceAll("[0-9]\\.[0-9]+\\.[0-9]+", "4.0.0")
.trim()
.trim()

then:
dockerFile == """
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ shadow = "8.1.1"
groovy = "3.0.22"
spock = "2.3-groovy-3.0"
oraclelinux = "9"
amazonlinux = "2023-minimal"
graalvmPlugin = "0.10.3"
mockserver = "5.15.0"
log4j2 = "2.24.1"
Expand Down
1 change: 1 addition & 0 deletions minimal-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var writeVersions = tasks.register("writeDefaultVersions", WriteVersions) {
versions.put('test-resources', libs.versions.micronaut.testresources)
versions.put('openapi', libs.versions.micronaut.openapi)
versions.put('oraclelinux', libs.versions.oraclelinux)
versions.put('amazonlinux', libs.versions.amazonlinux)
packageName = 'io.micronaut.gradle'
}

Expand Down

0 comments on commit fe4b58e

Please sign in to comment.