Jenkins将触发器块传递到共享库管道

fdbelqdn  于 2022-11-21  发布在  Jenkins
关注(0)|答案(1)|浏览(242)

我有一个共享库,其中包含一个声明性管道,许多作业都使用它来构建。但是,我希望能够将触发器块从Jenkinsfile传递到共享库,因为一些作业我希望通过Cron触发,其他作业我希望通过SNS触发,其他作业使用上游作业触发。
我有办法做到吗?我试过的一切都失败了
我试过了

  1. #Jenkinsfile
  2. @Library('build') _
  3. buildAmi{
  4. OS = "amazonlinux"
  5. owners = "amazon"
  6. filters = "\"Name=name,Values=amzn-ami-hvm-*-x86_64-gp2\""
  7. template = "linux_build_template.json"
  8. trigger = triggers {
  9. cron('0 H(06-07) * * *')
  10. }
  11. #Shared Lib
  12. pipeline {
  13. $buildArgs.trigger

其失败原因是

  1. Not a valid section definition

我也尝试过只将cron计划传递到共享库中,例如

  1. triggers {
  2. cron("${buildArgs.cron}")
  3. }

但这会产生误差

  1. Method calls on objects not allowed outside "script" blocks

我尝试过各种其他的方法,但似乎声明式风格需要一个内部只有触发器的触发器块。
有人知道有什么方法可以实现我正在努力做的事情吗?

x7yiwoj4

x7yiwoj41#

注解代码太多:
我们想把一些预置属性和jenkinsfile属性合并起来,这是一种有效的方法,因为属性方法只能被调用一次。
我们使用了带有触发信息的附加管道参数:
Jenkins档案:

  1. mypipeline (
  2. pipelineTriggers: [pollSCM(scmpoll_spec: '0 5,11,15,22 * * 1-5')],
  3. )

然后在共享库中

  1. @Field def pipelineProperties = [
  2. disableConcurrentBuilds(abortPrevious: true),
  3. buildDiscarder(logRotator(numToKeepStr: '10', artifactNumToKeepStr: '0'))
  4. ]
  5. def pipelineTriggersArgs = args.pipelineTriggers
  6. def setJobProperties() {
  7. def props = pipelineProperties
  8. def str = ""
  9. pipelineProperties.each { str += (blue + it.getClass().toString() + it.toString() + "\n") }
  10. if (pipelineTriggersArgs) {
  11. if (debugLevel > 29) {
  12. def plTrig = pipelineTriggers(pipelineTriggersArgs)
  13. str += (mag + "args: " + pipelineTriggersArgs.getClass().toString() + "\t\t" + pipelineTriggersArgs.toString() + "\n" + off)
  14. str += (red + "expr: " + plTrig.getClass().toString() + "\t\t" + plTrig.toString() + "\n" + off)
  15. echo str
  16. }
  17. props?.add(pipelineTriggers(pipelineTriggersArgs)) // pass param with list as the arg
  18. }
  19. properties(props)
  20. }

这是合并预设和参数的唯一方法,因为属性不能被覆盖。不完全是答案,但我认为是解决它的方法。

展开查看全部

相关问题