如何在詹金斯声明性管道中使用随机数

时间:2021-04-15 14:18:30

标签: shell jenkins jenkins-pipeline jenkins-groovy groovyshell

有人可以帮助我如何在 jenkins 声明性管道中使用随机数作为环境变量在整个管道中使用这个数字,我们需要这个数字作为我的构建工件的标签。谢谢。

我在我的 Jenkins 声明性管道中尝试了以下内容,但它抛出空消息

environment {
          rand = "$RANDOM"
}

stages{
    stage("number"){
     steps{
       script{
               echo " this is a number $rand"
}}}}

1 个答案:

答案 0 :(得分:0)

这是生成随机数的管道脚本:

pipeline {
    agent any
    
    environment {
        max = 50
        random_num = "${Math.abs(new Random().nextInt(max+1))}"
    }
    
    stages {
        
        stage('Randon number') {
            steps {
                echo "The random number is: ${env.random_num}"
            }
        }
        
    }
    
}

这将生成 0-50 之间的随机数。

这里可以改变max的值来决定上限范围。