如何解决Jenkins声明式管道块中的条件语句
的确,您当前不能when
在全局post块中使用。When
必须在stage指令内使用。
使用是一种合理的选择if else
,但是您需要在声明性管道中使用脚本编写的块来完成此工作:
pipeline {
agent any
parameters {
string(defaultValue: "master", description: 'Which branch?', name: 'BRANCH_NAME')
}
stages {
stage('test'){
steps {
echo "my branch is " + params.BRANCH_NAME
}
}
}
post {
success{
script {
if( params.BRANCH_NAME == 'master' ){
echo "mail list master"
}
else {
echo "mail list others"
}
}
}
}
}
参数为master时输出:
[Pipeline] {
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] echo
my branch is master
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
mail list master
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
参数为“ test”时输出:
[Pipeline] {
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] echo
my branch is test
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
mail list others
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
为了使它更加简洁,您可以将脚本作为函数调用:
pipeline {
agent any
parameters {
string(defaultValue: "master", description: 'Which branch?', name: 'BRANCH_NAME')
}
stages {
stage('test'){
steps {
echo "my branch is " + params.BRANCH_NAME
}
}
}
post {
success{
getMailList(params.BRANCH_NAME)
}
}
}
def getMailList(String branch){
if( branch == 'master' ){
echo "mail list master"
}
else {
echo "mail list others"
}
}
解决方法
有一个詹金斯管道。构建成功时需要/想要发送电子邮件。通过电子邮件将所有分支的电子邮件发送到 maillist-1, 并将master分支的构建过滤到
maillist-master 。
我尝试使用if
和when
语句步骤,但它们均在后期阻止中失败。
pipeline {
agent ...
stages {...}
post{
success{
archiveArtifacts: ...
if( env.BRANCH_NAME == 'master' ){
emailext( to: 'maillist-master@domain.com',replyTo: 'maillist-master@domain.com',subject: 'Jenkins. Build succeeded :^)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。