7

How can I access the build number and VCS checkout number in a Gradle script executed by Teamcity?

In Ant I can use ${build.number} and ${build.vcs.number.1} respectively.

Thank you.

neontapir
  • 4,698
  • 3
  • 37
  • 52
Paul
  • 999
  • 1
  • 12
  • 29

4 Answers4

6

These are simply JVM system properties that TeamCity sets for the Ant/Gradle JVM. You can access them with the usual Java means, for example System.getProperty("build.number").

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • 3
    Note that this answer is now a little dated. Assuming the use of the "Gradle" runner in TeamCity (which implicitly applies a TeamCity-defined init script), you now have to do something like this: `project.teamcity["build.number"]`. [See this question for other details.](http://stackoverflow.com/questions/20189846/how-can-i-define-the-teamcitybuild-number-property-in-gradle-from-command-li) – superEb May 19 '14 at 17:37
  • Any idea how to access Android app version number in TeamCity VCS labelling? – jimmy0251 Jan 07 '16 at 05:28
1

If you are developing Android application you can access build.number to update apk file name accordingly:

 defaultConfig {
    applicationId "com.mydemoci"
    minSdkVersion 14
    targetSdkVersion 23
    versionCode 1
    versionName "0.7"

    ext.buildNumber = System.getProperty("build.number") ?: "Regular"
    archivesBaseName = "$applicationId-v$versionName-b$buildNumber"
 } 

To test locally just run gradlew clean build -Dbuild.number=123

Roman
  • 860
  • 14
  • 14
1

I tested with many cases with gradle and injecting build.number outside variables. In my case, I use docker as a build environment so teamcity-provided environment does not be injected successfully.

There are two possibilities to inject variables to gradle. Note that -D and -P differences in below example.

// 1. gradle build -Pbuild.number=555
aaa = rootProject.properties.get("build.number") 

// 2. gradle bulid -Dbuild.number=444
bbb = System.getProperty("build.number")

gradle --help says [option] task as a command order but task [option] works.

An example build.gradle script is as below;

ext {
    buildNumber = (rootProject.properties.get("build.number") as Integer) ?: 1
}

android {

    defaultConfig {
        versionCode project.buildNumber
        versionName "1.3.7." + project.buildNumber.toString()
    }

In my case, I want to use build.number as a android versionCode, so parsing to Integer is required with :? null treatment.

Youngjae
  • 24,352
  • 18
  • 113
  • 198
0

As of TeamCity 9.1.5 this is changed. "Properties" (both "system" and "teamcity" ) properties are set as project ext properties.

https://confluence.jetbrains.com/display/TCD9/Defining+and+Using+Build+Parameters+in+Build+Configuration

Very confusing when reading the overveiw page https://confluence.jetbrains.com/display/TCD9/Configuring+Build+Parameters#ConfiguringBuildParameters-ConfigurationParameters the use of "system" (in "system properties") had me thinking they were set as JVM System properites so I tried the Gradle mechanism as described here: https://docs.gradle.org/current/userguide/build_environment.html which says to use "org.gradle.project." .. adding that with TeamCity that says to use "system." I tried to set a gradle property "repositoryUrl" as "system.org.gradle.project.repositoryUrl" ... This didn't work. adding "properties" to the gradle command in team city showed that there was a gradle property called "org.gradle.project.repositoryUrl"

By trial and error I stumbled on the obvious ... and was about to report it as a docs bug .. but then found it in the documentation

Summary: In TeamCity 9.1.5+ use

system.<propertyName>

e.g.

system.repositoryUrl 

In Gradle these are accessable as rootProject (or project) "properties" (not "system properties" ... e.g.

println repositoryUrl 
println project.repositoryUrl
println rootProject.repositoryUrl
println project.properties["repositoryUrl"]
println rootProject.ext.repositoryUrl
println "${repositryUrl}"
assert  hasProperty("repositoryUrl")

and the 500 other ways of doing the same thing.

DALDEI
  • 3,722
  • 13
  • 9