【发布时间】:2022-05-13 19:39:50
【问题描述】:
就 Jenkins 中的声明性管道而言,我在使用 when 关键字时遇到了问题。
我不断收到错误No such DSL method 'when' found among steps。我对 Jenkins 2 声明性管道有点陌生,我认为我不会将脚本化管道与声明性管道混为一谈。
此管道的目标是在成功运行 Sonar 后运行 mvn deploy 并发送失败或成功的邮件通知。我只希望在 master 或 release 分支上部署工件。
我遇到困难的部分在 post 部分。 通知 阶段运行良好。请注意,我可以在没有 when 子句的情况下使用它,但确实需要它或等效项。
pipeline {
agent any
tools {
maven 'M3'
jdk 'JDK8'
}
stages {
stage('Notifications') {
steps {
sh 'mkdir tmpPom'
sh 'mv pom.xml tmpPom/pom.xml'
checkout([$class: 'GitSCM', branches: [[name: 'origin/master']], doGenerateSubmoduleConfigurations: false, submoduleCfg: [], userRemoteConfigs: [[url: 'https://repository.git']]])
sh 'mvn clean test'
sh 'rm pom.xml'
sh 'mv tmpPom/pom.xml ../pom.xml'
}
}
}
post {
success {
script {
currentBuild.result = 'SUCCESS'
}
when {
branch 'master|release/*'
}
steps {
sh 'mvn deploy'
}
sendNotification(recipients,
null,
'https://link.to.sonar',
currentBuild.result,
)
}
failure {
script {
currentBuild.result = 'FAILURE'
}
sendNotification(recipients,
null,
'https://link.to.sonar',
currentBuild.result
)
}
}
}
【问题讨论】:
-
确实,您目前在全局帖子块中时无法使用。 'When' 必须在阶段指令中使用。使用 if else 是一个合乎逻辑的选择,但您需要在声明性管道中添加一个脚本块来完成这项工作:检查 stackoverflow.com/questions/49559882/…
标签: jenkins