Jenkins管道中关于triggers指令的条件 [英] Condition in Jenkins pipeline on the triggers directive

查看:437
本文介绍了Jenkins管道中关于triggers指令的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jenkins相对Jenkinsfile语法有一个很好的相对全面的文档 .但是我仍然找不到答案,可以在管道的顶层执行流控制吗?从字面上在pipeline {}部分(声明性)中添加if,例如:

Jenkins has a nice relatively comprehensive documentation about Jenkinsfile syntax. But I still not find there an answer is it possible to do a flow control on the top level of pipeline? Literally include something if just in pipeline {} section (Declarative) like:

pipeline {
    if (bla == foo) {
        triggers {
            ...configuration
        }
} 

pipeline {
    triggers {
        if (bla == foo) {
            something...
        }
    }
} 

triggers部分是只能包含一次且只能包含在pipeline部分中的部分.但是if语句似乎只能在阶段级别应用.

triggers section is a section which can be included only once and only in the pipeline section. But if statement has to be applied only in stage level seems.

有人知道如何在条件中包括某些内容,例如triggers或有条件地包含指令本身吗?

Do anyone know how to conditionally include something in direcitves, such as, triggers, or conditionally include directives itself?

推荐答案

您不能在whenscript之外的管道中使用流控制,但是您可以为触发参数之类的函数调用函数:

You can't use flow control in a pipeline outside of when and script, but you can call functions for things like trigger parameters:

pipeline {
    agent any
    triggers{ cron( getCronParams() ) }
    ...
}

def getCronParams() {
    if( someCondition ) {
        return 'H */4 * * 1-5'
    }
    else {
        return 'H/30 */2 * * *'
    } 
}

另一种方法是使用evaluate()动态生成管道脚本:

Another way is to generate your pipeline script dynamically using evaluate():

evaluate """
pipeline {
    agent any        
    ${getTriggers()}    
    ...
}
"""

String getTriggers() {
    if( someCondition ) {
        return "triggers{ cron('H */4 * * 1-5') }"
    }
    else {
        return "triggers{ pollSCM('H */4 * * 1-5') }"
    } 
}

这篇关于Jenkins管道中关于triggers指令的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