Jenkinsfile 中基于参数的表达式

Ric*_*ano 3 expression jenkins jenkins-pipeline

我会评估 Jenkinsfile 的“when”语法中的两个条件。特别是,我需要检查 params.x 何时为 true,params.y 何时为 true。我可以使用“and”运算符将两者插入到同一表达式中吗?

小智 10

来自詹金斯文档

expression
Execute the stage when the specified Groovy expression evaluates to true, for example: when { expression { return params.DEBUG_BUILD } } 
Note that when returning strings from your expressions they must be converted to booleans or return null to evaluate to false. 
Simply returning "0" or "false" will still evaluate to "true".
Run Code Online (Sandbox Code Playgroud)

因此,如果您插入有效的常规语句,它应该可以工作。

或者,您可以使用allOf语句,例如

expression
Execute the stage when the specified Groovy expression evaluates to true, for example: when { expression { return params.DEBUG_BUILD } } 
Note that when returning strings from your expressions they must be converted to booleans or return null to evaluate to false. 
Simply returning "0" or "false" will still evaluate to "true".
Run Code Online (Sandbox Code Playgroud)

  • 显然它是可选的。无论您是否放置“return”关键字,groovy 块都会返回最后计算的值。 (2认同)