Jenkins java.lang.NoSuchMethodError:没有这样的DSL方法' post'在步骤

时间:2017-11-30 00:04:48

标签: email jenkins

我试图在jenkins上实施一个舞台,以便在jenkins上发生失败时发送电子邮件。我做了一些与jenkins文档类似的东西:

#!/usr/bin/env groovy

node {

stage ('Send Email') {

        echo 'Send Email'

            post {
                failure {
                    mail to: 'aa@bb.cc',
                         subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
                         body: "Something is wrong with ${env.BUILD_URL}"
                }
            }

        }

}

但我总是得到这个错误:

  

java.lang.NoSuchMethodError:没有这样的DSL方法' post'发现之中   步骤[archive,bat,build,catchError,checkout,deleteDir,dir,   dockerFingerprintFrom,dockerFingerprintRun,echo,emailext,   emailextrecipients,envVarsForTool,error,fileExists,getContext,   git,input,isUnix,library,libraryResource,load,mail,milestone,   node,parallel,powershell,properties,publishHTML,pwd,readFile,   readTrusted,resolveScm,retry,script,sh,sleep,stage,stash,step,   svn,timeout,timestamps,tm,tool,unarchive,unstash,   validateDeclarativePipeline,waitUntil,withContext,withCredentials,   withDockerContainer,withDockerRegistry,withDockerServer,withEnv,   wrap,writeFile,ws]或符号[all,allOf,always,ant,   antFromApache,antOutcome,antTarget,any,anyOf,apiToken,   架构,archiveArtifacts,artifactManager,authorizationMatrix,   batchFile,booleanParam,branch,buildButton,buildDiscarder,   caseInsensitive,caseSensitive,certificate,changelog,changeset,   选择,choiceParam,cleanWs,时钟,云,命令,凭据,   cron,crumb,defaultView,demand,disableConcurrentBuilds,docker,   dockerCert,dockerfile,downloadSettings,downstream,dumb,envVars,   环境,表达,文件,fileParam,filePath,指纹,   frameOptions,freeStyle,freeStyleJob,fromScm,fromSource,git,   github,githubPush,gradle,headRegexFilter,headWildcardFilter,   超链接,hyperlinkToModels,继承,继承全局,   installSource,jacoco,jdk,jdkInstaller,jgit,jgitapache,jnlp,   jobName,junit,label,lastDuration,lastFailure,   lastGrantedAuthorities,lastStable,lastSuccess,legacy,legacySCM,   list,local,location,logRotator,loggedInUsersCanDoAnything,   masterBuild,maven,maven3Mojos,mavenErrors,mavenMojos,   mavenWarnings,modernSCM,myView,node,nodeProperties,nonInheriting,   nonStoredPasswordParam,none,not,overrideIndexTriggers,paneStatus,   参数,密码,模式,管道模型,pipelineTriggers,   plainText,plugin,pollSCM,projectNamingStrategy,proxy,   queueItemAuthenticator,quietPeriod,remotingCLI,run,runParam,   schedule,scmRetryCount,search,security,shell,skipDefaultCheckout,   skipStagesAfterUnstable,slave,sourceRegexFilter,   sourceWildcardFilter,sshUserPrivateKey,stackTrace,standard,status,   string,stringParam,swapSpace,text,textParam,tmpSpace,   toolLocation,unsecured,upstream,usernameColonPassword,   usernamePassword,viewsTabBar,weather,withAnt,zfs,zip]或globals   [currentBuild,docker,env,params,pipeline,scm]

我看到其他人发帖了,但提出的建议对我不起作用

2 个答案:

答案 0 :(得分:7)

我在这里遇到同样的问题。很多用于声明的示例...没有用于脚本化的示例。它几乎让你相信没有解决方案,但这没有意义。

这对我有用(没有try / finally也可以 - 或者如果你想要捕获的话)。

node {
    //some var declarations... or whatever

    try {
        //do some stuff, run your tests, etc.            
    } finally {
        junit 'build/test-results/test/*.xml'
    }
}

*编辑:看看their documentation ......我不小心完成了他们的推荐。只需单击“Toggle Scripted Pipeline(Advanced)”链接,您就会看到它。

答案 1 :(得分:1)

您遇到的问题是您的节点未添加到声明性管道,您不能在节点上使用post。您需要使用声明性管道包装您的节点。

这是示例代码

pipeline {
    agent any
    stages {
        stage('Send Email') {
            steps {
            node ('master'){
                echo 'Send Email'
            }
        }
        }
    }
    post { 
        always { 
            echo 'I will always say Hello!'
        }
        aborted {
            echo 'I was aborted'
        }
        failure {
            mail to: 'aa@bb.cc',
            subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
            body: "Something is wrong with ${env.BUILD_URL}"
        }
    }
}