Home > other >  Jenkins check if env.BRANCH that starts with a letter (v) followed by a number (v1) is greater than
Jenkins check if env.BRANCH that starts with a letter (v) followed by a number (v1) is greater than

Time:03-30

I'm expecting something like branch = "v1" or greater than 1, like v2 or v3.

I need to be able to do something like:

if branch == "v2" or greater than 2
do stuff

branches can also be master, feature/something, v1, etc. which shouldn't meet the if condition

CodePudding user response:

You can do like this

String getVersion = env.BRANCH_NAME
def getBranchTag = getVersion.substring(1)

if ( "$getBranchTag" > 1) {
  do stuff
}

CodePudding user response:

Okay, so the solution was simply:

String thisVersion = "v2".replace("v", ""); // e.g v2
String otherVersion = "v1".replace("v", ""); // e.g v1

def versionLaterThan(x) { x.toInteger() > 1}

println("thisVersion: "   versionLaterThan(thisVersion));
println("otherVersion: "   versionLaterThan(otherVersion));

outputs:

thisVersion: true
otherVersion: false

The I can make a conditional to run if thisVersion is true

  • Related