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

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



Solution 1:[1]

You can do like this

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

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

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 aphexlog