简体   繁体   English

如何在 Jenkins 声明式管道中使用随机数

[英]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.

相关问题 如何在 Jenkins 声明式管道中运行“sudo su”命令? - How to run "sudo su" command in Jenkins Declarative pipeline? 在 jenkins 声明性管道 cloudformation 中使用带有 shell 命令的 If 语句 - Using If statement with shell commands in jenkins Declarative pipeline cloudformation Bash 脚本未在 Jenkins 声明性管道中采用第二个参数 - Bash Script is Not Taking 2nd Argument in Jenkins Declarative Pipeline 如何在 Jenkins 管道脚本中使用 source 命令 - How to use source command within Jenkins pipeline script 如何在 Jenkins 管道脚本中使用 shell 脚本变量作为 groovy 变量 - How to use shell script variable as groovy variable in Jenkins pipeline script 在声明性管道内的 shell 脚本中传递 Jenkins 参数值时出现错误替换错误 - Bad Substitution error while passing Jenkins Parameter value in shell script inside Declarative pipeline Jenkins 管道在 shell 和 curl 中使用变量 - Jenkins pipeline use variable inside shell and curl 如何在 Jenkins 管道中定义一个使用 curl 检查服务器是否启动的测试阶段? - How to define a test stage in Jenkins pipeline which makes use of curl to check if the server is up or not? 如何将 shell 脚本添加到 jenkins 管道 - How to add shell script to jenkins pipeline 詹金斯:如何在脚本管道中更改外壳? - Jenkins: How to change shell in Scripted Pipeline?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM