简体   繁体   English

如何覆盖 jenkins groovy 中的全局变量

[英]How to overwrite global variable in jenkins groovy

I was working on a jenkins groovy script.我正在研究 jenkins groovy 脚本。 I have defined one global variable at the start of the script, That is being used all over the groovy.我在脚本的开头定义了一个全局变量,它在整个 groovy 中使用。 Users using that groovy can modify the value for global variable as per their requirement.使用 groovy 的用户可以根据需要修改全局变量的值。

Now the issue is that I want to set a default value to the global variable in case a user left the global variable blank, How can I achieve this scenerio in Groovy?现在的问题是我想为全局变量设置一个默认值,以防用户将全局变量留空,我怎样才能在 Groovy 中实现这个场景?

Thanks in advance.提前致谢。 below is how my groovy looks like下面是我的 groovy 的样子

String var1 = " "
String var2 = " "

pipeline {
  agent any
  stages {

    stage('Stage 1') {
      steps {
        script {
          if(var1 == " ") {
              var1 = <default value>
          }
        }
      }
    }

    stage('Stage 2') {
      steps {
        script {
          docker login <here i want to use default var1>
        }
      }
    }

  }
}
if(!MY_GLOBAL_VAR){
    MY_GLOBAL_VAR = <default value>
}

or more explicit或更明确的

if(MY_GLOBAL_VAR == null){
    MY_GLOBAL_VAR = <default value>
}
if(!MY_GLOBAL_VAR){
    MY_GLOBAL_VAR = <default value>
}

or more explicit或更明确的

if(MY_GLOBAL_VAR){
    MY_GLOBAL_VAR = <default value>
}

If you run this in a declarative pipeline you need to surround it in a script{} block.如果您在声明式管道中运行它,则需要将其包围在script{}块中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jenkins Groovy-覆盖定义中的全局变量 - Jenkins Groovy - overwriting global variable in definition 如何在 Jenkins 中打印 Groovy 变量? - How to print a Groovy variable in Jenkins? Jenkins Groovy - 如何获取给定作业的所有正在运行的构建的 params 全局变量? - Jenkins Groovy - How to get params global variable of all running builds of a given job? 如何在 Jenkins 中永久使用 Groovy PostBuild 更新全局环境变量并在其他后续构建中使用它? - How can I update a global Environment Variable using Groovy PostBuild in Jenkins permanently and use it in other subsequent builds? 如何在没有调用方法的情况下访问 vars/*.groovy Jenkins 库中的全局变量/函数? - How to access Global variable/ functions in vars/*.groovy Jenkins Library without call method? 如何在Jenkins管道中将groovy变量传递给powershell? - How to pass groovy variable to powershell in Jenkins pipeline? 如何将Groovy变量传递给Shell Block Jenkins - How to pass a groovy variable to a shell block jenkins 如何在Jenkins Groovy脚本中将参数作为变量引用 - How to reference a parameter as a variable in Jenkins Groovy Script 如何在 jenkins groovy 中的变量值中添加动态性 - how to add dynamicity in a variable value in jenkins groovy 如何在jenkins sh命令中使用groovy变量 - How to use groovy variable in jenkins sh command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM