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