You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过Helm部署的Jenkins JDK配置异常致Gradle构建失败

问题描述

通过Helm安装最新版Jenkins,GitLab集成与Git Webhook功能正常,但Gradle构建因JDK版本不兼容失败。已在Jenkins主Pod中安装Adoptium JDK17,系统配置默认JDK为17且路径正确,任务也配置使用默认JDK;但构建控制台显示实际运行环境为JDK11,JAVA_HOME指向JDK17,最终因Spring Boot Gradle插件3.1.4需要JDK17适配导致构建失败。

完整控制台输出:

Running as SYSTEM
Agent default-1sxpg is provisioned from template default
---
apiVersion: "v1"
kind: "Pod"
metadata:
  labels:
    jenkins/jenkins-jenkins-agent: "true"
    jenkins/label-digest: "500b4f18aee87616849e4f4c2435020898e34aa0"
    jenkins/label: "jenkins-jenkins-agent"
  name: "default-1sxpg"
  namespace: "jenkins"
spec:
  containers:
  - args:
    - "********"
    - "default-1sxpg"
    env:
    - name: "JENKINS_SECRET"
      value: "********"
    - name: "JENKINS_TUNNEL"
      value: "jenkins-agent.jenkins.svc.cluster.local:50000"
    - name: "JENKINS_AGENT_NAME"
      value: "default-1sxpg"
    - name: "JENKINS_NAME"
      value: "default-1sxpg"
    - name: "JENKINS_AGENT_WORKDIR"
      value: "/home/jenkins/agent"
    - name: "JENKINS_URL"
      value: "http://jenkins.jenkins.svc.cluster.local:80/"
    image: "jenkins/inbound-agent:3107.v665000b_51092-15"
    imagePullPolicy: "IfNotPresent"
    name: "jnlp"
    resources:
      limits:
        memory: "512Mi"
        cpu: "512m"
      requests:
        memory: "512Mi"
        cpu: "512m"
    tty: false
    volumeMounts:
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
    workingDir: "/home/jenkins/agent"
  nodeSelector:
    kubernetes.io/os: "linux"
  restartPolicy: "Never"
  serviceAccountName: "default"
  volumes:
  - emptyDir:
      medium: ""
    name: "workspace-volume"

Building remotely on default-1sxpg (jenkins-jenkins-agent) in workspace /home/jenkins/agent/workspace/USER TEST
The recommended git tool is: NONE
using credential jenkins_gitlab_userid
Cloning the remote Git repository
Cloning repository http://gitlab.em2study.com/em2/user.git
 > git init /home/jenkins/agent/workspace/USER TEST # timeout=10
Fetching upstream changes from http://gitlab.em2study.com/em2/user.git
 > git --version # timeout=10
 > git --version # 'git version 2.30.2'
using GIT_ASKPASS to set credentials jenkins gitlab userid
 > git fetch --tags --force --progress -- http://gitlab.em2study.com/em2/user.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url http://gitlab.em2study.com/em2/user.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
 > git rev-parse refs/remotes/origin/main^{commit} # timeout=10
Checking out Revision 953356eb86a78f8258a8cb8a7484511222107e3f (refs/remotes/origin/main)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 953356eb86a78f8258a8cb8a7484511222107e3f # timeout=10
Commit message: "123"
 > git rev-list --no-walk 953356eb86a78f8258a8cb8a7484511222107e3f # timeout=10
[USER TEST] $ /bin/sh -xe /tmp/jenkins6040822211157154585.sh
+ java --version
openjdk 11.0.19 2023-04-18
OpenJDK Runtime Environment Temurin-11.0.19+7 (build 11.0.19+7)
OpenJDK 64-Bit Server VM Temurin-11.0.19+7 (build 11.0.19+7, mixed mode)
+ echo /opt/java/openjdk
/opt/java/openjdk
[Gradle] - Launching build.
Unpacking https://services.gradle.org/distributions/gradle-8.5-bin.zip to /home/jenkins/agent/tools/hudson.plugins.gradle.GradleInstallation/gradle_gitlab on default-1sxpg
[USER TEST] $ /home/jenkins/agent/tools/hudson.plugins.gradle.GradleInstallation/gradle_gitlab/bin/gradle clean build

Welcome to Gradle 8.5!

Here are the highlights of this release:
 - Support for running on Java 21
 - Faster first use with Kotlin DSL
 - Improved error and warning messages

For more details see https://docs.gradle.org/8.5/release-notes.html

