web-dev-qa-db-ja.com

Groovy:ここではメソッド定義は予期されていません

これは簡単な修正であると確信していますが、プログラミング全般、特にGroovyは初めてです。

私は このガイド に従って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は内部クラスの定義を受け入れないことを理解していますが、プログラミングの初心者と同じように、それを修正する方法がわかりません。どんな助け(私が学ぶことができるように説明で!)は高く評価されるでしょう。

15
Alex

notifyBuild()メソッドをnodeおよびstage階層の外に移動します。

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も例外ではないようです。

22
BalRog

Closuresを使用できます 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)
        }
    }
}
9
heroin