在Jenkins CI/CD流水线代码中,有没有办法将动态小时作为参数传递给Cron Job?

huangapple go评论66阅读模式
英文:

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 &quot;randomHour: ${randomHour}&quot;
println &quot;randomMinute: ${randomMinute}&quot;
jobProps &lt;&lt; pipelineTriggers([parameterizedCron(&#39;${randomMinute} ${randomHour} * * * %DEPLOY_TEST_ENVIRONMENT=true&#39;)])

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 &lt;&lt; pipelineTriggers([parameterizedCron(&#39;\${randomMinute} \${randomHour} * * * %DEPLOY_TEST_ENVIRONMENT=true&#39;)])

I think expanding them should solve your problem

jobProps &lt;&lt; pipelineTriggers([parameterizedCron(&quot;${randomMinute} ${randomHour} * * * %DEPLOY_TEST_ENVIRONMENT=true&quot;)])

You could also take advantage of Jenkins hash syntax

jobProps &lt;&lt; pipelineTriggers([parameterizedCron(&#39;H H(7-9) * * * %DEPLOY_TEST_ENVIRONMENT=true&#39;)])

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.

huangapple
  • 本文由 发表于 2023年8月9日 16:53:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定