Search code examples
mavenquarkusgoogle-cloud-buildgraalvmcloudbuild.yaml

How to build quarkus native image on google cloudbuild


Does anyone has an idea how to build quarkus native image on cloudbuild? I use following command to do so:

- name: maven:3-jdk-11
    entrypoint: mvn
    args: ["package", "-Dmaven.test.skip=true", "-Pnative", "-Dquarkus.native.container-build=true", "-Dquarkus.container-image.build=true"]

Locally everything works fine, but when I try to do it on Google Cloud it throws an error:

[ERROR] Failed to execute goal io.quarkus:quarkus-maven-plugin:1.12.2.Final:build (default) on project fishki: Failed to build quarkus application: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
[ERROR]     [error]: Build step io.quarkus.deployment.pkg.steps.NativeImageBuildStep#build threw an exception: java.lang.IllegalStateException: No container runtime was found to run the native image builder
[ERROR]     at io.quarkus.deployment.pkg.steps.NativeImageBuildContainerRunner.detectContainerRuntime(NativeImageBuildContainerRunner.java:114)

My idea is to try to provide the container runtime to run the native image builder, but I have no idea how to do it.

I will appreciate any help, thanks!

EDIT:

I use following cloudbuild.yaml

steps:
  - name: maven:3-jdk-11
    entrypoint: mvn
    args: ["quarkus:add-extension", "-Dextensions=container-image-docker"]
  - name: docker:latest
  - name: maven:3-jdk-11
    entrypoint: mvn
    args: ["package", "-Pnative", "-Dmaven.test.skip=true", "-Dquarkus.container-image.build=true", "-Dquarkus.native.container-build=true", "-Dquarkus.native.container-runtime=docker"]
  - name: 'gcr.io/cloud-builders/docker'
    args: [ 'build', '-t', 'gcr.io/XXX-XX-XXX/XX-XXX', '.' ]
  - name: "gcr.io/cloud-builders/docker"
    args: ["push", "gcr.io/XXXX/XXX-XXXX"]
  - name: "gcr.io/cloud-builders/gke-deploy"
    args:
      - run
      - --filename=./deployment.yaml
      - --image=gcr.io/XXX/XXX:latest
      - --location=europe-west1-b
      - --cluster=XX-XXX-XXX-1

Now I have a new problem - when I try to install docker container runtime, I get the following error:

[ERROR] Failed to execute goal io.quarkus:quarkus-maven-plugin:1.12.2.Final:build (default) on project fishki: Failed to build quarkus application: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
[ERROR]     [error]: Build step io.quarkus.deployment.pkg.steps.NativeImageBuildStep#build threw an exception: java.lang.RuntimeException: Failed to pull builder image quay.io/quarkus/ubi-quarkus-native-image:21.0.0-java11

Solution

  • I was struggling with this for a while, but the info in the question you asked helped me a lot, so thank you for the ideas which lead me to resolve this issue.

    The way I ended up doing it was to build a custom build image like so (Dockerfile):

    FROM maven:3-jdk-11
    run apt-get update
    run apt-get install docker.io -y
    run docker --version
    

    You can find my build image here (note: mine works with gradle, not maven): https://hub.docker.com/repository/docker/lackrobin/quarkus-gradle-build-image

    Push this image to the gcr, or any other registry that you can access from google cloud build.

    In the cloudbuild.yaml file the following configuration should do the trick:

    steps:
      - name: gcr.io/[link to the builder image above]
        args:
          - addExtension
          - '--extensions=container-image-docker'
          - build
          - '-Dquarkus.package.type=native'
          - '-Dquarkus.native.native-image-xmx=16g'
          - '-Dorg.gradle.jvmargs=-Xmx3g -XX:MaxPermSize=2048m'
        entrypoint: ./gradlew
      - name: gcr.io/cloud-builders/docker
        args:
          - build
          - '-t'
          - gcr.io/[project name]/[image name]
          - '-f'
          - src/main/docker/Dockerfile.native
          - .
    images:
    timeout: 2000s
      - gcr.io/gcr.io/[project name]/[image name]
    options:
      machineType: E2_HIGHCPU_32
    

    quarkus, or GraalVM uses a lot of resources to build. I had to use a vm with more resources for the build. The command machineType: E2_HIGHCPU_32 under options is responsible for that.