I have the following code in my build.gradle
Contents in version.properties
are:
buildVersion=1.2.3
$v
variable during the Gradle build is coming as: 1.2.3 $artifactoryVersion
variable in JENKINS build is coming as: 1.2.3.1, 1.2.3.2, 1.2.3.x ... and so on where the 4th digit is Jenkins BUILD_NUMBER available to gradle build script during Jenkins build.BUT, when I'm running this build.gradle
on my desktop where I dont have BUILD_NUMBER variable available or set in my ENVIRONMENT variables, I get an error saying trim()
can't work on null. (as there's no BUILD_NUMBER
for Desktop/local build).
I'm trying to find a way i.e.
What should I code in my script so that if BUILD_NUMBER
is not available, then instead of gradle build processing failing for an error, it'd set jenkinsBuild = "0"
(hard coded) otherwise, pick what it gets during Jenkins build.
For ex: in Bash, we set a variable var1=${BUILD_NUMBER:-"0"}
which will set var1
to a valid Jenkins BUILD number if it's available and set to a value, otherwise if it's NULL, then var1 = "0"
.
I DON'T want to have each developer/user set this BUILD_NUMBER
in some property file. All I want is, if this variable doesn't exist, then the code should put "0"
in jenkinsBuilds variable and doesn't error out during desktop builds. I know during Jenkins build, it's working fine.
// Build Script def fname = new File( 'version.properties' ) Properties props = new Properties() props.load( new FileInputStream( fname ) ) def v = props.get( 'buildVersion' ) def env = System.getenv() def jenkinsBuild = env['BUILD_NUMBER'].trim() if( jenkinsBuild.length() > 0 ) { artifactoryVersion = "$v.$jenkinsBuild" }
You can set the default values for variables by adding ! default flag to the end of the variable value. It will not re-assign the value, if it is already assigned to the variable.
Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor.
You can use something called Bash parameter expansion to accomplish this. To get the assigned value, or default if it's missing: FOO="${VARIABLE:-default}" # If variable not set or null, use default. # If VARIABLE was unset or null, it still is after this (no assignment done).
undefined is a type by itself (undefined). Unassigned variables are initialized by JavaScript with a default value of undefined. Here as the variable is declared but not assigned to any value, the variable by default is assigned a value of undefined. On the other hand, null is an object.
All you need is some regular Java/Groovy code:
def jenkinsBuild = System.getenv("BUILD_NUMBER") ?: "0"
The code above uses Groovy's "elvis" operator, and is a shorthand for the following code, which uses Java's ternary operator:
def buildNumber = System.getenv("BUILD_NUMBER") def jenkinsBuild = buildNumber != null ? buildNumber : "0"
Here's the answer to using a Java plain object (JDK8):
public class Sample { private String region; private String fruit; public Sample() { region = System.getenv().getOrDefault("REGION", null); fruit = System.getenv().getOrDefault("FRUIT", "apple"); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With