栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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"    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/616626.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号