Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a variable and set it to a default value if something goes wrong during definition

I have the following code in my build.gradle

Contents in version.properties are:

buildVersion=1.2.3 
  • Value of $v variable during the Gradle build is coming as: 1.2.3
  • Value of $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.

  1. 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".

  2. 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" } 
like image 887
AKS Avatar asked Jan 14 '14 01:01

AKS


People also ask

How can we set default value to the variable?

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.

What is the default value of 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.

How do you assign a default value to a variable in UNIX?

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).

What is the default value of object variable * undefined?

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.


2 Answers

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" 
like image 152
Peter Niederwieser Avatar answered Oct 02 '22 19:10

Peter Niederwieser


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");     } } 
like image 38
Rahul_findme Avatar answered Oct 02 '22 20:10

Rahul_findme