Utilisation d'Oracle GraalVM dans les pipelines de build DevOps

Les pipelines de build DevOps vous permettent d'utiliser Oracle GraalVM pour créer des applications Java hautes performances.

Pendant la phase de build géré, vous pouvez installer et utiliser des composants Oracle GraalVM tels que Native Image et Java Development Kit (JDK) en ajoutant une simple commande de gestionnaire de packages YUM dans votre fichier de spécification de build.

Remarque

Oracle GraalVM est disponible sur Oracle Cloud Infrastructure (OCI) sans frais supplémentaires.

Mise à jour du fichier de spécification de build

To install and use Oracle GraalVM in the DevOps build pipeline, update your build specification file as follows:

  1. Ajoutez la commande suivante pour installer les composants Oracle GraalVM requis. Par exemple, cette commande installe Native Image, JDK et d'autres dépendances requises.
    steps:
      - type: Command
        name: "Install the latest Oracle GraalVM for JDK 20 - JDK and Native Image"
        command: |
          yum -y install graalvm-20-native-image
  2. Définissez la variable d'environnement JAVA_HOME.
    env:
      variables:
        "JAVA_HOME" : "/usr/lib64/graalvm/graalvm-java20"
  3. Définissez la variable d'environnement PATH.
    env:
      variables:
        # PATH is a reserved variable and cannot be defined as a variable.
        # PATH can be changed in a build step and the change is visible in subsequent steps.
     
    steps:
      - type: Command
        name: "Set the PATH here"
        command: |
          export PATH=$JAVA_HOME/bin:$PATH

Exemple 1 :

Exemple de fichier de spécification de build permettant de créer un exécutable natif à partir d'une application Java à l'aide de Maven et d'Oracle GraalVM pour JDK 17 - Native Image.

version: 0.1
component: build
timeoutInSeconds: 600
runAs: root
shell: bash
env:
  variables:
    "JAVA_HOME" : "/usr/lib64/graalvm/graalvm-java17"
    # PATH is a reserved variable and cannot be defined as a variable.
    # However, PATH can be changed in a build step and the change is visible in subsequent steps.
steps:
  - type: Command
    name: "Install the latest Oracle GraalVM for JDK 17 - JDK and Native Image"
    command: |
      yum -y install graalvm-17-native-image
  - type: Command
    name: "Set the PATH here. JAVA_HOME already set in env > variables above."
    command: |
      export PATH=$JAVA_HOME/bin:$PATH
  - type: Command
    name: "Build a native executable with Oracle GraalVM for JDK 17 - Native Image"
    command: |
      mvn --no-transfer-progress -Pnative -DskipTests package
outputArtifacts:
  - name: app_native_executable
    type: BINARY
    location: target/my-app

Exemple 2 :

Exemple de fichier de spécification de build permettant de créer un exécutable natif à partir d'une application Micronaut à l'aide de Maven et d'Oracle GraalVM pour JDK 17 - Native Image.

version: 0.1
component: build
timeoutInSeconds: 900
runAs: root
shell: bash
env:
  variables:
    "JAVA_HOME" : "/usr/lib64/graalvm/graalvm-java17"
    TAG: "mn-hello-ni:0.0.1"
    APP_FILE: "MnHelloRest"
  exportedVariables:
    - BUILDRUN_HASH
steps:
  - type: Command
    name: "Define unique image tag"
    timeoutInSeconds: 140
    command: |
      echo "OCI_BUILD_RUN_ID: ${OCI_BUILD_RUN_ID}"
      export BUILDRUN_HASH=`echo ${OCI_BUILD_RUN_ID} | rev | cut -c 1-7`
      echo "BUILDRUN_HASH: " $BUILDRUN_HASH
  - type: Command
    name: "Install the latest Oracle GraalVM for JDK 17 - JDK and Native Image"
    command: |
      yum -y install graalvm-17-native-image
  - type: Command
    name: "Set the PATH here. JAVA_HOME already set in env > variables above."
    command: |
      export PATH=$JAVA_HOME/bin:$PATH
  - type: Command
    name: "Build a native executable with Oracle GraalVM for JDK 17 - Native Image"
    command: |
      ./mvnw --no-transfer-progress package -Dpackaging=native-image
  - type: Command
    name: "Package the native executable in a runtime container image"
    command: |
      docker build -f ./Dockerfile \
                  --build-arg APP_FILE=${APP_FILE} \
                  -t ${TAG} .
