问题描述
我有一个像这样的 Jenkins 流水线作业:
I have a Jenkins pipeline job like this:
triggers {
parameterizedCron(env.BRANCH_NAME == "master" ? "0 12 * * * % RUN_E2E=true;MODE=parallel" : "")
}
后来我有这样的条件逻辑:
Later I have conditional logic like this:
stage('Build Release') {
when {
allOf {
branch 'master'
not {
triggeredBy 'TimerTrigger'
}
}
}
但是,triggerBy 没有被激活.即,不是由 timerTrigger 触发的".即使在 parameterizedCron 运行时似乎也是如此.
The triggeredBy is not activated however. That is, "not triggered by timerTrigger" appears to be true even when the parameterizedCron runs.
我从 这里的文档中得到了这个例子.
I got this example from the docs here.
我的问题是,如果我希望我的构建/发布阶段仅在分支 == master 上执行,而在 parameterizedCron 执行期间不,我该怎么做?
My question is, if I want my build/release stage to execute only on branch == master, and not during the parameterizedCron execution, how can I do that?
推荐答案
你的问题是 parameterizedCron
不是 TimerTrigger
,只有常规 cron
是.
Your problem is that parameterizedCron
is not a TimerTrigger
, only the regular cron
is.
满足您的要求的最简单方法是添加一个参数并在 parameterizedCron
中设置它:
The easiest way to go about your requirement is to add a parameter and set it in parameterizedCron
:
triggers {
parameterizedCron(env.BRANCH_NAME == "master" ? "0 12 * * * % RUN_E2E=true;MODE=parallel;SHOULD_BUILD_RELEASE=no" : "")
}
你可以这样做:
stage('Build Release') {
when {
allOf {
branch 'master'
expression { SHOULD_BUILD_RELEASE == 'yes' }
}
}
否则,您可能会以编程方式发现触发构建的原因,如果发现是 parameterizedCron 触发了构建/发布部分,则退出构建/发布部分.有关示例,请参阅此处.在 parameterizedCron
的情况下,相关部分是这样的:
Otherwise, you may programmatically discover why your build was triggered, and drop out of the build/release part if it turns out that parameterizedCron has triggered it. See here for an example. In the case of parameterizedCron
, the relevant part is this:
timerCause = currentBuild.rawBuild.getCause(org.jenkinsci.plugins.parameterizedscheduler.ParameterizedTimerTriggerCause)
if (timerCause) {
echo "Build reason: Build was started by parameterized timer"
}
最后,您可以尝试使用确切的类,就像这样(我还没有检查过,它可能会或可能不会起作用):
Finally, you may try playing around with the exact class, like this (I haven't checked it and it may or may not work):
stage('Build Release') {
when {
allOf {
branch 'master'
not {
triggeredBy 'ParameterizedTimerTriggerCause'
}
}
}
这篇关于由 Jenkins 管道中的参数化 Cron 触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!