【问题标题】:Jenkins java.lang.NoSuchMethodError: No such DSL method 'post' found among stepsJenkins java.lang.NoSuchMethodError:在步骤中找不到这样的 DSL 方法“post”
【发布时间】:2018-05-13 19:35:55
【问题描述】:

我正在尝试在 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: No such DSL method 'post' found between 步骤 [存档,bat,构建,catchError,结帐,deleteDir,目录, dockerFingerprintFrom、dockerFingerprintRun、echo、emailext、 emailextrecipients、envVarsForTool、错误、fileExists、getContext、 git,输入,isUnix,库,libraryResource,加载,邮件,里程碑, 节点、并行、powershell、属性、publishHTML、pwd、readFile、 readTrusted, resolveScm, retry, script, sh, sleep, stage, stash, step, svn, 超时, 时间戳, tm, 工具, 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, 分支, buildButton, buildDiscarder, 不区分大小写、区分大小写、证书、变更日志、变更集、 选择,choiceParam,cleanWs,时钟,云,命令,凭据, cron, crumb, defaultView, demand, disableConcurrentBuilds, docker, dockerCert, dockerfile, downloadSettings, 下游, 哑, envVars, 环境、表达式、文件、fileParam、filePath、指纹、 框架选项,freeStyle,freeStyleJob,fromScm,fromSource,git, github,githubPush,gradle,headRegexFilter,headWildcardFilter, 超链接,超链接模型,继承,继承全局, installSource, jacoco, jdk, jdkInstaller, jgit, jgitapache, jnlp, 作业名称、junit、标签、lastDuration、lastFailure、 lastGrantedAuthorities, lastStable, lastSuccess, legacy, legacySCM, 列表,本地,位置,logRotator,loggedInUsersCanDoAnything, masterBuild, 行家, maven3Mojos, mavenErrors, mavenMojos, mavenWarnings, modernSCM, myView, 节点, nodeProperties, nonInheriting, nonStoredPasswordParam, none, not, overrideIndexTriggers, paneStatus, 参数、密码、模式、管道模型、管道触发器、 纯文本、插件、pollSCM、projectNamingStrategy、代理、 queueItemAuthenticator,quietPeriod,remotingCLI,运行,runParam, 计划、scmRetryCount、搜索、安全、外壳、skipDefaultCheckout、 skipStagesAfterUnstable, slave, sourceRegexFilter, sourceWildcardFilter、sshUserPrivateKey、stackTrace、标准、状态、 字符串,stringParam,swapSpace,文本,textParam,tmpSpace, toolLocation, unsecured, upstream, usernameColonPassword, usernamePassword、viewsTabBar、weather、withAnt、zfs、zip] 或 globals [currentBuild, docker, env, params, pipeline, scm]

我看到其他一些帖子,但提出的建议对我不起作用

【问题讨论】:

    标签: email jenkins


    【解决方案1】:

    我在这里遇到了同样的问题。声明性的很多例子......没有脚本的例子。它几乎使您相信没有解决方案,但这没有意义。

    这对我有用(它可以在没有 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)”链接,您就会看到它。

    【讨论】:

    • 完美答案。
    【解决方案2】:

    您遇到的问题是您的节点未添加到声明性管道中,您不能在节点上使用 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}"
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      • 2018-11-11
      • 2018-01-24
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      相关资源
      最近更新 更多