简体   繁体   中英

How to overwrite global variable in jenkins groovy

I was working on a jenkins groovy script. I have defined one global variable at the start of the script, That is being used all over the groovy. Users using that groovy can modify the value for global variable as per their requirement.

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?

Thanks in advance. below is how my groovy looks like

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM