Groovy:这里没有预期的方法定义

时间:2016-11-29 16:17:48

标签: groovy jenkins-pipeline slack

我确信这是一个简单的修复,但我是一般编程和特别是Groovy的新手。

我正在尝试按照this guide在Jenkins 2管道作业中启用松弛通知,但粘贴和自定义最终代码块会给我以下错误;

Method definition not expected here. Please define the method at an appropriate place or perhaps try using a block/Closure instead.

这是我现在的代码,虽然为了安全起见我已经替换了几位。错误发生在def notifyBuild(String buildStatus = 'STARTED') {行。

node('on-demand-t2large'){
    stage ('Checkout') {
        checkout( checkout stuff here )
    }

    stage ('Build') {
        try {
            notifyBuild('STARTED')

            dir("place") {
                sh 'script name'
            } 
        }
        catch (e) {
            // If there was an exception thrown, the build failed
            currentBuild.result = "FAILED"
            throw e
        } 
        finally {
            // Success or failure, always send notifications
            notifyBuild(currentBuild.result)
        }

        def notifyBuild(String buildStatus = 'STARTED') {
            // build status of null means successful
            buildStatus =  buildStatus ?: 'SUCCESSFUL'

            // Default values
            def colorName = 'RED'
            def colorCode = '#FF0000'
            def subject = "${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
            def summary = "${subject} (${env.BUILD_URL})"
            def details = """<p>STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
            <p>Check console output at &QUOT;<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>&QUOT;</p>"""

            // Override default values based on build status
            if (buildStatus == 'STARTED') {
                color = 'YELLOW'
                colorCode = '#FFFF00'
            } else if (buildStatus == 'SUCCESSFUL') {
                color = 'GREEN'
                colorCode = '#00FF00'
            } else {
                color = 'RED'
                colorCode = '#FF0000'
            }

            // Send notifications
            slackSend (channel: '@me', color: colorCode, message: summary)
        }
    } 
}

我从一些谷歌搜索中了解到,groovy不接受内部类定义,但是我和编程一样新,我不知道如何修复它。任何帮助(有解释,所以我可以学习!)将不胜感激。

2 个答案:

答案 0 :(得分:18)

notifyBuild()方法移到nodestage层次结构之外,如下所示:

node('on-demand-t2large'){
    stage ('Checkout') {
        checkout( checkout stuff here )
    }


    stage ('Build') {
        try {
            notifyBuild('STARTED')

            dir("place") {
                sh 'script name'
            }
        } catch (e) {
            // If there was an exception thrown, the build failed
            currentBuild.result = "FAILED"
            throw e
        } finally {
            // Success or failure, always send notifications
            notifyBuild(currentBuild.result)
        }
    }
}

def notifyBuild(String buildStatus = 'STARTED') {
    // build status of null means successful
    buildStatus =  buildStatus ?: 'SUCCESSFUL'

    // Default values
    def colorName = 'RED'
    def colorCode = '#FF0000'
    def subject = "${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
    def summary = "${subject} (${env.BUILD_URL})"
    def details = """<p>STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
    <p>Check console output at &QUOT;<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>&QUOT;</p>"""

    // Override default values based on build status
    if (buildStatus == 'STARTED') {
        color = 'YELLOW'
        colorCode = '#FFFF00'
    } else if (buildStatus == 'SUCCESSFUL') {
        color = 'GREEN'
        colorCode = '#00FF00'
    } else {
        color = 'RED'
        colorCode = '#FF0000'
    }

    // Send notifications
    slackSend (channel: '@me', color: colorCode, message: summary)
}

说明:大多数Groovy DSL不允许在DSL元素中声明。 Jenkins DSL似乎也不例外。

答案 1 :(得分:8)

您可以使用闭包http://groovy-lang.org/closures.html

node('on-demand-t2large') {
    def notifyBuild = { String buildStatus -> 
        // build status of null means successful
        buildStatus =  buildStatus ?: 'SUCCESSFUL'

        // Default values
        def colorName = 'RED'
        def colorCode = '#FF0000'
        def subject = "${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
        def summary = "${subject} (${env.BUILD_URL})"
        def details = """<p>STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
        <p>Check console output at &QUOT;<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>&QUOT;</p>"""

        // Override default values based on build status
        if (buildStatus == 'STARTED') {
            color = 'YELLOW'
            colorCode = '#FFFF00'
        } else if (buildStatus == 'SUCCESSFUL') {
            color = 'GREEN'
            colorCode = '#00FF00'
        } else {
            color = 'RED'
            colorCode = '#FF0000'
        }

        // Send notifications
        slackSend (channel: '@me', color: colorCode, message: summary)
    }

    stage ('Checkout') {
        checkout( checkout stuff here )
    }

    stage ('Build') {
        try {
            notifyBuild('STARTED')

            dir("place") {
                sh 'script name'
            }
        } catch (e) {
            // If there was an exception thrown, the build failed
            currentBuild.result = "FAILED"
            throw e
        } finally {
            // Success or failure, always send notifications
            notifyBuild(currentBuild.result)
        }
    }
}