Search code examples
gradleteamcitybuild.gradlegradle-taskgradle-git

Update TeamCity Build Number with version in Gradle build script


I have a gradle build script as below:

buildscript {
    repositories {
         //Some repo
    }
        dependencies {
            classpath "com.palantir.gradle.gitversion:gradle-git-version:0.8.0"
        }
    }
}
apply plugin: 'com.palantir.git-version'

version gitVersion()

I would like to update TeamCity build number with the value of version from the gitVersion() function. Is there anyway to do that?

I know in Maven world, for TeamCity to pick-up the version in pom.xml file, I can specify TeamCity build number equal to %maven.project.version%. But how to achieve the same thing with Gradle?

An attempt:

Also, I thought of a solution to create a Gradle Exec task to update TeamCity build number via Service Message (doc here):

task updateTeamCityBuildNumber(type: Exec) {
    x=version
    echo $x
    echo "##teamcity[setParameter name='buildNumber' value='${x}']"
}

But I got this error: Could not set unknown property 'x' for task ':updateTeamCityBuildNumber' of type org.gradle.api.tasks.Exec

So another question on this direction is that how do I pass the variable version into the Gradle Exec task and refer to it in the "echo" statement below?

(My TeamCity build agent is Linux)

Thanks and let me know if you need more details


Solution

  • Try replacing echo command with

    println "##teamcity[buildNumber '${version}']"
    

    There is also a documentation page on Confluence.