Accessing Teamcity build number in Gradle build sc

2019-04-10 04:51发布

问题:

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.

回答1:

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



回答2:

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



回答3:

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.



回答4:

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.