首页 > 解决方案 > 在詹金斯的 JAVA_HOME 中找不到“java”可执行文件

问题描述

我正在使用 .net core 3.1,为此我创建了一个 docker 文件,其中还包含一些工具,如用于 Jenkins 的 nodejs、openjdk-11 等。此 docker 文件已被推送到 AWS ECR(docker 映像基于 linux 容器)。

最重要的是,我一直在使用 sonar-qube 和 Jenkins。虽然此图像的构建作业在 Jenkins 上运行,但出现了一些奇怪的错误:

SonarScanner for MSBuild 4.9
Using the .NET Core version of the Scanner for MSBuild
Post-processing started.
Calling the SonarQube Scanner...
Could not find 'java' executable in JAVA_HOME or PATH.
The SonarQube Scanner did not complete successfully
13:59:33.157  Post-processing failed. Exit code: 1

根据上述错误,声纳扫描仪的结束命令是停止处理。 1. 泊坞窗文件:

#
# Start from the base image
FROM mcr.microsoft.com/dotnet/core/sdk:3.1

#
# Install dependencies
RUN apt-get -yqq update
RUN apt-get -yqq install zip
RUN apt-get -yqq install curl
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash
RUN apt-get -yqq install nodejs
#RUN apt-get install -y openjdk-11-jre

RUN apt-get update && \
    apt-get install -y openjdk-11-jre && \
    rm -rf /var/lib/apt/lists/*

RUN dotnet tool install Amazon.Lambda.Tools --tool-path /usr/share/dotnet
RUN dotnet tool install dotnet-sonarscanner --tool-path /usr/share/dotnet

#
# Set required permissions
RUN chown 1004:sudo /usr/share/dotnet/dotnet-lambda
RUN chmod a+w /usr/bin
RUN chmod a+rwx -R /usr/share/dotnet/.store
RUN chmod o+x /usr/share/dotnet/dotnet-lambda
RUN chmod a+w /usr/share/dotnet/dotnet-sonarscanner


#
# Export paths
ENV PATH="${PATH}:/usr/share/dotnet"
ENV JAVA_HOME = /usr/lib/jvm/java-11-openjdk-amd64/bin/java

2.詹金斯文件:

def runSonarqube() {
    stage('run SonarQube scan') {
        sh ("dotnet tool install --global dotnet-sonarscanner")
        withEnv('PATH="$PATH:/usr/share/dotnet"'){
            sh('echo ${PATH}')
            withSonarQubeEnv('SonarQube Server') {
                sh " $HOME/.dotnet/tools/dotnet-sonarscanner begin /k:\"${projectName}\" /d:sonar.host.url=\"${sonarHostUrl}\" /d:sonar.cs.opencover.reportsPaths=\"src/Test/coverage.opencover.xml\""
                sh " dotnet build "
                sh " $HOME/.dotnet/tools/dotnet-sonarscanner end"
            }
            timeout(time: 10, unit: 'MINUTES') {
                def qg = waitForQualityGate()
                if (qg.status == 'ERROR') {
                    error "Pipeline aborted due to quality gate failure: ${qg.status}"
                }
            }
        }
    }
}

node {
    def scmVars
    try {
        stage('notify slack') {
            notifySlack()
        }
        stage('checkout') {
            scmVars = checkout scm
        }
        withAWS(region: awsRegion, credentials: awsCredentialsId) {
            sh "\$(aws ecr get-login --no-include-email --region ${awsRegion})"
            docker.withRegistry("${imageBaseUrl}", "${imageAuthenticationCredentials}") {
                sh "echo ${PATH}"
                docker.image('mcr.microsoft.com/dotnet/core/sdk:3.1').inside {

                    withEnv(['HOME=/tmp']) {
                        stage('restore project dependencies') {
                            sh 'dotnet restore iNewsConnector.Service/iNewsConnector.Service.csproj'
                        }
                        stage('build project') {
                            sh 'dotnet build iNewsConnector.Service/iNewsConnector.Service.csproj --configuration Release'
                        }
                        stage('create package') {
                            sh "dotnet publish -f netcoreapp3.1 --nologo -o ./iNewsConnector.Service/bin/Release/netcoreapp3.1/publish"
                            zip zipFile: "${artifactsDir}/${packageName}", archive: true, dir: "./iNewsConnector.Service/bin/Release/netcoreapp3.1/publish"
                        }
                        stage('archive') {
                            // Archive the zipped package for the deployment job
                            archiveArtifacts artifacts: "${artifactsDir}/${packageName},git-commit.txt", fingerprint: true
                        }

                        //if(env.BRANCH_NAME == 'develop') {
                            // Perform SonarQube analysis
                            runSonarqube()
                        //}
                    }
                }
            }
        }
        currentBuild.result = 'SUCCESS'
    } catch (InterruptedException e) {
        // Build interupted
        currentBuild.result = 'ABORTED'
        throw e
    } catch (e) {
        // If there was an exception thrown, the build failed
        currentBuild.result = 'FAILED'
        throw e
    } finally {
        // Success or failure, always send notifications
        notifySlack(currentBuild.result)
        // disableDeferredWipeout makes sure that the cleanup is deterministic
        cleanWs disableDeferredWipeout: true
    }
}

虽然我在我的 docker 映像中设置了 PATH 和 JAVA_HOME。

我为此尝试了所有解决方案,但对我没有任何作用。有人可以指导我吗?

谢谢

标签: sonarqubeasp.net-core-3.1java-home

解决方案


虽然报告的是JAVA_HOME,但实际使用的是sonar-scanner中的jre,需要授权:

chmod 755 ...sonar-scanner-4.3.0.2102-linux/jre/bin/java

推荐阅读