Unable to pass build number into Gradle?

I cannot seem to get the TeamCity build number passed into my Gradle build.  I couldn't see build.number appearing anywhere, so I tried defining a build property that gets assigned the value %system.build.number% and I can't see that either.

I tried this in my Gradle build and was unable to see anything related to the build number in all the emitted properties:

    System.getProperties().each { System.println(it) }

Any tricks to accessing the build number in Gradle?

7 comments
Comment actions Permalink

Alan,

You can refer to buildnumber in gradle build like this:

println "Build number passed " + teamcity["build.number"]


Also, you may want to take a look at other properties available inside build script:

task printProperties << {
    teamcity.each { key, val ->
      println "##teamcity-property name='${key}' value='${val}'"
    }
  }

0
Comment actions Permalink

How would that interoperate with an interactive Gradle invocation at the command line? I get an error about the 'teamcity' property not being defined.
I need to support both.

0
Comment actions Permalink

As you can not access TeamCity from interactive mode, you will need some default value.
Take a look at the following example:

if (hasProperty("teamcity")) {
  buildNumber = teamcity["build.number"]
} else {
  buildNumber = "N/A"
}

0
Comment actions Permalink

I tried this, but it doesn't work:

distTar {

    baseName = project.name+'.'+

               project.version+'.'+

               System.getProperty("system.rnf.brach_name")

    if (hasProperty("teamcity")) {

        baseName = baseName +'.'+

                   teamcity['build.number']+'.'+

                   teamcity['build.vcs.number.1']

    }

    archiveName = baseName+'.tar'

    into(baseName) {

        from '.'

        include 'config/*'

        include 'run-script/*.sh'

    }

}

this works when I don't have teamcity (on my local machine), but even when it runs in teamcity, it doesn't add the build.number.

When I have:

distTar {

    baseName = project.name+'.'+

               project.version+'.'+

               System.getProperty("system.rnf.brach_name")+'.'+

               teamcity['build.number']+'.'+

               teamcity['build.vcs.number.1']

    archiveName = baseName+'.tar'

    into(baseName) {

        from '.'

        include 'config/*'

        include 'run-script/*.sh'

    }

}


then it works in teamcity, but not on the local machine.
0
Comment actions Permalink

Gavriel,

Would you mind attaching a build log from TeamCity server for variant that works locally and does not work in teamcity?

0
Comment actions Permalink
When it doesn't work in TeamCity, then the tar in artifacts has a name like: ArsAggregator.1.0.master.tar but when it works it generates a tar like: ArsAggregator.1.0.master.20.ed8279f4b477cc27f740fa735754cc8f3625e378.tar

The build log when it doesn't work looks like this:

[09:59:19]Checking for changes

[09:59:24]Publishing internal artifacts

[09:59:25][Publishing internal artifacts] Sending build.start.properties.gz file

[09:59:24]Clearing temporary directory: /backup/buildAgent2/temp/buildTmp

[09:59:24]Checkout directory: /backup/buildAgent2/work/15722a7e13941b77

[09:59:24]Updating sources: server side checkout

[09:59:25][Updating sources] Building incremental patch for VCS root: Git-Root

[09:59:25][Updating sources] Repository sources transferred

[09:59:25][Updating sources] Updating /backup/buildAgent2/work/15722a7e13941b77/ArsAggregator

[09:59:25]Starting: bash /opt/gradle/gradle-1.6/bin/gradle --init-script /backup/buildAgent2/plugins/gradle-runner/scripts/init.gradle -Dsystem.rnf.brach_name=master ArsAggregator:clean ArsAggregator:distTar

[09:59:25]in directory: /backup/buildAgent2/work/15722a7e13941b77

[09:59:27]Init lib: /backup/buildAgent2/lib/serviceMessages.jar:/backup/buildAgent2/lib/runtime-util.jar:/backup/buildAgent2/plugins/gradle-runner/lib/gradle-runner-common.jar

[09:59:28]Starting Gradle in TeamCity build 21

[09:59:31]Starting Gradle in TeamCity build 21

[09:59:31]Starting Gradle in TeamCity build 21

[09:59:34]Starting Gradle in TeamCity build 21

[09:59:34]Starting Gradle in TeamCity build 21

[09:59:34]Starting Gradle in TeamCity build 21

[09:59:34]Starting Gradle in TeamCity build 21

[09:59:34]Starting Gradle in TeamCity build 21

[09:59:34]Starting Gradle in TeamCity build 21

[09:59:34]Starting Gradle in TeamCity build 21

[09:59:34]Starting Gradle in TeamCity build 21

[09:59:34]Starting Gradle in TeamCity build 21

[09:59:34]Starting Gradle in TeamCity build 21

[09:59:34]Starting Gradle in TeamCity build 21

[09:59:34]Starting Gradle in TeamCity build 21

[09:59:34]Starting Gradle in TeamCity build 21

[09:59:34]Starting Gradle in TeamCity build 21

[09:59:35]:ArsAggregator:clean

[09:59:35]:Analytics:compileJava UP-TO-DATE

[09:59:35]:Analytics:processResources UP-TO-DATE

[09:59:35]:Analytics:classes UP-TO-DATE

[09:59:35]:Analytics:jar UP-TO-DATE

[09:59:36]:BaseAggApplication:compileJava UP-TO-DATE

[09:59:36]:BaseAggApplication:processResources UP-TO-DATE

[09:59:36]:BaseAggApplication:classes UP-TO-DATE

[09:59:36]:BaseAggApplication:jar UP-TO-DATE

[09:59:36]:ArsAggregator:compileJava (1s)

[09:59:37]:ArsAggregator:processResources

[09:59:37]:ArsAggregator:classes

[09:59:38]:ArsAggregator:jar

[09:59:38]:ArsAggregator:startScripts

[09:59:38]:ArsAggregator:distTar

[09:59:39]

[09:59:39]BUILD SUCCESSFUL

[09:59:39]

[09:59:39]Total time: 14.005 secs

[09:59:39]Process exited with code 0

[09:59:39]Publishing internal artifacts

[09:59:39][Publishing internal artifacts] Sending build.finish.properties.gz file

[09:59:39]Publishing artifacts

[09:59:39][Publishing artifacts] Collecting files to publish: [cron/*, deploy/*, config/** => config, ArsAggregator/build/distributions/**]

[09:59:39][Publishing artifacts] Sending cron/*

[09:59:39][Publishing artifacts] Sending config/**

[09:59:39]Build finished
0
Comment actions Permalink

Thank you for the log.

Groovy applies hasProperty("teamcity")  call to Task instance. Please, specify Project instance explicitly like this:

if (project.hasProperty("teamcity")) {
...
}


it should fix the issue

0

Please sign in to leave a comment.