Starting a Gradle Daemon (subsequent builds will be faster)

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'user'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.1.4.
     Required by:
         project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.1.4
      > No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.1.4 was found. The consumer was configured to find a library for use during runtime, compatible with Java 11, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.5' but:
          - Variant 'apiElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.4 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'javadocElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.4 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 11)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'mavenOptionalApiElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.1.4 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'mavenOptionalRuntimeElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.1.4 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'runtimeElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.4 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'sourcesElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.4 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 11)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 47s
Build step 'Invoke Gradle script' changed build result to FAILURE
Build step 'Invoke Gradle script' marked build as failure
Finished: FAILURE

原因分析

  • Jenkins主节点安装的JDK17不会被Kubernetes代理节点使用,构建任务实际运行在代理Pod中,而代理Pod使用的jenkins/inbound-agent:3107.v665000b_51092-15镜像默认自带JDK11,这是控制台显示Java版本为11的根本原因。
  • JAVA_HOME指向JDK17是Jenkins主节点传递的环境变量,但代理节点中该路径下实际是JDK11(镜像默认JDK路径为/opt/java/openjdk),导致环境变量与实际运行的Java版本不匹配。

解决方案

方案1:自定义Jenkins代理镜像(推荐)

  1. 创建Dockerfile,基于官方inbound-agent镜像安装Adoptium JDK17:
FROM jenkins/inbound-agent:3107.v665000b_51092-15

USER root
# 安装Adoptium JDK17
RUN apt-get update && apt-get install -y wget && \
    wget -O /tmp/openjdk17.tar.gz https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.9%2B9/OpenJDK17U-jdk_x64_linux_hotspot_17.0.9_9.tar.gz && \
    mkdir -p /opt/java/jdk17 && \
    tar -xzf /tmp/openjdk17.tar.gz -C /opt/java/jdk17 --strip-components=1 && \
    rm /tmp/openjdk17.tar.gz && \
    apt-get remove -y wget && apt-get clean && rm -rf /var/lib/apt/lists/*

# 设置默认JDK为17
RUN update-alternatives --install /usr/bin/java java /opt/java/jdk17/bin/java 1 && \
    update-alternatives --install /usr/bin/javac javac /opt/java/jdk17/bin/javac 1 && \
    update-alternatives --set java /opt/java/jdk17/bin/java && \
    update-alternatives --set javac /opt/java/jdk17/bin/javac

USER jenkins
ENV JAVA_HOME=/opt/java/jdk17
  1. 构建并推送镜像到你的容器仓库:
docker build -t your-registry/jenkins-inbound-agent-jdk17:3107.v665000b_51092-15 .
docker push your-registry/jenkins-inbound-agent-jdk17:3107.v665000b_51092-15
  1. 修改Jenkins Kubernetes代理模板:
    • 进入Manage Jenkins > Manage Nodes and Clouds > Configure Clouds
    • 找到Kubernetes云配置,编辑默认代理模板,将镜像替换为自定义镜像地址

方案2:在代理Pod启动时安装JDK17(临时方案)

在代理模板的Pod Templates > Containers > jnlp中配置启动前命令,临时安装JDK17:

  1. 添加环境变量:JAVA_HOME=/opt/java/jdk17
  2. 在Commands to run before container starts中添加:
apt-get update && apt-get install -y wget && \
wget -O /tmp/openjdk17.tar.gz https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.9%2B9/OpenJDK17U-jdk_x64_linux_hotspot_17.0.9_9.tar.gz && \
mkdir -p /opt/java/jdk17 && \
tar -xzf /tmp/openjdk17.tar.gz -C /opt/java/jdk17 --strip-components=1 && \
rm /tmp/openjdk17.tar.gz && \
apt-get remove -y wget && apt-get clean && rm -rf /var/lib/apt/lists/* && \
update-alternatives --install /usr/bin/java java /opt/java/jdk17/bin/java 1 && \
update-alternatives --install /usr/bin/javac javac /opt/java/jdk17/bin/javac 1 && \
update-alternatives --set java /opt/java/jdk17/bin/java && \
update-alternatives --set javac /opt/java/jdk17/bin/javac

注意:此方案每次启动代理Pod都会重新安装JDK,增加构建启动时间,仅适合临时测试。

补充:Gradle项目配置验证

确保项目build.gradle指定正确的Java版本:

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.1.4'
    id 'io.spring.dependency-management' version '1.1.3'
}

java {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
}

内容的提问来源于stack exchange,提问作者이남웅

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 12:34:53