英文:
Is there any way to pass dynamic hours as a parameter in crone job in jankins cicd pipeline code
问题
import java.util.Random
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')])
我想在早上7点到9点之间的任意时间运行定时任务。我尝试了上述的方式,但对我来说不起作用。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论