• Jenkins插件Parameterized Scheduler用法


    Jenkins定时触发构建的同时设定参数。可以根据不同的定时构建器设置不同参数或环境变量的值。可以设置多个参数。并结合when控制stage流程的执行。结合when和triggeredBy区分定时构建的stage和手动执行的stage。

    Jenkins插件名称:Parameterized Scheduler
    插件链接:Parameterized Scheduler插件官方文档
    这个是官方文档,其中包含了安装,介绍,使用示例,Issues等相关信息。
    config位置:configure->Build Triggers->Build periodically with parameters
    下面为对官方文档的机翻+自我理解和使用实例补充。

    什么是Parameterized Scheduler?

    Parameterized Scheduler是一个 Jenkins 插件,支持在构建计划中设置参数。支持使用多个 cron 行,每个 cron 行都以 % 和一些键值对name=value结尾,可以安排参数化构建在不同时间使用不同参数运行
    能在不同的cron表达式下设置不同的参数值,可以同时设置多个参数值。
    安装参考:https://www.jenkins.io/doc/book/managing/plugins/
    安装完之后,配置页面config会有如下标识:
    在这里插入图片描述在这里插入图片描述

    如何配置实现呢?

    Build periodically with parameters% 符号之前的 cron表达式的编写和处理方式与 jenkins 中的 Build periodically Schedule 相同。不同的是Build periodically with parameterscorn表达式后加%,然后添加项目构建参数所需的name=value键值对,可以同时添加多个。
    这个插件的idea源于Job流程构建时可能会使用到不同环境的需要。在不同的定时构建条件下,构建流程的参数可以设置为不同的,从而控制流程的开合。
    其中Build periodically Schedulecron表达式类似为

    triggers{
      cron('H * * * *')
    }
    
    • 1
    • 2
    • 3

    Build periodically with parameterscron表达式设置可参考下文:

    示例一,不同corn表达式指定单个参数的值

    在此示例中,有两个cron表达式,表示的是两种定时构建方案。
    其中每隔15min触发的Job流程里,Job参数会被设置为env=int
    其中每隔30min触发的Job流程里,Job参数会被设置为env=qa

    # lets run against the integration environment at 15 past the hour
    15 * * * * %env=int
    # run QA too
    30 * * * * %env=qa
    
    • 1
    • 2
    • 3
    • 4

    在pipeline中该代码片段为

    triggers {
        parameterizedCron('''
            15 * * * * %env=int
            30 * * * * %env=qa
        ''')
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    示例二,不同corn表达式指定多个参数的值

    比如有三个参数:
    ● furniture
    ● color
    ● name (with a default of fred
    可以使用如下流程:

    # leave spaces where you want them around the parameters. They'll be trimmed.
    # we let the build run with the default name
    5 * * * * %furniture=chair;color=black
    # now, let's override that default name and use Mr. Rubble.
    10 * * * * %furniture=desk;color=yellow;name=barney
    
    • 1
    • 2
    • 3
    • 4
    • 5

    表示每5min触发一次Job流程,Job参数会被设置为,furniture=chair;color=black
    每20min触发一次的Job流程,Job参数会被设置为,furniture=desk;color=yellow;name=barney
    在pipeline中该代码片段为

    triggers {
        parameterizedCron('''
            5 * * * * %furniture=chair;color=black
            10 * * * * %furniture=desk;color=yellow;name=barney
        ''')
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    声明式pipeline 配置例子

    可以使用触发器指令下的keyparameterizedCron来指定参数化 cron 触发器。内置的 cron 触发器仍然可用,并且独立于parameterizedCron
    例子

    pipeline {
        agent any
        parameters {
          string(name: 'PLANET', defaultValue: 'Earth', description: 'Which planet are we on?')
          string(name: 'GREETING', defaultValue: 'Hello', description: 'How shall we greet?')
        }
        triggers {
            parameterizedCron('''
                # leave spaces where you want them around the parameters. They'll be trimmed.
                # we let the build run with the default name
                */2 * * * * %GREETING=Hola;PLANET=Pluto
                */3 * * * * %PLANET=Mars
            ''')
        }
        stages {
            stage('Example') {
                steps {
                    echo "${params.GREETING} ${params.PLANET}"
                    script { currentBuild.description = "${params.GREETING} ${params.PLANET}" }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    使用when/triggeredBy指令

    when指令的选项之一是triggeredBy子句。当使用内置的 cron 触发器时,应该使用triggedBy 'TimerTrigger'。但是,parameterizedCron 触发器与内置触发器是不同的触发器,因此应该相应地更新triggeredBy,为 triggeredBy 'ParameterizedTimerTriggerCause'

    内置的cron 触发器中cron表达式结合when/triggeredBy指令

    使用Build periodically Schedulecron表达式,执行 控制某阶段 只能在定时构建时才触发这个流程时,需要写为:

    pipeline {
        agent any
        parameters {
          string(name: 'PLANET', defaultValue: 'Earth', description: 'Which planet are we on?')
          string(name: 'GREETING', defaultValue: 'Hello', description: 'How shall we greet?')
        }
        triggers {
            cron('*/2 * * * *')
        stages {
            stage('Example') {
                when {
                    triggeredBy 'TimerTrigger'
                }
                steps {
                    echo 'This build was triggered by a `parameterizedCron` trigger'
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    parameterizedCron 触发器中cron表达式结合when/triggeredBy指令

    使用Build periodically with parameterscron表达式,执行 控制某阶段 只能在定时构建时才触发这个流程时,需要写为:

    pipeline {
        agent any
        parameters {
          string(name: 'PLANET', defaultValue: 'Earth', description: 'Which planet are we on?')
          string(name: 'GREETING', defaultValue: 'Hello', description: 'How shall we greet?')
        }
        triggers {
            parameterizedCron('''
                # leave spaces where you want them around the parameters. They'll be trimmed.
                # we let the build run with the default name
                */2 * * * * %GREETING=Hola;PLANET=Pluto
                */3 * * * * %PLANET=Mars
            ''')
        stages {
            stage('Example') {
                when {
                    triggeredBy 'ParameterizedTimerTriggerCause'
                }
                steps {
                    echo 'This build was triggered by a `parameterizedCron` trigger'
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    相当于该阶段stage('Example') 只在 参数化定时构建(parameterizedCron) 触发流程时才会执行该阶段流程。手动触发该流程不会触发到这个阶段流程。

    parameterizedCron 触发器中cron表达式参数控制stage执行

    下面这个示例流程则是,当3点定时触发时,TEST_MODE=Daily,会触发stage('daily_test')而不会触发stage('weekly_test')
    当4点定时触发Job时,TEST_MODE=Weekly,会触发stage('weekly_test')而不会触发stage(‘daily_test’)`;

    pipeline {
        agent any
        parameters {
          string(name: 'TEST_MODE', defaultValue: '', description: 'TEST MODE: Daily Weekly')
        }
        triggers {
            parameterizedCron('''
                H 3 * * * %TEST_MODE=Daily
                H 4 * * * %TEST_MODE=Weekly
            ''')
        stages {
            stage('daily_test') {
               when {
    	              environment name: 'TEST_MODE', value: 'Daily'
    	              beforeAgent true
    	          }
                steps {
                    echo 'Daily Test'
                }
            }
            stage('weekly_test') {
               when {
    	              environment name: 'TEST_MODE', value: 'Weekly'
    	              beforeAgent true
    	          }
                steps {
                    echo 'WeeklyTest'
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    这样就能实现在不同时间点,控制传递给Job的参数值,并根据参数值控制执行不同流程。

    脚本化管道示例

    在脚本式管道要实现这个,可以参考下文:

    properties([
      parameters([
        string(name: 'PLANET', defaultValue: 'Earth', description: 'Which planet are we on?'),
        string(name: 'GREETING', defaultValue: 'Hello', description: 'How shall we greet?')
      ]),
      pipelineTriggers([
        parameterizedCron('''
            */2 * * * * %GREETING=Hola;PLANET=Pluto
            */3 * * * * %PLANET=Mars
        ''')
      ])
    ])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    config页面直接配置

    在这里插入图片描述

    参考

    关于corn表达式

    Jenkins cron定时构建触发器

    关于when

    when的用法

  • 相关阅读:
    业务脚本pytest封装
    webpack使用 三 优化环境配置
    ControllerBeanNameHandlerMapping类功能简介说明
    AIE性质DSA荧光团叶酸修饰介孔二氧化硅荧光纳米粒子/银纳米簇聚集诱导发光微球的制备
    Abbexa丨Abbexaα黑色素细胞刺激素 (aMSH) 蛋白方案
    方舟开服配置教程服务器怎么开
    颜色杂项笔记
    Java方法的使用
    聊聊 HTTP 性能优化
    Nginx下载安装与配置(linux)
  • 原文地址:https://blog.csdn.net/qq_40804558/article/details/136649640