如何在Jenkins脚本化管道中设置多行参数化cron作业?

时间:2019-09-04 13:45:43

标签: jenkins cron jenkins-pipeline

所以这很好:

properties([
    //https://stackoverflow.com/questions/35370810/how-do-i-use-jenkins-pipeline-properties-step
    parameters([
        //note here that the default will be the element you have as a first choice in the array
        choice(name: 'environmentName', choices: ['PROD', 'STAGE'], description: 'Choose the environment you want to run automation tests'),
    ]),

    //explained here: https://stackoverflow.com/questions/44113834/trigger-hourly-build-from-scripted-jenkinsfile
    //recall that ANY change you do here you will need to execute the job once manually for the new cron parameter to be updated, pushing it is not enough
    //minutes, hours, day of month, month, day of week  //H is any first available (here minute) of the hour according to resources
    pipelineTriggers([
       parameterizedCron('*/2 * * * 0-4 %environmentName=STAGE')
    ]),
])        

但是,当尝试按以下说明在多行中定义它时:https://github.com/jenkinsci/parameterized-scheduler-plugin/blob/master/README.md

然后这不起作用:

properties([
    //https://stackoverflow.com/questions/35370810/how-do-i-use-jenkins-pipeline-properties-step
    parameters([
        //note here that the default will be the element you have as a first choice in the array
        choice(name: 'environmentName', choices: ['PROD', 'STAGE'], description: 'Choose the environment you want to run automation tests'),
    ]),

    //explained here: https://stackoverflow.com/questions/44113834/trigger-hourly-build-from-scripted-jenkinsfile
    //recall that ANY change you do here you will need to execute the job once manually for the new cron parameter to be updated, pushing it is not enough
    //minutes, hours, day of month, month, day of week  //H is any first available (here minute) of the hour according to resources
    pipelineTriggers([
        //cron('H 23 * * 0-4') //this works with the DEFAULT parameters
        parameterizedCron { //this is documented here: https://github.com/jenkinsci/parameterized-scheduler-plugin/blob/master/README.md
            parameterizedSpecification( '''
                # leave spaces where you want them around the parameters. They'll be trimmed
                #*/2 * * * * %environmentName=STAGE;SomeOtherVariable=Pluto
                #you may repeat multiple configuration if you want
                */2 * * * 0-4 %environmentName=STAGE
            ''' )
        }
    ]),
])

执行詹金斯工作后,收到的异常是这样的:

java.lang.ClassCastException: org.jenkinsci.plugins.parameterizedscheduler.ParameterizedTimerTrigger.parameterizedSpecification expects class java.lang.String but received class org.jenkinsci.plugins.workflow.cps.CpsClosure2

所以看来parameterizedSpecification方法很好,它只抱怨传递的参数,但是传递的参数只是一个多行字符串。 这是正确的Groovy语法问题还是其他问题?完全不确定。我们需要您的帮助

1 个答案:

答案 0 :(得分:0)

答案可能就这么简单

parameterizedCron( '''
          */2 * * * 0-4 %environmentName=STAGE
          */3 * * * 0-4 %environmentName=PROD
        ''' )

表示您不需要包含parameterizedSpecification

的更复杂形式

在不完全了解其工作原理的情况下

相关问题