Access Teamcity build number in Gradle build script

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

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

Thanks.

+4
source share
4 answers

These are just the properties of the JVM system that TeamCity sets for the JVM Ant / Gradle. You can access them using regular Java tools, such as System.getProperty("build.number") .

+4
source

If you are developing an Android application, you can access build.number to update the 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

0
source

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

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

Very confusing when reading the overfulfilled page https://confluence.jetbrains.com/display/TCD9/Configuring+Build+Parameters#ConfiguringBuildParameters-ConfigurationParameters using the "system" (in the "system properties") if I thought they like the JVM System, 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, which says use the "system." I tried setting the Gradle property "repositoryUrl" to "system.org.gradle.project.repositoryUrl" ... This did not work. adding "properties" to the Gradle command in the command city revealed that there is a Gradle property called "org.gradle.project.repositoryUrl"

As a result of trial and error, I came across the obvious ... and was going to report it as a docs error .. but then found it in the documentation

Summary: In TeamCity 9.1.5+ use

 system.<propertyName> 

eg.

 system.repositoryUrl 

In Gradle, they are available as rootProject (or project) "properties (not" system properties "... for example,

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

and 500 other ways to do the same.

0
source

I tested a lot of cases with gradle and introduced external build.number variables. In my case, I use docker as the build environment, so the environment provided by the command will not be successfully implemented.

There are two options for entering variables in gradle. Note that the differences are below -D and -P below.

 // 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 the order of the commands, but task [option] works.

An example build.gradle script is given 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 android versionCode, so Integer parsing is required with :? zero treatment.

0
source

All Articles