如何在 Jenkins 声明式管道中循环参数值

时间:2021-04-26 05:50:33

标签: jenkins jenkins-pipeline jenkins-groovy

我是 Jenkins 的新手,我正在尝试遍历 Jenkins 管道中的参数值。 所以我的参数只包含整数值,基于它的代码应该运行那么多次。

比如说:如果参数的值为:3,我想运行代码3次。我不确定如何在 Pipeline 中使用 for 循环。有人可以帮助我实现它。

这是我的一小段代码,我这样做是为了测试。

temp=activities_by_gender.groupby(['sport', 'gender']).agg(np.mean).groupby(level=0).apply(
    lambda x: 100 * x / x.sum()).unstack()

1 个答案:

答案 0 :(得分:1)

此管道脚本应为您提供提示:


pipeline {
    agent any
    stages {
        stage ('loop'){
            steps {
                echo "Looping"
            script {
                int num = "${env.count}".toInteger()
                
            for (int i=0; i<= num; ++i) {
                echo "Hello"
            }
            }
            }

        }
    }
}

在上面的管道脚本中,我使用变量 count 从用户那里接受脚本需要运行的次数。我用过This project is parameterised(下面是截图)

enter image description here

现在,我使用 toInterger() 将字符串值转换为 int,然后使用 for 循环对其进行迭代。

下面是count = 3的值

时的输出

enter image description here