Gitlab CI/CD 以 docker:stable 作为基础镜像,安装 JDK 但 /bin/sh: eval: line 155: java: not found on java --version

问题描述 投票:0回答:1

我想调用 java 命令作为 Gitlab 管道的一部分。

管道看起来像:

build:
  image: docker:stable
  stage: build
  script:
    - wget https://download.oracle.com/graalvm/23/latest/graalvm-jdk-23_linux-x64_bin.tar.gz
    - tar -xvf graalvm-jdk-23_linux-x64_bin.tar.gz
    - mv graalvm-jdk-23.0.1+11.1 /opt/
    - JAVA_HOME='/opt/graalvm-jdk-23.0.1+11.1'
    - PATH="$JAVA_HOME/bin:$PATH"
    - echo $PATH
    - export PATH
    - java --version
# // later here will download maven, compile a graalvm native image, and docker push it
  variables:
    DOCKER_HOST: tcp://docker:2376
    DOCKER_TLS_CERTDIR: "/certs"
    DOCKER_TLS_VERIFY: 1
    DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client"
  services:
    - docker:dind

但是,当尝试执行java命令时,我得到:

/bin/sh: eval: line 155: java: not found

我尝试移动文件夹,chmod 777,提供相对、绝对路径,但总是得到

/bin/sh: eval: line 155: java: not found

我期待这一步,调用 java --version 能够成功。

如何正确调用java命令?

java gitlab gitlab-ci graalvm graalvm-native-image
1个回答
0
投票

我建议直接使用 graalvm 镜像而不是 slim docker 镜像

graalvm 和 sdkman

下载页面已经有容器选项,您可以在下面看到两个使用maven工作的图像

variables:
  DOCKER_HOST: tcp://docker:2376
  DOCKER_TLS_CERTDIR: "/certs"
  DOCKER_TLS_VERIFY: 1
  DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client"

services:
  - docker:dind

stages:
  - build

build-with-native-image:
  stage: build
  image: container-registry.oracle.com/graalvm/native-image:23
  script:
    - microdnf install unzip zip findutils
    - java --version
    - native-image --version
    - curl -s "https://get.sdkman.io" | bash
    - source "$HOME/.sdkman/bin/sdkman-init.sh"
    - sdk install maven
    - mvn package

# Job using the JDK container
build-with-jdk:
  stage: build
  image: container-registry.oracle.com/graalvm/jdk:23
  script:
    - microdnf install unzip zip findutils
    - java --version
    - curl -s "https://get.sdkman.io" | bash
    - source "$HOME/.sdkman/bin/sdkman-init.sh"
    - sdk install maven
    - mvn test

运行示例此处

docker 镜像

yml中的问题是使用了

docker:stable
,它被从所有内容中剥离,甚至容器中不存在glibc,所以下面你可以看到基于debian的docker镜像并使用脚本执行进一步的步骤

build:
  image: debian:bullseye-slim
  stage: build
  script:
    - apt-get update && apt-get install -y wget tar
    - wget https://download.oracle.com/graalvm/23/latest/graalvm-jdk-23_linux-x64_bin.tar.gz
    - tar -xvf graalvm-jdk-23_linux-x64_bin.tar.gz
    - mv graalvm-jdk-23.0.1+11.1 /opt/
    - JAVA_HOME=/opt/graalvm-jdk-23.0.1+11.1
    - export PATH="$JAVA_HOME/bin:$PATH"
    - /opt/graalvm-jdk-23.0.1+11.1/bin/java --version
    - java --version
# // later here will download maven, compile a graalvm native image, and docker push it
  variables:
    DOCKER_HOST: tcp://docker:2376
    DOCKER_TLS_CERTDIR: "/certs"
    DOCKER_TLS_VERIFY: 1
    DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client"
  services:
    - docker:dind

工作示例这里

© www.soinside.com 2019 - 2024. All rights reserved.