如何在Jenkins脚本化管道中设置多行参数化cron作业? [英] How to setup a multiline parametrized cron job in Jenkins Scripted Pipeline?

查看:92
本文介绍了如何在Jenkins脚本化管道中设置多行参数化cron作业?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这很好:

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

However when trying to define it in multiline as explained here: 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
            ''' )
        }
    ]),
])

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

and upon execution of the jenkins job the exception received is this one:

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语法问题还是其他问题?完全不确定.我们需要您的帮助

So it seems that the parameterizedSpecification method is found ok and it complains only for the passed parameter but the passed parameter is only a multiline string. Is this an issue of proper Groovy syntax or anything else? Not sure at all. We need your help

推荐答案

答案可能就这么简单:

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

表示您不需要包含parameterizedSpecification

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

without exactly understanding why this worked

这篇关于如何在Jenkins脚本化管道中设置多行参数化cron作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