Jenkins 管道错误:java.lang.NoSuchMethodError:找不到这样的 DSL 方法“withSonarQubeEnv”

Posted

技术标签:

【中文标题】Jenkins 管道错误:java.lang.NoSuchMethodError:找不到这样的 DSL 方法“withSonarQubeEnv”【英文标题】:Jenkins Pipeline Error : java.lang.NoSuchMethodError: No such DSL method 'withSonarQubeEnv' found 【发布时间】:2020-04-30 20:12:40 【问题描述】:

我正在使用声明式 jenkins 管道并尝试集成 sonarqube 步骤。但是,当我运行构建时,我收到“java.lang.NoSuchMethodError: No such DSL method 'withSonarQubeEnv' found”错误。完整的堆栈跟踪是:

java.lang.NoSuchMethodError: No such DSL method 'withSonarQubeEnv' found among steps [AddInteractivePromotion, ArtifactoryGradleBuild, ArtifactoryMavenBuild, ConanAddRemote, ConanAddUser, InitConanClient, MavenDescriptorStep, RunConanCommand, acceptGitLabMR, addGitLabMRComment, ansiColor, archive, artifactoryDistributeBuild, artifactoryDownload, artifactoryPromoteBuild, artifactoryUpload, bat, build, catchError, checkout, collectEnv, deleteDir, deployArtifacts, dir, dockerFingerprintFrom, dockerFingerprintRun, dockerPullStep, dockerPushStep, echo, envVarsForTool, error, fileExists, findFiles, getArtifactoryServer, getContext, git, gitlabBuilds, gitlabCommitStatus, input, isUnix, jiraAddComment, jiraAddWatcher, jiraAssignIssue, jiraAssignableUserSearch, jiraDeleteIssueLink, jiraDeleteIssueRemoteLink, jiraDeleteIssueRemoteLinks, jiraEditComment, jiraEditComponent, jiraEditIssue, jiraEditVersion, jiraGetComment, jiraGetComments, jiraGetComponent, jiraGetComponentIssueCount, jiraGetFields, jiraGetIssue, jiraGetIssueLink, jiraGetIssueLinkTypes, jiraGetIssueRemoteLink, jiraGetIssueRemoteLinks, jiraGetIssueTransitions, jiraGetIssueWatches, jiraGetProject, jiraGetProjectComponents, jiraGetProjectStatuses, jiraGetProjectVersions, jiraGetProjects, jiraGetVersion, jiraJqlSearch, jiraLinkIssues, jiraNewComponent, jiraNewIssue, jiraNewIssueRemoteLink, jiraNewIssues, jiraNewVersion, jiraNotifyIssue, jiraTransitionIssue, jiraUserSearch, junit, library, libraryResource, load, mail, milestone, newArtifactoryServer, newBuildInfo, newGradleBuild, newMavenBuild, node, parallel, powershell, properties, publishBuildInfo, pwd, readFile, readJSON, readManifest, readMavenPom, readProperties, readTrusted, readYaml, resolveScm, retry, script, sh, sleep, sshagent, stage, stash, step, svn, synopsys_detect, timeout, tm, tool, touch, unarchive, unstash, unzip, updateGitlabCommitStatus, validateDeclarativePipeline, waitUntil, withContext, withCredentials, withDockerContainer, withDockerRegistry, withDockerServer, withEnv, wrap, writeFile, writeJSON, writeMavenPom, writeYaml, ws, xrayScanBuild, zip] or symbols [all, allOf, always, ant, antFromApache, antOutcome, antTarget, any, anyOf, apiToken, architecture, archiveArtifacts, artifactManager, authorizationMatrix, batchFile, booleanParam, branch, buildButton, buildDiscarder, caseInsensitive, caseSensitive, certificate, changelog, changeset, choice, choiceParam, clock, cloud, command, configFile, configFileProvider, credentials, cron, crumb, defaultView, demand, deploy, disableConcurrentBuilds, disableResume, docker, dockerCert, dockerfile, downloadSettings, downstream, dumb, durabilityHint, envVars, environment, expression, file, fileParam, filePath, fingerprint, frameOptions, freeStyle, freeStyleJob, fromScm, fromSource, git, gitBranchDiscovery, gitLabConnection, gitTagDiscovery, gitlab, glassfish2, glassfish3, glassfish4, gradle, headRegexFilter, headWildcardFilter, hyperlink, hyperlinkToModels, inheriting, inheritingGlobal, installSource, jacoco, jboss3, jboss4, jboss5, jboss6, jboss7, jdk, jdkInstaller, jgit, jgitapache, jnlp, jobName, label, lastDuration, lastFailure, lastGrantedAuthorities, lastStable, lastSuccess, legacy, legacySCM, list, local, location, logRotator, loggedInUsersCanDoAnything, masterBuild, maven, maven3Mojos, mavenErrors, mavenMojos, mavenWarnings, modernSCM, myView, node, nodeProperties, nodejs, nodejsci, nonInheriting, none, not, overrideIndexTriggers, paneStatus, parameters, password, pattern, permanent, pipeline-model, pipelineTriggers, plainText, plugin, pollSCM, projectNamingStrategy, proxy, queueItemAuthenticator, quietPeriod, run, runParam, schedule, scmRetryCount, scriptApproval, scriptApprovalLink, search, security, shell, skipDefaultCheckout, skipStagesAfterUnstable, slave, sourceRegexFilter, sourceWildcardFilter, sshUserPrivateKey, stackTrace, standard, status, string, stringParam, swapSpace, text, textParam, tmpSpace, tomcat4, tomcat5, tomcat6, tomcat7, tomcat8, tomcat9, toolLocation, unsecured, upstream, userSeed, usernameColonPassword, usernamePassword, veracode, veracodeDynamicAnalysisResubmit, veracodeDynamicAnalysisReview, veracodeDynamicRescan, viewsTabBar, weather, withAnt, zfs, zip] or globals [Artifactory, currentBuild, docker, env, params, pipeline, scm]
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:202)
    at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
    at sun.reflect.GeneratedMethodAccessor3478.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:160)
    at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:157)
    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:158)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:162)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:132)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
    at WorkflowScript.run(WorkflowScript:104)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.delegateAndExecute(ModelInterpreter.groovy:136)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(ModelInterpreter.groovy:488)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(ModelInterpreter.groovy:258)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(ModelInterpreter.groovy:256)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(ModelInterpreter.groovy:487)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:214)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.toolsBlock(ModelInterpreter.groovy:382)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
    at sun.reflect.GeneratedMethodAccessor478.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
    at com.cloudbees.groovy.cps.impl.ClosureBlock.eval(ClosureBlock.java:46)
    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:186)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:370)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:93)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:282)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:270)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

