发布条件时的声明式管道 [英] Declarative pipeline when condition in post
问题描述
就詹金斯(Jenkins)中的声明式管道而言, when 关键字给我带来了麻烦.
As far as declarative pipelines go in Jenkins, I'm having trouble with the when keyword.
我一直收到错误No such DSL method 'when' found among steps
.我对Jenkins 2声明式管道有些陌生,并且我认为我不会将脚本化管道与声明式管道混为一谈.
I keep getting the error No such DSL method 'when' found among steps
. I'm sort of new to Jenkins 2 declarative pipelines and don't think I am mixing up scripted pipelines with declarative ones.
此管道的目标是在Sonar成功运行后运行mvn deploy
,并发送失败或成功的邮件通知.我只希望在master或release分支上部署工件.
The goal of this pipeline is to run mvn deploy
after a successful Sonar run and send out mail notifications of a failure or success. I only want the artifacts to be deployed when on master or a release branch.
我遇到困难的部分在 post 部分. 通知阶段效果很好.请注意,我没有 when 子句就可以使用它,但是确实需要它或等效的子句.
The part I'm having difficulties with is in the post section. The Notifications stage is working great. Note that I got this to work without the when clause, but really need it or an equivalent.
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
)
}
}
}
推荐答案
在文档中声明性管道,其中提到您不能在post
块中使用when
. when
仅在stage指令内允许使用.
因此,您可以做的是在script
中使用if
来测试条件:
In the documentation of declarative pipelines, It is mentionned that you can't use when
in the post
block. when
is allowed only inside a stage directive.
So what you can do is test the conditions using an if
in a script
:
post {
success {
script {
if (${GIT_LOCAL_BRANCH} == 'master')
currentBuild.result = 'SUCCESS'
}
}
// failure block
}
这篇关于发布条件时的声明式管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!