I'm trying to set a variable on a switch statement. I created a simple function to do this, but it won't actually update the value. here is the function I created:
def VAR1 = 'this is not the real value';
stage('Set Variables') {
steps {
script{
SetVariables()
}
}
}
def SetVariables() {
sh 'echo $BRANCH_NAME'
switch(BRANCH_NAME){
case 'BranchA':
sh 'echo We are in the right branch'
VAR1 = "This is the correct String"
sh 'echo $VAR1 is the variable's value'
break
}
}
Both of the echos are showing, but the value of VAR1 remains 'this is not the real value'
If I move this code into a steps/script block it works perfectly fine.
stage('Set Variables') {
steps {
script{
switch(BRANCH_NAME){
case 'BranchA':
sh 'echo We are in the right branch'
VAR1 = "This is the correct String"
sh 'echo $VAR1 is the variable's value'
break
}
}
}
}
This bit of code will expand as I add more variables and cases and I'd like to have it stored at the bottom in a function to keep things clean and readable. What am I missing here that is preventing the variable from being updated properly in a function?