是否有办法将动态小时作为参数传递给Jenkins CI/CD管道代码中的定时任务?

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

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

我认为扩展它们应该解决你的问题。

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

你也可以利用Jenkins哈希语法

jobProps &lt;&lt; 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-2.html
匿名

发表评论

匿名网友

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

确定