TeamCity :: How can I access teamcity build ID in Java TeamCity :: How can I access teamcity build ID in Java selenium selenium

TeamCity :: How can I access teamcity build ID in Java


Are you executing the java code using a build runner?

If so, then you should be able to pass %system.teamcity.build.id% to the runner, and make it available to your code.

i.e. If you're using the command line runner

java -Dbuild_id=%system.teamcity.build.id%

which you can then access as system arguments

Or if you're using gradle, you can do something like

if (project.hasProperty("teamcity")) {    version = project.teamcity["teamcity.build.id"]}

and pass 'version' to the java command line.

In maven, you can just access it using:

${teamcity.build.id}

in your pom.xml

(I could do with a little more info about how you're running java to answer this specifically)


I've noticed that lots of people want to know the answer to this question.Fortunately with the help of comment from @Jayan I was able to do solve my exact problem which was how to get URL for build artifacts.

As mentioned in link https://confluence.jetbrains.com/display/TCD10/Patterns+For+Accessing+Build+Artifacts, by default, TeamCity uses Internal Build ID for the path that can be used to access build artifacts:

/repository/download/BUILD_TYPE_EXT_ID/BUILD_ID:id/ARTIFACT_PATH

Accessing build Id could be difficult in the runtime(That is the reason of this question), but we can also use Build Number to access artifacts

/repository/download/BUILD_TYPE_EXT_ID/BUILD_NUMBER/ARTIFACT_PATH

And as shown in my question build number can be accessed as

String BUILD_NUMBER= System.getenv("BUILD_NUMBER");

and

String BUILD_TYPE_EXT_ID = System.getenv("TEAMCITY_BUILDCONF_NAME");


Yes, but you can create env var with value "%system.teamcity.buildType.id%" and read it in build. After that you can do an api request like:

$APIURL = "${API_BaseUrl}/httpAuth/app/rest/builds/?locator=buildType:${API_BuildType},state:running,count:1"$APIXML = (Invoke-RestMethod -Headers $API_CredentialsHeader -Credential $API_Credentials -Uri $APIURL -Method GET -ContentType "application/xml" -TimeoutSec 20)# Here you build id.$APIXML.builds.build.id

This is PS example. But idea the same. In Java that might be more easy.