Jenkins 根据工作日自动更改选择参数值

Jenkins change choice parameter value automatically based on the week day

在我的 jenkins 作业中,我有一个包含 4 个值(val1、val2、val3、val4)的选择参数。

是否可以根据循环时间事件动态设置选择参数值?

更准确地说,我想在一年中的每个星期一动态更改此值。

例如:

Monday March 16 => it takes val1 
Monday March 23 => it takes val2 
Monday March 30 => it takes val3 
Monday April 6  => it takes val4 
Monday April 13 => it takes val1

等等。

所以,你的问题基本上可以归结为两个:

  1. 如何根据当前日期以编程方式确定 select 的值?
  2. 弄清楚后,如何使该值成为管道 choice 参数中的默认值?

考虑到您自己在 #1 方面做得很好(可能包括获取周数并将余数除以 4),让我们来解决第二个问题。

要根据某些任意 Groovy 脚本的结果修改 choices 参数,您可能需要 运行 在声明性管道之前使用脚本管道,如下所示:

def use_as_default = getValToUseAsDefault() // val1 on March 16, etc.

def list_of_vals = []

list_of_vals += use_as_default // first in the list will get to be selected

if (! ("val1" in list_of_vals) ) { list_of_vals += "val1"}
if (! ("val2" in list_of_vals) ) { list_of_vals += "val2"}
if (! ("val3" in list_of_vals) ) { list_of_vals += "val3"}
if (! ("val4" in list_of_vals) ) { list_of_vals += "val4"}

list_of_vals = Arrays.asList(list_of_vals)

pipeline
{
    agent any

    parameters
    {
        choice(name: 'VALS', choices: list_of_vals, description: 'Choose value')
    }
...
}

def getValToUseAsDefault() {
   // left as exercise to OP
   return "val1"
}