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

How to loop parameter value in Jenkins Declarative pipeline

我有一个 Jenkins 管道,我需要在其中获取 Integer 的参数值并执行 for 循环。

举个例子:如果参数的值为:3,我想循环代码3次。以下是我尝试但找不到确切解决方案

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

        }
    }
}

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


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(下面是截图)

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

下面是当值为count = 3

时的输出