Is there a way to pass current BUILD_NUMBER to Gradle deamon from Jenkins build?
Asked Answered
S

3

12

I am accessing Jenkins build number in Gradle script by calling:

def buildNumber = System..getenv('BUILD_NUMBER')

It works fine the first time I run the build. The second time the build is run, the number is not incremented, it stays the same from previous build run. I am using 4.4.1 version of Gradle. I don't remember having the same problem with earlier versions.

My current workaround is to pass --no-daemon switch to Gradle. However, that way I am not able to benefit from daemon feature. Is there a way to use daemons ans still get the correct build number in Gradle build?

Swithbart answered 2/1, 2018 at 23:42 Comment(0)
P
10

Are you using Java 9? With Java 9 it is not possible for Gradle anymore to modify the environment of the Daemon - so you cannot pass properties by using environment variables. You should see the following warning in your logs:

Warning: Unable able to set daemon's environment variables to match the client because: 
Java 9 does not support modifying environment variables.

You can pass the 'BUILD_NUMBER' as a system property (-DbuildNumber=$BUILD_NUMBER) or a Gradle project property (-PbuildNumber=$BUILD_NUMBER) from by the command line of Gradle instead.

Paulsen answered 5/1, 2018 at 21:59 Comment(1)
The environment variable problem with Java 9 was fixed in Gradle 4.10: docs.gradle.org/4.10/…Digestion
S
2

It seems this was discussed and solved in the gradle forums, by reading the System env in a special block:

class Globals {
  String buildNr =  System.getenv( 'BUILD_NUMBER' ).toString()
}
ext {
   globals = new Globals()
}

// reference it like:
println "value of BUILD_NUMBER = " + globals.buildNr
Saying answered 5/1, 2018 at 5:23 Comment(0)
P
0

I normally build a key-value store with a timestamp using redis to store build numbers. This way i can isolate and retrieve , and send data back forth between any client.

Psychotechnics answered 12/1, 2018 at 0:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.