How to setup a multiline parametrized cron job in Jenkins Scripted Pipeline?

被刻印的时光 ゝ 提交于 2021-01-29 02:49:50

问题


So this is working fine:

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')
    ]),
])        

However when trying to define it in multiline as explained here: https://github.com/jenkinsci/parameterized-scheduler-plugin/blob/master/README.md

then this is NOT working:

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

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


回答1:


It might be that the answer is as simple as this:

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

meaning that you don't need the more complex form of including the parameterizedSpecification

without exactly understanding why this worked



来源:https://stackoverflow.com/questions/57789810/how-to-setup-a-multiline-parametrized-cron-job-in-jenkins-scripted-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!