如何将活动选择参数的选定值传递给构建脚本

How to pass selected values of active choice parameters to build script

我是 Jenkins 的新手,请帮我解决这个问题。 我有一个有点复杂的 Maven 项目,所以我使用 testNG xmls 分离了测试。根据执行流程,我将选择合适的 .xml 文件来构建项目。现在我正在做的是通过选择所需的 xml 文件手动构建脚本,如下所示。

我设置了一个选择参数 (name=testSuite),其中包括所有 xmls 并在构建部分包含 mvn clean test 命令。

mvn install test -DsuiteXmlFile=src/test/resources/testSuite/$testSuite

当我构建时,它将从选择参数下拉列表中获取选定的值并执行。

但我的要求是我想在特定时间段内将其定期集成到 运行 构建中。为此,我尝试使用“主动选择参数”,但请帮助我如何调用选定的复选框选项并继续 mvn install test

以下是我使用的方法。

  1. 我创建了一个管道脚本来生成“活动选择参数和参考参数”
  2. 然后我尝试使用 `echo "Scripts: ${params.Scripts}"
  3. 获取选定的值

下面是我的参数,

“脚本”将包括复选框和 xml 文件

在这种情况下,我将如何将选定的 xml 传递给 mvn clean test,因为截至目前,它将使用逗号分隔符作为 caseCreation.xml,testng.xml 传递所有选定的值。由于这个原因,我如何分离每个选定的 xmls 并将其传递给 mvn install test -DsuiteXmlFile=src/test/resources/testSuite/$testSuite.

也请帮我想出一个更好的方法,在特定时间段内定期构建,以满足上述情况。

提前致谢。

`

我想推荐一个我自己使用的插件 运行 基于 cron 语法并能够指定参数的作业:Parameterized Scheduler

properties([
    pipelineTriggers([
        parameterizedCron('''
            0 20 * * 1-5 %gitRevision=desktop/dev;brokerName=robotests
            0 00 * * 1-5 %gitRevision=master;brokerName=robotests
        ''')
    ]),
])

如果我没理解错的话,用不同的参数定义几个 运行 就足够了。 如果你想使用 Active Choice 参数值,那么你应该明白它将所有值存储为一个字符串。使用 Groovy 魔术将字符串转换为列表并指定所需的索引(将其拆分),例如

sh "mvn install test -DsuiteXmlFile=src/test/resources/testSuite/${Scripts.tokenize(',')[0]}"

将是您的第一个值

下面是声明性管道的示例:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                script {
                    Scripts.tokenize(',').each{
                        sh "mvn install test -DsuiteXmlFile=src/test/resources/testSuite/${it}"
                    }
                }
            }
        }
    }
}

脚本化:

node (){ 
    stage("Build") {
        Scripts.tokenize(',').each{
            sh "mvn install test -DsuiteXmlFile=src/test/resources/testSuite/${it}"
        }
    }
}