英文:
Is there any way to pass dynamic hours as a parameter in crone job in jankins cicd pipeline code
问题
val random = Random()
val randomHour = 7 + random.nextInt(3) // 生成一个在7到9之间的随机数
val randomMinute = random.nextInt(60) // 生成一个在0到59之间的随机数
println("randomHour: ${randomHour}")
println("randomMinute: ${randomMinute}")
jobProps << pipelineTriggers([parameterizedCron("${randomMinute} ${randomHour} * * * %DEPLOY_TEST_ENVIRONMENT=true")])
英文:
def random = new Random()
def randomHour = 7 + random.nextInt(3) // Generate a random number between 7 and 9
def randomMinute = random.nextInt(60) // Generate a random number between 0 and 59
println "randomHour: ${randomHour}"
println "randomMinute: ${randomMinute}"
jobProps << pipelineTriggers([parameterizedCron('${randomMinute} ${randomHour} * * * %DEPLOY_TEST_ENVIRONMENT=true')])
I trying to run crone job any random time between 7:00AM to 9:00AM. I tryied with above way but it is not working for me.
答案1
得分: 1
为什么你试图避免在这里扩展变量?
jobProps << pipelineTriggers([parameterizedCron('${randomMinute} ${randomHour} * * * %DEPLOY_TEST_ENVIRONMENT=true')])
我认为扩展它们应该解决你的问题。
jobProps << pipelineTriggers([parameterizedCron("${randomMinute} ${randomHour} * * * %DEPLOY_TEST_ENVIRONMENT=true")])
你也可以利用Jenkins哈希语法
jobProps << pipelineTriggers([parameterizedCron('H H(7-9) * * * %DEPLOY_TEST_ENVIRONMENT=true')])
它基本上做了相同的事情,除了它不会在每次运行时更改计划的时间,它会选择随机时间,然后不再更改。
英文:
Why are you trying to avoid expanding the variables here?
jobProps << pipelineTriggers([parameterizedCron('\${randomMinute} \${randomHour} * * * %DEPLOY_TEST_ENVIRONMENT=true')])
I think expanding them should solve your problem
jobProps << pipelineTriggers([parameterizedCron("${randomMinute} ${randomHour} * * * %DEPLOY_TEST_ENVIRONMENT=true")])
You could also take advantage of Jenkins hash syntax
jobProps << pipelineTriggers([parameterizedCron('H H(7-9) * * * %DEPLOY_TEST_ENVIRONMENT=true')])
It does pretty much the same thing, except it doesn't change the scheduled time every run, it picks random time and after that it doesn't change.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论