英文:
How can I configure Jenkins multibranch pipelines to use option with different args for branches & pull requests in a single Jenkinsfile?
问题
我正在使用声明式 Jenkins 多分支流水线,并希望在构建分支时使用以下选项:options { disableConcurrentBuilds(abortPrevious: false) }
,在构建拉取请求时使用:options { disableConcurrentBuilds(abortPrevious: true) }
。
disableConcurrentBuilds
禁止同时执行流水线。可以用于防止对共享资源的同时访问等情况。例如:options { disableConcurrentBuilds() } 可以在已经有正在执行的流水线构建时排队构建,或者 options { disableConcurrentBuilds(abortPrevious: true) } 可以中止正在运行的构建并启动新的构建。
是否可以在单个 Jenkinsfile 中根据分支类型配置 Jenkins 多分支流水线以使用不同的 disableConcurrentBuilds() 参数?
类似于 Azure 流水线中的 batch 和 autoCancel 在 Azure 流水线中的可行性:
trigger:
batch: true # 等待运行完成,然后启动另一个运行
branches:
include:
- main
pr:
autoCancel: true # 取消相同 PR 的正在进行的运行,默认为 true
branches:
include:
- '*'
英文:
i am working on declarative jenkins multibranch pipelines and i would like to use
options { disableConcurrentBuilds(abortPrevious: false) }
when building branches- and
options { disableConcurrentBuilds(abortPrevious: true) }
when building pull requests.
options
> disableConcurrentBuilds
>
> Disallow concurrent executions of the Pipeline. Can be useful for preventing simultaneous accesses to shared resources, etc. For example: options { disableConcurrentBuilds() } to queue a build when there’s already an executing build of the Pipeline, or options { disableConcurrentBuilds(abortPrevious: true) } to abort the running one and start the new build.
Is it possible to configure Jenkins multibranch pipelines to use different arguments for disableConcurrentBuilds() options based on branch type in a single Jenkinsfile?
like it is possible in azure pipelines with batch and autoCancel:
trigger:
batch: true #waits until the run is completed, then starts another run
branches:
include:
- main
pr:
autoCancel: true #cancel in-progress runs for the same PR Defaults to true
branches:
include:
- '*'
答案1
得分: 1
Jenkins在构建拉取请求时定义了一个名为CHANGE_ID
的环境变量。您可以使用它来设置布尔参数:
options {
disableConcurrentBuilds(abortPrevious: env.CHANGE_ID ? true : false)
}
英文:
When building pull requests, Jenkins defines an environment variable named CHANGE_ID
. You can use it to set the boolean parameter:
options {
disableConcurrentBuilds(abortPrevious: env.CHANGE_ID ? true : false)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论