[英]How to use Random number in Jenkins Declarative pipeline
Can someone please help me how to use random numbers in jenkins declarative pipelines as a environment variable to use this number in whole pipeline, we need this number to use as a tag to my build artifact.有人可以帮我如何在 jenkins 声明性管道中使用随机数作为环境变量,以便在整个管道中使用这个数字,我们需要这个数字作为我的构建工件的标签。 Thank you.
谢谢你。
I tried below in my Jenkins declarative pipeline but it throws null message我在我的 Jenkins 声明性管道中尝试了下面,但它抛出了 null 消息
environment {
rand = "$RANDOM"
}
stages{
stage("number"){
steps{
script{
echo " this is a number $rand"
}}}}
Here is the pipeline script, to generate random number:这是管道脚本,用于生成随机数:
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}"
}
}
}
}
This will generate random numbers between 0-50.这将生成 0-50 之间的随机数。
Here you can change the value of the max
to decide the upper limit range.在这里您可以更改
max
的值来决定上限范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.