outputArtifacts:
  - name: app_native_executable
    type: BINARY
    location: target/MnHelloRest
  - name: runtime_image
    type: DOCKER_IMAGE
    location: ${TAG}

Exemple 3 :

Exemple de fichier de spécification de build permettant de créer un exécutable natif à partir d'une application Java à l'aide de Maven et de GraalVM Enterprise 22.x pour Java 17 - Native Image.

version: 0.1
component: build
timeoutInSeconds: 600
runAs: root
shell: bash
env:
  variables:
    "JAVA_HOME" : "/usr/lib64/graalvm/graalvm22-ee-java17"
    # PATH is a reserved variable and cannot be defined as a variable.
    # However, PATH can be changed in a build step and the change is visible in subsequent steps.
steps:
  - type: Command
    name: "Install the latest GraalVM Enterprise 22.x for Java 17 - JDK and Native Image"
    command: |
      yum -y install graalvm22-ee-17-native-image
  - type: Command
    name: "Set the PATH here. JAVA_HOME already set in env > variables above."
    command: |
      export PATH=$JAVA_HOME/bin:$PATH
  - type: Command
    name: "Build a native executable with the installed GraalVM Enterprise 22.x for Java 17 - Native Image"
    command: |
      mvn --no-transfer-progress -Pnative -DskipTests package
outputArtifacts:
  - name: app_native_executable
    type: BINARY
    location: target/my-app

Exemple 4 :

Exemple de fichier de spécification de build permettant de créer un exécutable natif à partir d'une application Micronaut à l'aide de Maven et de GraalVM Enterprise 22.x pour Java 17 - Native Image.

version: 0.1
component: build
timeoutInSeconds: 900
runAs: root
shell: bash
env:
  variables:
    "JAVA_HOME" : "/usr/lib64/graalvm/graalvm22-ee-java17"
    TAG: "mn-hello-ni:0.0.1"
    APP_FILE: "MnHelloRest"
  exportedVariables:
    - BUILDRUN_HASH
steps:
  - type: Command
    name: "Define unique image tag"
    timeoutInSeconds: 140
    command: |
      echo "OCI_BUILD_RUN_ID: ${OCI_BUILD_RUN_ID}"
      export BUILDRUN_HASH=`echo ${OCI_BUILD_RUN_ID} | rev | cut -c 1-7`
      echo "BUILDRUN_HASH: " $BUILDRUN_HASH
  - type: Command
    name: "Install the latest GraalVM Enterprise 22.x for Java 17 - JDK and Native Image"
    command: |
      yum -y install graalvm22-ee-17-native-image
  - type: Command
    name: "Set the PATH here. JAVA_HOME already set in env > variables above"
    command: |
      export PATH=$JAVA_HOME/bin:$PATH
  - type: Command
    name: "Build a native executable with the installed GraalVM Enterprise 22.x for Java 17 - Native Image"
    command: |
      ./mvnw --no-transfer-progress package -Dpackaging=native-image
  - type: Command
    name: "Package the native executable in a runtime container image"
    command: |
      docker build -f ./Dockerfile \
                  --build-arg APP_FILE=${APP_FILE} \
                  -t ${TAG}
outputArtifacts:
  - name: app_native_executable
    type: BINARY
    location: target/MnHelloRest
  - name: runtime_image
    type: DOCKER_IMAGE
    location: ${TAG}