Je suis sûr que c'est une solution simple mais je suis nouveau dans la programmation en général et Groovy en particulier.
J'essaie de suivre ce guide pour activer les notifications de relâchement dans un travail de pipeline Jenkins 2 mais coller et personnaliser le bloc de code final me donne l'erreur suivante;
Method definition not expected here. Please define the method at an appropriate place or perhaps try using a block/Closure instead.
C'est mon code en ce moment, bien que j'aie substitué quelques bits à la sécurité. L'erreur se produit sur la ligne 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)
}
}
}
D'après certains googleurs, groovy n'accepte pas la définition de classe interne, mais aussi nouveau que je sois en programmation, je ne sais pas comment y remédier. Toute aide (avec des explications pour que je puisse apprendre!) Serait appréciée.
Déplacez la méthode notifyBuild()
en dehors de la hiérarchie node
et stage
, comme ceci:
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)
}
Explication: La plupart des DSL Groovy n'autorisent pas les déclarations à l'intérieur des éléments DSL. Il semble que le Jenkins DSL ne fait pas exception.
Vous pouvez utiliser des fermetures 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)
}
}
}