Jenkins声明式管道。块中的条件语句

Jenkins声明式管道。块中的条件语句,第1张

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] echomy branch is master[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (Declarative: Post Actions)[Pipeline] script[Pipeline] {[Pipeline] echomail list master[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] // node[Pipeline] End of PipelineFinished: SUCCESS

参数为“ test”时输出:

[Pipeline] {[Pipeline] stage[Pipeline] { (test)[Pipeline] echomy branch is test[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (Declarative: Post Actions)[Pipeline] script[Pipeline] {[Pipeline] echomail list others[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] // node[Pipeline] End of PipelineFinished: 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"    }}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zaji/5622722.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-15

发表评论

登录后才能评论

评论列表(0条)

    保存