Jenkins有一个不错的比较全面的documentation关于 Jenkinsfile 语法。但是我仍然没有找到答案是否可以在管道的顶层进行流量控制?字面上包括一些东西 if
就在 pipeline {}
部分(声明性)如:
pipeline {
if (bla == foo) {
triggers {
...configuration
}
}
或者
pipeline {
triggers {
if (bla == foo) {
something...
}
}
}
triggers
section 是一个只能包含一次且只能包含在 pipeline
中的部分。部分。但是if
语句似乎只能应用于阶段级别。有谁知道如何在指令中有条件地包含一些东西,例如,
triggers
,或有条件地包含指令本身?
最佳答案
您不能在 when
之外的管道中使用流量控制和 script
,但您可以为触发器参数之类的东西调用函数:
pipeline {
agent any
triggers{ cron( getCronParams() ) }
...
}
def getCronParams() {
if( someCondition ) {
return 'H */4 * * 1-5'
}
else {
return 'H/30 */2 * * *'
}
}
另一种方法是使用
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 - Jenkins 管道中触发器指令的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61106044/