Groovy: 此处不应定义方法
Groovy: Method definition not expected here
我确定这是一个简单的修复,但我对一般编程是新手,尤其是 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 "<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</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 不接受内部 class 定义,但作为编程新手我不知道如何修复它。任何帮助(解释,以便我可以学习!)将不胜感激。
将 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 "<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</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 也不例外。
你可以使用闭包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 "<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</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)
}
}
}
我确定这是一个简单的修复,但我对一般编程是新手,尤其是 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 "<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</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 不接受内部 class 定义,但作为编程新手我不知道如何修复它。任何帮助(解释,以便我可以学习!)将不胜感激。
将 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 "<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</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 也不例外。
你可以使用闭包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 "<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</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)
}
}
}