詹金斯文件 -

stage("build & SonarQube analysis") 
            environment 
                scannerHome = tool 'SonarQubeScanner'
            
            steps 
                withSonarQubeEnv('sonarqube') 
                    sh "$scannerHome/bin/sonar-scanner"
                
            
        
Jenkins version : 2.190.1 <br/>
SonarQube Version : 7.7 <br/>
SonarQube Scanner : 2.9.0 <br/>

我尝试了各种选择,但都没有运气。任何帮助将不胜感激。

【问题讨论】:

用法和语法似乎正确,并且您声称已安装插件,因此这可能是错误或感知环境与实际环境之间的脱节。 感谢马特的评论。我也很惊讶为什么会出现这个错误。这是我执行管道的实际环境,所以我相信这是一些错误。我试图弄清楚是否可以通过其他方式解决此问题。 【参考方案1】:

通过更新 Jenkins 中的 sonar-scanner 版本解决了问题。

【讨论】:

【参考方案2】:
I suppose the DSL method you are using might be fault, you need to use 

    job('example') 
        publishers 
            sonar 
                branch('feature-xy')
                overrideTriggers 
                    skipIfEnvironmentVariable('SKIP_SONAR')
                
            
        

【讨论】:

以上是关于Jenkins 管道错误:java.lang.NoSuchMethodError:找不到这样的 DSL 方法“withSonarQubeEnv”的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 Jenkins 管道在本地部署 mule 4 应用程序

在 Jenkins 声明式管道中使用 waitForQualityGate

声纳质量门和电子邮件 Jenkins 管道

序言中不允许内容,但序言在运行时可以吗? Groovy,Jenkins,Java,管道,XML [重复]

使用 Jenkins 管道上的 AWS cli “期望用双引号括起来的属性名称”

如何在Openshift平台上通过Jenkins管道运行docker-in-docker?