TeamCity :: How can I access teamcity build ID in Java
Asked Answered
Q

4

16

Teamcity Build ID (which is different from BUILD_NUMBER) is used in various URLs. I want to send an email having path of a build's artifacts/ overview etc.

In Java, we can get currently running teamcity build number as follows:

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

This is because TC provides an environment variable namely BUILD_NUMBER. But unfortunately, there is no environment variable corresponding to BUILD_ID.

TeamCity does provide Configuration parameters (like teamcity.build.id) and System property (like system.teamcity.auth.userId) but I don't know how to access these using Java. I want to read the value of teamCity.build.id jusy like we can read environment variables names mentioned in How to fetch the Value of Teamcity Configuration in java?

Quittance answered 4/10, 2016 at 5:52 Comment(4)
Are you executing your code from inside of TeamCity plugin?Tubuliflorous
Above code in Java class file. If the java project is ran through one of TC build steps, above line gets executed and gives current build number.Quittance
The build id is not exposed as environment variable. Did you check any other way of including URL is good enough -- confluence.jetbrains.com/display/TCD10/…Valeda
Hay @Jayan.The link shared by you worked for me. I would be glad to share the bounty to you. Could you please put your comment as answer. I will accept that..Quittance
H
7

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)

Hystero answered 13/10, 2016 at 14:29 Comment(6)
Hi. I don't want to set the variable beforehand instead I want to read its value during executionQuittance
Yes, but during execution of what? The build, or when you're running the jar which is built from that build?Hystero
here I am referring to the execution of build. Thanks.Quittance
Then i have answered your question, it is available in your build runner using the code I've shownHystero
From maven I don't want to pass system.teamcity.build.id as param. Instead system.teamcity.build.id is a system parameter present in TeamCity. I want to read this value as it is needed in URL of a artifact and this value changes with every build. I am really appreciating your help and would be very happy to give bounty points if I get the answer I need.Quittance
This is the first time you mentioned maven! You can just access the variable directly in maven using ${teamcity.build.id} - I edited my answer.Hystero
Q
2

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");
Quittance answered 10/4, 2017 at 4:39 Comment(0)
E
0

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.

Euphony answered 4/10, 2016 at 12:7 Comment(1)
Hi. I don't want to set the variable beforehand instead I want to read its value during executionQuittance
A
0

A link to a TeamCity build can use build number instead of buildID. But, it requires buildTypeId as well (can be seen in build configuration page URL).

A sample of such link is:

https://buildserver/viewLog.html?buildTypeId=Project_Trunk&buildNumber=46523

Hope this helps someone.

Abdul answered 10/4, 2017 at 6:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.